Lab: Object Oriented Programming using C++
1. Write a Program for Swapping of two numbers.
#include <iostream>
using namespace std;
int main()
{
int a, b, temp;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "Before swapping:" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
temp = a;
a = b;
b = temp;
cout << "After swapping:" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
return 0;
}
2. Write a Program to find sum of four numbers using default argument
passing.
#include <iostream>
using namespace std;
// Function with default arguments
int sum(int a = 0, int b = 0, int c = 0, int d = 0)
{
return a + b + c + d;
}
int main()
{
int n1, n2, n3, n4;
cout << "Enter four numbers: ";
cin >> n1 >> n2 >> n3 >> n4;
cout << "Sum = " << sum(n1, n2, n3, n4) << endl;
return 0;
}
3. Write a Program to find square and cube of a number using inline function.
#include <iostream>
using namespace std;
// Inline function to find square
inline int square(int n)
{
return n * n;
}
// Inline function to find cube
inline int cube(int n)
{
return n * n * n;
}
int main()
{
int num;
cout << "Enter a number: ";
cin >> num;
cout << "Square = " << square(num) << endl;
cout << "Cube = " << cube(num) << endl;
return 0;
}
4. Write a Program to find the factorial of a number.
#include <iostream>
using namespace std;
int main()
{
int n;
long long factorial = 1;
cout << "Enter a number: ";
cin >> n;
for (int i = 1; i <= n; i++) {
factorial *= i;
}
cout << "Factorial of " << n << " = " << factorial << endl;
return 0;
}
5. Write a Program to find reverse of a number.
#include <iostream>
using namespace std;
int main()
{
int num, reverse = 0, rem;
cout << "Enter a number: ";
cin >> num;
while (num != 0)
{
rem = num % 10;
reverse = reverse * 10 + rem;
num = num / 10;
}
cout << "Reverse of the number = " << reverse << endl;
return 0;
}
6. Write a program to find sum of four numbers using default argument
passing in member function.
#include <iostream>
using namespace std;
class Sum
{
public:
int add(int a = 0, int b = 0, int c = 0, int d = 0)
{
return a + b + c + d;
}
};
int main()
{
Sum s;
int n1, n2, n3, n4;
cout << "Enter four numbers: ";
cin >> n1 >> n2 >> n3 >> n4;
cout << "Sum = " << [Link](n1, n2, n3, n4) << endl;
return 0;
}
7. Write a Program to find area of circle, triangle and rectangle using
function overloading.
#include <iostream>
using namespace std;
class Area
{
public:
// Area of Circle
float area(float radius)
{
return 3.14 * radius * radius;
}
// Area of Rectangle
int area(int length, int breadth)
{
return length * breadth;
}
// Area of Triangle
float area(float base, float height)
{
return (0.5 * base * height);
}
};
int main()
{
Area a;
cout << "Area of Circle (radius = 5) = "<< [Link](5.0f) << endl;
cout << "Area of Rectangle (length = 10, breadth = 5) = "<< [Link](10, 5)<< endl;
cout << "Area of Triangle (base = 8, height = 4) = "<< [Link](8.0f, 4.0f) << endl;
return 0;
}
[Link] a program to distinguish the properties of static and non-static data
members.
#include <iostream>
using namespace std;
class Student
{
private:
int rollNo; // Non-static data member
static int count; // Static data member
public:
Student()
{
count++;
}
void setData(int r)
{
rollNo = r;
}
void display()
{
cout << "Roll No: " << rollNo << endl;
cout << "Total Students: " << count << endl;
}
};
// Definition of static data member
int Student::count = 0;
int main()
{
Student s1, s2, s3;
[Link](101);
[Link](102);
[Link](103);
cout << "Student 1:" << endl;
[Link]();
cout << "\nStudent 2:" << endl;
[Link]();
cout << "\nStudent 3:" << endl;
[Link]();
return 0;
}
[Link] a program to show the method of accessing static private member
function.
#include <iostream>
using namespace std;
class Demo
{
private:
// Static private member function
static void showMessage()
{
cout << "Static Private Member Function Accessed!" << endl;
}
public:
// Public static member function to access private static function
static void accessFunction()
{
showMessage();
}
};
int main()
{
// Accessing the private static function through a public static function
Demo::accessFunction();
return 0;
}
10. Write a program to show the ways of calling constructors and destructors.
#include <iostream>
using namespace std;
class Demo
{
public:
// Default Constructor
Demo()
{
cout << "Default Constructor Called" << endl;
}
// Parameterized Constructor
Demo(int x)
{
cout << "Parameterized Constructor Called: " << x << endl;
}
// Copy Constructor
Demo(const Demo &obj)
{
cout << "Copy Constructor Called" << endl;
}
// Destructor
~Demo()
{
cout << "Destructor Called" << endl;
}
};
int main()
{
cout << "Object 1:" << endl;
Demo obj1; // Default constructor
cout << "\nObject 2:" << endl;
Demo obj2(10); // Parameterized constructor
cout << "\nObject 3:" << endl;
Demo obj3 = obj2; // Copy constructor
cout << "\nEnd of main function" << endl;
return 0;
Sample Output
Object 1:
Default Constructor Called
Object 2:
Parameterized Constructor Called: 10
Object 3:
Copy Constructor Called
End of main function
Destructor Called
Destructor Called
Destructor Called
11. Write a program to perform ++ operator overloading using member
function.
#include <iostream>
using namespace std;
class Number
{
private:
int value;
public:
// Constructor
Number(int v)
{
value = v;
}
// Overloading prefix ++ operator
void operator++()
{
++value;
}
// Display function
void display()
{
cout << "Value = " << value << endl;
}
};
int main()
{
Number n(10);
cout << "Before Increment: ";
[Link]();
++n; // Calls overloaded ++ operator
cout << "After Increment: ";
[Link]();
return 0;
}
12. Write a program to perform ++ operator overloading using friend
function.
#include <iostream>
using namespace std;
class Number
{
private:
int value;
public:
// Constructor
Number(int v)
{
value = v;
}
// Friend function declaration
friend void operator++(Number &n);
// Display function
void display()
{
cout << "Value = " << value << endl;
}
};
// Friend function definition
void operator++(Number &n)
{
++[Link];
}
int main()
{
Number n(10);
cout << "Before Increment: ";
[Link]();
++n; // Calls overloaded ++ operator
cout << "After Increment: ";
[Link]();
return 0;
}
13. Write a program to perform + operator overloading for two complex
number addition.
#include <iostream>
using namespace std;
class Complex
{
private:
int real, imag;
public:
// Constructor
Complex(int r = 0, int i = 0)
{
real = r;
imag = i;
}
// Overloading + operator
Complex operator+(Complex c)
{
Complex temp;
[Link] = real + [Link];
[Link] = imag + [Link];
return temp;
}
// Display function
void display()
{
cout << real << " + " << imag << "i" << endl;
}
};
int main()
{
Complex c1(4, 5);
Complex c2(3, 2);
Complex c3;
c3 = c1 + c2; // Calls overloaded + operator
cout << "First Complex Number: ";
[Link]();
cout << "Second Complex Number: ";
[Link]();
cout << "Sum of Complex Numbers: ";
[Link]();
return 0;
}
14. Write a program to perform + operator overloading for string
concatenation.
#include <iostream>
#include <cstring>
using namespace std;
class String
{
private:
char str[100];
public:
// Constructor
String(const char *s = "")
{
strcpy(str, s);
}
// Overloading + operator
String operator+(String s)
{
String temp;
strcpy([Link], str);
strcat([Link], [Link]);
return temp;
}
// Display function
void display()
{
cout << str << endl;
}
};
int main()
{
String s1("Hello ");
String s2("World");
String s3;
s3 = s1 + s2; // Calls overloaded + operator
cout << "First String: ";
[Link]();
cout << "Second String: ";
[Link]();
cout << "Concatenated String: ";
[Link]();
return 0;
}
15. Write a program to perform single inheritance.
#include <iostream>
using namespace std;
// Base Class
class Person
{
protected:
string name;
public:
void getName()
{
cout << "Enter Name: ";
cin >> name;
}
};
// Derived Class
class Student : public Person
{
private:
int rollNo;
public:
void getData()
{
getName(); // Accessing base class function
cout << "Enter Roll Number: ";
cin >> rollNo;
}
void display()
{
cout << "\nStudent Details" << endl;
cout << "Name: " << name << endl;
cout << "Roll Number: " << rollNo << endl;
}
};
int main()
{
Student s;
[Link]();
[Link]();
return 0;
}
16. Write a program to perform multiple inheritance.
#include <iostream>
using namespace std;
// Base Class 1
class Student
{
protected:
int rollNo;
public:
void getRollNo()
{
cout << "Enter Roll Number: ";
cin >> rollNo;
}
};
// Base Class 2
class Marks
{
protected:
int marks;
public:
void getMarks()
{
cout << "Enter Marks: ";
cin >> marks;
}
};
// Derived Class inheriting from both Student and Marks
class Result : public Student, public Marks
{
public:
void display()
{
cout << "\nStudent Result" << endl;
cout << "Roll Number: " << rollNo << endl;
cout << "Marks: " << marks << endl;
}
};
int main()
{
Result r;
[Link]();
[Link]();
[Link]();
return 0;
}
17. Write a program to create an integer array using new operator and find
the sum and average of array elements.
#include <iostream>
using namespace std;
int main()
{
int n, sum = 0;
float average;
cout << "Enter the number of elements: ";
cin >> n;
// Dynamic array allocation using new operator
int *arr = new int[n];
cout << "Enter " << n << " elements:" << endl;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
sum += arr[i];
}
average = (float)sum / n;
cout << "\nSum = " << sum << endl;
cout << "Average = " << average << endl;
// Free dynamically allocated memory
delete[] arr;
return 0;
}
18. Write a program to implement virtual destructor.
#include <iostream>
using namespace std;
// Base Class
class Base
{
public:
Base()
{
cout << "Base Constructor Called" << endl;
}
virtual ~Base() // Virtual Destructor
{
cout << "Base Destructor Called" << endl;
}
};
// Derived Class
class Derived : public Base
{
public:
Derived()
{
cout << "Derived Constructor Called" << endl;
}
~Derived()
{
cout << "Derived Destructor Called" << endl;
}
};
int main()
{
Base *ptr = new Derived();
delete ptr; // Calls both destructors because base destructor is virtual
return 0;
}
19. Create the Person class. Create some objects of this class (by taking
information from the user). Inherit the class Person to create two classes
Teacher and Student class. Maintain the respective information in the classes
and create, display and delete objects of these two classes (Use Runtime
Polymorphism).
#include <iostream>
#include <string>
using namespace std;
// Base Class
class Person
{
protected:
string name;
int age;
public:
virtual void getData()
{
cout << "Enter Name: ";
cin >> name;
cout << "Enter Age: ";
cin >> age;
}
virtual void display()
{
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
virtual ~Person()
{
cout << "Person Object Destroyed" << endl;
}
};
// Derived Class Teacher
class Teacher : public Person
{
private:
string subject;
double salary;
public:
void getData() override
{
Person::getData();
cout << "Enter Subject: ";
cin >> subject;
cout << "Enter Salary: ";
cin >> salary;
}
void display() override
{
cout << "\n--- Teacher Details ---" << endl;
Person::display();
cout << "Subject: " << subject << endl;
cout << "Salary: " << salary << endl;
}
~Teacher()
{
cout << "Teacher Object Destroyed" << endl;
}
};
// Derived Class Student
class Student : public Person
{
private:
int rollNo;
float marks;
public:
void getData() override
{
Person::getData();
cout << "Enter Roll Number: ";
cin >> rollNo;
cout << "Enter Marks: ";
cin >> marks;
}
void display() override
{
cout << "\n--- Student Details ---" << endl;
Person::display();
cout << "Roll Number: " << rollNo << endl;
cout << "Marks: " << marks << endl;
}
~Student()
{
cout << "Student Object Destroyed" << endl;
}
};
int main()
{
Person *p;
// Teacher Object
p = new Teacher();
cout << "\nEnter Teacher Information" << endl;
p->getData();
p->display();
delete p;
// Student Object
p = new Student();
cout << "\nEnter Student Information" << endl;
p->getData();
p->display();
delete p;
return 0;
}
Sample Output
Enter Teacher Information
Enter Name: Ramesh
Enter Age: 40
Enter Subject: Math
Enter Salary: 50000
--- Teacher Details ---
Name: Ramesh
Age: 40
Subject: Math
Salary: 50000
Teacher Object Destroyed
Person Object Destroyed
Enter Student Information
Enter Name: Rahul
Enter Age: 20
Enter Roll Number: 101
Enter Marks: 85
--- Student Details ---
Name: Rahul
Age: 20
Roll Number: 101
Marks: 85
Student Object Destroyed
Person Object Destroyed
Concepts Used
Inheritance: Teacher and Student inherit from Person.
Runtime Polymorphism: Achieved using virtual functions (getData() and
display()) and a base-class pointer (Person *p).
Virtual Destructor: Ensures proper destruction of derived class objects
when deleted through a base-class pointer.
Dynamic Memory Allocation: Objects are created using new and destroyed
using delete.
20. Write a program to Copy the contents of one file to other.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string sourceFile, destFile;
string line;
cout << "Enter source file name: ";
cin >> sourceFile;
cout << "Enter destination file name: ";
cin >> destFile;
// Open source file for reading
ifstream fin(sourceFile);
if (!fin)
{
cout << "Error opening source file!" << endl;
return 1;
}
// Open destination file for writing
ofstream fout(destFile);
if (!fout)
{
cout << "Error opening destination file!" << endl;
return 1;
}
// Copy content line by line
while (getline(fin, line))
{
fout << line << endl;
}
cout << "\nFile copied successfully!" << endl;
// Close files
[Link]();
[Link]();
return 0;
}