IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
EX.NO: 1
CLASS AND OBJECTS
AIM
To write a C++ program to demonstrate the concept of class and objects.
ALGORITHM
1. Start the program.
2. Define a class Student with data members like name, roll number, and marks.
3. Include member functions to input and display student details.
4. In the main() function, create an object of the class.
5. Use the object to call the input and display functions.
6. End the program.
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 1
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
SOURCE CODE
#include <iostream>
using namespace std;
// Define class
class Student {
private:
string name;
int rollNo;
float marks;
public:
// Function to get student details
void getDetails() {
cout << "Enter name: ";
cin >> name;
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter marks: ";
cin >> marks;
}
// Function to display student details
void displayDetails() {
cout << "\nStudent Details:" << endl;
cout << "Name: " << name << endl;
cout << "Roll Number: " << rollNo << endl;
cout << "Marks: " << marks << endl;
}
};
// Main function
int main() {
Student s1; // Creating object
s1.getDetails(); // Calling member function
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 2
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
s1.displayDetails(); // Calling member function
return 0;
}
OUTPUT
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 3
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
RESULT:
Thus the program was created and executed successfully.
EX.NO: 2
CONSTRUCTOR, COPY CONSTRUCTOR AND DESTRUCTOR.
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 4
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
AIM:
To write a C++ program to demonstrate Constructor, Copy Constructor, and Destructor.
ALGORITHM:
1. Start the program.
2. Define a class Example with a data member.
3. Create:
o A constructor to initialize the object.
o A copy constructor to copy data from another object.
o A destructor to display a message when the object is destroyed.
4. In the main() function:
o Create an object using the normal constructor.
o Create another object using the copy constructor.
5. End the program.
SOURCE CODE:
#include <iostream>
using namespace std;
class Example {
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 5
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
private:
int value;
public:
// Constructor
Example(int v) {
value = v;
cout << "Constructor called. Value = " << value << endl;
}
// Copy Constructor
Example(const Example &obj) {
value = obj.value;
cout << "Copy Constructor called. Value = " << value << endl;
}
// Destructor
~Example() {
cout << "Destructor called. Value = " << value << endl;
}
};
int main() {
Example obj1(100); // Calls constructor
Example obj2 = obj1; // Calls copy constructor
return 0; // Destructor will be called automatically for obj2 and obj1
}
OUTPUT
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 6
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 7
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
RESULT:
Thus the program was created and executed successfully.
EX.NO: 3
FUNCTION OVERLOADING, DEFAULT ARGUMENTS AND INLINE
FUNCTION.
AIM
To demonstrate three distinct C++ features: function overloading (defining multiple
functions with the same name but different parameters), default arguments (providing default
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 8
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
values for function parameters), and inline functions (hinting to the compiler to insert function
code directly at the call site for potential performance optimization).
ALGORITHM
1. Function Overloading:
o Define a function add that takes two integers and returns their sum.
o Overload add to take three integers and return their sum.
o Overload add to take two doubles and return their sum.
2. Default Arguments:
o Define a function greet that takes a std::string name with a default value of
"Guest" and an int hour with a default value of 12.
o Call greet with different combinations of arguments (all, some, none).
3. Inline Function:
o Define a small function multiply using the inline keyword that takes two integers
and returns their product.
o Call multiply and explain the concept of inlining.
SOURCE CODE
#include<iostream>
using namespace std;
// Inline function
inline int square(int x) {
return x * x;
}
// Function overloading
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 9
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
void display(int a) {
cout << "Integer: " << a << endl;
}
void display(double a) {
cout << "Double: " << a << endl;
}
// Function with default arguments
void greet(string name = "User") {
cout << "Hello, " << name << "!" << endl;
}
int main() {
// Inline function
cout << "Square of 5: " << square(5) << endl;
// Function overloading
display(10);
display(10.5);
// Default arguments
greet();
greet("Alice");
return 0;
}
OUTPUT
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 10
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 11
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
RESULT
Thus the program was created and executed successfully.
EX.NO: 4
FRIEND FUNCTIONS.
AIM
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 12
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
To demonstrate the concept of a friend function in C++, which is a non-member
function that is granted special permission to access the private and protected members of a
class.
ALGORITHM
1. Start the program.
2. Define a class Box with three private data members: length, breadth, and height.
3. Create a constructor to initialize the values of length, breadth, and height.
4. Declare a friend function displayVolume(Box) inside the class Box.
5. Define the friend function outside the class using the friend keyword:
o It receives an object of class Box.
o It accesses the private members using the object.
o It calculates volume as: length * breadth * height.
o It displays the result.
6. In the main() function, create an object box1 with specific values.
7. Call the friend function by passing the object to it.
8. End the program.
SOURCE CODE
#include<iostream>
using namespace std;
class Box {
private:
int length, breadth, height;
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 13
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
public:
Box(int l, int b, int h) {
length = l;
breadth = b;
height = h;
}
// Friend function declaration
friend void displayVolume(Box b);
};
// Friend function definition
void displayVolume(Box b) {
int volume = b.length * b.breadth * b.height;
cout << "Volume of Box = " << volume << endl;
}
int main() {
Box box1(4, 5, 6);
displayVolume(box1);
return 0;
}
OUTPUT
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 14
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 15
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
RESULT
Thus the program was created and executed successfully
EX.NO: 5
PASSING OBJECTS TO FUNCTIONS
AIM
To write a C++ program to demonstrate the concept of passing objects to functions.
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 16
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
ALGORITHM
1. Start the program.
2. Define a class Student with data members name and marks.
3. Create a member function to get input from the user.
4. Create a non-member function that takes a Student object as an argument and displays the
details.
5. In the main() function, create a Student object and call the functions.
6. End the program.
SOURCE CODE
#include <iostream>
using namespace std;
// Class Definition
class Student {
private:
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 17
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
string name;
int marks;
public:
// Member function to input student details
void getData() {
cout << "Enter student name: ";
cin >> name;
cout << "Enter marks: ";
cin >> marks;
// Friend function declaration
friend void displayData(Student s); // Passing object to function
};
// Function definition to display data (object passed by value)
void displayData(Student s) {
cout << "\n--- Student Details ---" << endl;
cout << "Name : " << s.name << endl;
cout << "Marks : " << s.marks << endl;
// Main function
int main() {
Student stud;
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 18
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
stud.getData(); // Call member function
displayData(stud); // Pass object to function
return 0;
OUTPUT
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 19
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
RESULT
Thus the program was created and executed successfully
EX.NO: 6 POINTERS AND DYNAMIC MEMORY ALLOCATION USING NEW
AND DELETE OPERATORS
AIM
To write a C++ program to demonstrate pointers and dynamic memory allocation using
new and delete operators.
ALGORITHM
1. Start the program.
2. Declare a pointer variable.
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 20
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
3. Use the new operator to allocate memory dynamically.
4. Store and display values using the pointer.
5. Use the delete operator to free the allocated memory.
6. End the program.
SOURCE CODE
#include <iostream>
using namespace std;
int main()
{
// Step 1: Declare pointer
int *ptr;
// Step 2: Dynamic memory allocation
ptr = new int; // Allocate memory for one integer
// Step 3: Store value in allocated memory
cout << "Enter a number: ";
cin >> *ptr;
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 21
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
// Step 4: Display the value
cout << "You entered: " << *ptr << endl;
// Step 5: Free the memory
delete ptr;
return 0;
}
OUTPUT
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 22
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 23
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
RESULT
Thus the program was created and executed successfully
EX.NO: 7
UNARY OPERATOR OVERLOADING
AIM
To write a C++ program to demonstrate unary operator overloading using a class.
ALGORITHM
1. Start the program.
2. Define a class with a data member (e.g., int num).
3. Create a constructor to initialize the value.
4. Overload the unary - operator using a member function.
5. In the main function, create an object and display the original value.
6. Use the overloaded operator and display the updated value.
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 24
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
7. End the program.
SOURCE CODE
#include <iostream>
using namespace std;
class Number
{
private:
int num;
public:
// Constructor
Number(int n)
{
num = n;
}
// Overloading unary minus operator
void operator - ()
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 25
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
{
num = -num;
}
// Function to display the number
void display()
{
cout << "Value: " << num << endl;
}
};
int main()
{
// Create object
Number n1(10);
cout << "Before overloading:" << endl;
n1.display();
// Use overloaded unary minus
-n1;
cout << "After overloading:" << endl;
n1.display();
return 0;
}
OUTPUT
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 26
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 27
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
RESULT
Thus the program was created and executed successfully
EX.NO: 8
BINARY OPERATOR OVERLOADING
AIM
To write a C++ program to demonstrate binary operator overloading using a class.
ALGORITHM
1. Start the program.
2. Define a class with two data members.
3. Create a constructor to initialize the data.
4. Overload the binary + operator using a member function.
5. Return a new object with the result of addition.
6. In main(), create two objects and add them using +.
7. Display the result using a member function.
8. End the program.
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 28
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
SOURCE CODE
#include <iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
// Constructor
Complex(int r = 0, int i = 0) {
real = r;
imag = i;
}
// Overload + operator
Complex operator + (Complex obj) {
Complex temp;
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 29
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
temp.real = real + obj.real;
temp.imag = imag + obj.imag;
return temp;
}
// Display function
void display() {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex c1(3, 4), c2(2, 5), c3;
cout << "First Complex Number: ";
c1.display();
cout << "Second Complex Number: ";
c2.display();
// Using overloaded + operator
c3 = c1 + c2;
cout << "Sum of Complex Numbers: ";
c3.display();
return 0;
}
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 30
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
OUTPUT
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 31
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
RESULT
Thus the program was created and executed successfully
EX.NO: 9
SINGLE INHERITANCE, MULTILEVEL INHERITANCE,
MULTIPLE INHERITANCE, AND HIERARCHICAL INHERITANCE.
AIM
To write a C++ program to demonstrate Single Inheritance, Multilevel Inheritance,
Multiple Inheritance, and Hierarchical Inheritance.
ALGORITHM
1. Start the program.
2. Create a base class Base.
3. Create a class SingleDerived that inherits from Base to show Single Inheritance.
4. Create class Level2 that inherits from SingleDerived to show Multilevel Inheritance.
5. Create another class SecondBase. Create MultipleDerived that inherits from both Base
and SecondBase to show Multiple Inheritance.
6. Create Derived1 and Derived2 classes from Base to show Hierarchical Inheritance.
7. Call respective member functions and display results.
8. End the program.
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 32
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
SOURCE CODE
#include <iostream>
using namespace std;
// Base class for all types
class Base {
public:
void showBase() {
cout << "Base class called." << endl;
}
};
// -------------------- SINGLE INHERITANCE --------------------
class SingleDerived : public Base {
public:
void showSingle() {
cout << "Single Inheritance Derived class called." << endl;
}
};
// -------------------- MULTILEVEL INHERITANCE --------------------
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 33
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
class Level2 : public SingleDerived {
public:
void showLevel2() {
cout << "Multilevel Inheritance Level2 class called." << endl;
}
};
// -------------------- MULTIPLE INHERITANCE --------------------
class SecondBase {
public:
void showSecondBase() {
cout << "Second Base class called." << endl;
}
};
class MultipleDerived : public Base, public SecondBase {
public:
void showMultiple() {
cout << "Multiple Inheritance Derived class called." << endl;
}
};
// -------------------- HIERARCHICAL INHERITANCE --------------------
class Derived1 : public Base {
public:
void showDerived1() {
cout << "Hierarchical Inheritance Derived1 class called." << endl;
}
};
class Derived2 : public Base {
public:
void showDerived2() {
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 34
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
cout << "Hierarchical Inheritance Derived2 class called." << endl;
}
};
// -------------------- MAIN FUNCTION --------------------
int main() {
// Single Inheritance
cout << "\n--- Single Inheritance ---" << endl;
SingleDerived obj1;
obj1.showBase();
obj1.showSingle();
// Multilevel Inheritance
cout << "\n--- Multilevel Inheritance ---" << endl;
Level2 obj2;
obj2.showBase();
obj2.showSingle();
obj2.showLevel2();
// Multiple Inheritance
cout << "\n--- Multiple Inheritance ---" << endl;
MultipleDerived obj3;
obj3.showBase();
obj3.showSecondBase();
obj3.showMultiple();
// Hierarchical Inheritance
cout << "\n--- Hierarchical Inheritance ---" << endl;
Derived1 d1;
Derived2 d2;
d1.showBase();
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 35
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
d1.showDerived1();
d2.showBase();
d2.showDerived2();
return 0;
}
OUTPUT
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 36
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
RESULT
Thus the program was created and executed successfully
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 37
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
EX.NO: 10
VIRTUAL FUNCTIONS.
AIM:
To write a C++ program to demonstrate the concept of virtual functions using a base
class and derived classes.
ALGORITHM
1. Start the program.
2. Define a base class named Shape.
o Declare a virtual function named draw() that prints a generic message.
3. Define a derived class named Circle that:
o Overrides the draw() function to print "Drawing a circle."
4. Define another derived class named Square that:
o Overrides the draw() function to print "Drawing a square."
5. In the main() function:
o Declare a pointer of the base class Shape* s.
o Create objects of Circle and Square.
6. Assign the address of the Circle object to the base class pointer and call s->draw().
o This will call the overridden draw() in the Circle class due to runtime
polymorphism.
7. Assign the address of the Square object to the base class pointer and call s->draw().
o This will call the overridden draw() in the Square class.
8. End the program.
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 38
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
SOURCE CODE
#include <iostream>
using namespace std;
// Base class
class Shape {
public:
// Virtual function
virtual void draw() {
cout << "Drawing a generic shape." << endl;
}
};
// Derived class
class Circle : public Shape {
public:
// Overridden function
void draw() {
cout << "Drawing a circle." << endl;
}
};
// Another derived class
class Square : public Shape {
public:
// Overridden function
void draw() {
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 39
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
cout << "Drawing a square." << endl;
}
};
int main() {
Shape* s; // Base class pointer
Circle c;
Square sq;
// Point to Circle object
s = &c;
s->draw(); // Calls Circle's draw()
// Point to Square object
s = &sq;
s->draw(); // Calls Square's draw()
return 0;
}
OUTPUT
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 40
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
RESULT
Thus the program was created and executed successfully
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 41
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
EX.NO: 11
MANIPULATE A TEXT FILE.
AIM
To write a C++ program to demonstrate basic text file manipulation — writing, reading,
and appending using file streams.
ALGORITHM:
1. Start the program.
2. Declare a string variable line to store lines read from the file.
3. Open a text file (example.txt) using ofstream for writing:
o If the file opens successfully:
Write two lines to the file.
Close the file.
Display a message: "Data written to file successfully."
o Else:
Display an error message.
4. Open the same file using ofstream in append mode (ios::app):
o If the file opens successfully:
Append one more line to the file.
Close the file.
Display a message: "Data appended to file successfully."
o Else:
Display an error message.
5. Open the file using ifstream to read its contents:
o If the file opens successfully:
Display "Reading file contents:"
Read and display each line using a loop.
Close the file.
o Else:
Display an error message.
6. End the program.
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 42
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
SOURCE CODE
#include <iostream>
#include <fstream>
using namespace std;
int main() {
string line;
// ---------- Write to file ----------
ofstream writeFile("example.txt"); // Create and open file for writing
if (writeFile.is_open()) {
writeFile << "Hello, this is BHUVANA.\n";
writeFile << "This is C PLUS PLUS PROGRAM.\n";
writeFile.close();
cout << "Data written to file successfully.\n";
} else {
cout << "Unable to open file for writing.\n";
}
// ---------- Append to file ----------
ofstream appendFile("example.txt", ios::app); // Open file in append mode
if (appendFile.is_open()) {
appendFile << "This is an appended line.\n";
appendFile.close();
cout << "Data appended to file successfully.\n";
} else {
cout << "Unable to open file for appending.\n";
}
// ---------- Read from file ----------
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 43
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
ifstream readFile("example.txt"); // Open file for reading
if (readFile.is_open()) {
cout << "\nReading file contents:\n";
while (getline(readFile, line)) {
cout << line << endl;
}
readFile.close();
} else {
cout << "Unable to open file for reading.\n";
}
return 0;
}
OUTPUT
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 44
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
RESULT
Thus the program was created and executed successfully
EX.NO: 12
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 45
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
SEQUENTIAL I/O OPERATIONS ON A FILE
AIM
To write a C++ program to perform sequential input/output (I/O) operations on a text
file using file streams.
ALGORITHM
1. Start the program.
2. Declare necessary variables.
3. Use ofstream to open a file and write data to it sequentially.
4. Close the file.
5. Use ifstream to open the same file and read data sequentially.
6. Display the contents on the screen.
7. Close the file.
8. End the program.
SOURCE CODE
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 46
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
#include <iostream>
#include <fstream>
using namespace std;
int main() {
string name;
int age;
// ---------- Writing to the file ----------
ofstream outFile("student.txt");
if (outFile.is_open()) {
outFile << "Alice 20\n";
outFile << "Bob 22\n";
outFile << "Charlie 19\n";
outFile.close();
cout << "Data written to file successfully.\n";
} else {
cout << "Error opening file for writing.\n";
}
// ---------- Reading from the file ----------
ifstream inFile("student.txt");
if (inFile.is_open()) {
cout << "\nReading data from file:\n";
while (inFile >> name >> age) {
cout << "Name: " << name << ", Age: " << age << endl;
}
inFile.close();
} else {
cout << "Error opening file for reading.\n";
}
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 47
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
return 0;
}
OUTPUT
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 48
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
RESULT
Thus the program was created and executed successfully
EX.NO: 13
FIND THE BIGGEST NUMBER USING COMMAND LINE ARGUMENTS
AIM
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 49
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
To write a C++ program to find the biggest number using command line arguments.
ALGORITHM
1. Start the program.
2. Use argc to check the number of command line arguments.
3. Use argv[] to access each argument (as string), convert to integer using atoi().
4. Initialize the first number as the largest.
5. Loop through remaining arguments and update the largest if a bigger number is found.
6. Display the biggest number.
7. End the program.
HOW TO RUN COMMAND LINE ARGUMENTS IN DEV C++
1. Go to "Execute" → "Parameters".
2. Add values like: 45 23 67 89 12 in the command-line arguments box.
3. Click OK and run the program.
SOURCE CODE
#include <iostream>
#include <cstdlib> // for atoi()
using namespace std;
int main(int argc, char* argv[]) {
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 50
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
if (argc < 2) {
cout << "Please provide numbers as command line arguments.\n";
return 1;
}
int max = atoi(argv[1]); // Convert first argument to integer
for (int i = 2; i < argc; i++) {
int num = atoi(argv[i]); // Convert argument to integer
if (num > max) {
max = num;
}
}
cout << "The biggest number is: " << max << endl;
return 0;
}
OUTPUT
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 51
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 52
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
RESULT
Thus the program was created and executed successfully
EX.NO: 14
CLASS TEMPLATE
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 53
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
AIM
To write a C++ program that demonstrates Class Templates for creating generic classes
that can work with different data types.
ALGORITHM
1. Start the program.
2. Define a class template with a type parameter T.
3. Add a constructor to initialize values.
4. Create a member function to display the value.
5. In the main() function, create objects with different data types like int, float, char, etc.
6. Call the member function to show how the class works with each type.
7. End the program.
SOURCE CODE
#include <iostream>
using namespace std;
// Class template
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 54
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
template <class T>
class Sample {
private:
T value;
public:
Sample(T v) {
value = v;
}
void display() {
cout << "Value: " << value << endl;
}
};
int main() {
// Object with int
Sample<int> obj1(100);
obj1.display();
// Object with float
Sample<float> obj2(45.67);
obj2.display();
// Object with char
Sample<char> obj3('A');
obj3.display();
return 0;
}
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 55
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
OUTPUT
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 56
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
RESULT
Thus the program was created and executed successfully
EX.NO: 15
FUNCTION TEMPLATE.
AIM
To write a C++ program that demonstrates the use of function templates to perform
operations on different data types.
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 57
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
ALGORITHM
1. Start the program.
2. Define a function template that takes two parameters and returns the greater one.
3. In the main() function, call the template function with different data types (int, float,
char).
4. Display the results.
5. End the program.
SOURCE CODE
#include <iostream>
using namespace std;
// Function Template to find the maximum of two values
template <typename T>
T findMax(T a, T b) {
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 58
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
return (a > b) ? a : b;
}
int main() {
// Integer comparison
cout << "Max of 10 and 20 is: " << findMax(10, 20) << endl;
// Float comparison
cout << "Max of 5.5 and 2.3 is: " << findMax(5.5f, 2.3f) << endl;
// Character comparison
cout << "Max of 'A' and 'Z' is: " << findMax('A', 'Z') << endl;
return 0;
}
OUTPUT
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 59
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 60
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
RESULT
Thus the program was created and executed successfully
EX.NO: 16
EXCEPTION HANDLING.
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 61
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
AIM
To write a C++ program that demonstrates exception handling using try, throw, and
catch blocks.
ALGORITHM
1. Start the program.
2. Take two integers as input: numerator and denominator.
3. Use a try block to check if the denominator is zero.
4. If zero, throw an exception.
5. Catch the exception using a catch block and display an error message.
6. If not zero, perform division and display the result.
7. End the program.
SOURCE CODE
#include <iostream>
using namespace std;
int main() {
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 62
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
int numerator, denominator;
float result;
cout << "Enter numerator: ";
cin >> numerator;
cout << "Enter denominator: ";
cin >> denominator;
try {
if (denominator == 0)
throw "Division by zero is not allowed.";
result = (float)numerator / denominator;
cout << "Result: " << result << endl;
}
catch (const char* msg) {
cout << "Exception: " << msg << endl;
}
return 0;
}
OUTPUT
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 63
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 64
IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM.
December 30, 1899
RESULT
Thus the program was created and executed successfully
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++ LAB [23UPCS15] 65