Object oriented Programming (OOPS) practical file
Object oriented Programming (OOPS) practical file
Aim : Write a C++ program that performs basic mathema cal opera ons using
a switch statement.
Source code :
#include <iostream>
using namespace std;
int main() {
double num1, num2;
char opera on;
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
cout << "Enter the opera on (+, -, *, /): ";
cin >> opera on;
switch (opera on) {
case '+':
cout << "Result: " << num1 + num2 << endl;
break;
case '-':
cout << "Result: " << num1 - num2 << endl;
break;
case '*':
cout << "Result: " << num1 * num2 << endl;
break;
case '/':
if (num2 != 0) {
cout << "Result: " << num1 / num2 << endl;
} else {
cout << "Error: Division by zero is not allowed." << endl; }
break;
default:
cout << "Error: Invalid opera on entered. Please use +, -, *, or /." << endl;
}
return 0;
}
Output:
Experiment – 2a
Aim : Write a program to calculate the power of a number using the standard
power func on
Source code :
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double base, exponent, result;
cout << "Enter the base: ";
cin >> base;
cout << "Enter the exponent: ";
cin >> exponent;
result = pow(base, exponent);
cout << base << " raised to the power of " << exponent << " is: " << result << endl;
return 0;
}
Output:
Experiment – 2b
Aim : Write a program to calculate the power of a number without using the
standard power func on.
Source code :
#include <iostream>
using namespace std;
int main() {
double base, result = 1;
int exponent;
cout << "Enter the base: ";
cin >> base;
cout << "Enter the exponent: ";
cin >> exponent;
if (exponent >= 0) {
for (int i = 0; i < exponent; ++i) {
result *= base;}
} else {
for (int i = 0; i < -exponent; ++i) {
result *= base; }
result = 1 / result;
}
cout << base << " raised to the power of " << exponent << " is: " << result << endl;
return 0;
}
Output:
Experiment – 3a
Aim : Write a program to store and display student informa on using a
structure.
Source code :
#include <iostream>
using namespace std;
struct Student {
string name;
int rollNumber;
float marks;
};
int main() {
Student student;
cout << "Enter student name: ";
getline(cin, student.name);
cout << "Enter roll number: ";
cin >> student.rollNumber;
cout << "Enter marks: ";
cin >> student.marks;
cout << "\nStudent Informa on:" << endl;
cout << "Name: " << student.name << endl;
cout << "Roll Number: " << student.rollNumber << endl;
cout << "Marks: " << student.marks << endl;
return 0;
}
Output:
Experiment – 3b
Aim : Write a program to store and display student informa on using a class.
Source code :
#include <iostream>
#include <string>
using namespace std;
class Student {
string name;
int rollNumber;
float marks;
public:
void setData(string n, int r, float m) {
name = n;
rollNumber = r;
marks = m;
}
void displayData() {
cout << "Name: " << name << "\nRoll Number: " << rollNumber << "\nMarks: " << marks
<< endl;
}
};
int main() {
Student s;
string name;
int rollNumber;
float marks;
cout << "Enter name: ";
getline(cin, name);
cout << "Enter roll number: ";
cin >> rollNumber;
cout << "Enter marks: ";
cin >> marks;
s.setData(name, rollNumber, marks);
s.displayData();
return 0;
}
Output:
Experiment – 4a
Aim : Illustrate the use of a default constructor in C++.
Source code :
#include <iostream>
class Student {
string name;
int rollNumber;
float marks;
public:
Student() {
name = "Unknown";
rollNumber = 0;
marks = 0.0;
void displayData() {
cout << "Name: " << name << "\nRoll Number: " << rollNumber << "\nMarks: " << marks << endl;
};
int main() {
Student s;
s.displayData();
return 0;
Output:
Experiment – 4b
Aim : Illustrate the use of a parameterized constructor in C++.
Source code :
#include <iostream>
class Student {
string name;
int rollNumber;
float marks;
public:
name = n;
rollNumber = r;
marks = m;
void displayData() {
cout << "Name: " << name << "\nRoll Number: " << rollNumber << "\nMarks: " << marks << endl;
};
int main() {
s.displayData();
return 0;
Output:
Experiment – 4c
Aim : Illustrate the use of a copy constructor in C++.
Source code :
#include <iostream>
class Student {
string name;
int rollNumber;
float marks;
public:
name = n;
rollNumber = r;
marks = m;
Student(const Student& s) {
name = s.name;
rollNumber = s.rollNumber;
marks = s.marks;
void displayData() {
cout << "Name: " << name << "\nRoll Number: " << rollNumber << "\nMarks: " << marks << endl;
};
int main() {
Student s2 = s1;
s1.displayData();
s2.displayData();
return 0;
Output:
Experiment – 5
Aim : Write a C++ program to demonstrate func on overloading.
Source code :
#include <iostream>
class Calculator {
public:
return a + b;
return a + b;
return a + b + c;
};
int main() {
Calculator calc;
cout << "Addi on of two integers: " << calc.add(15, 20) << endl;
cout << "Addi on of two floats: " << calc.add(5.5f, 3.5f) << endl;
cout << "Addi on of three integers: " << calc.add(1, 2, 4) << endl;
return 0;
Output:
Experiment – 6
Aim : Write a C++ program to demonstrate constructor overloading.
Source code :
#include <iostream>
#include <string>
class Student {
string name;
int rollNumber;
float marks;
public:
Student() {
name = "Unknown";
rollNumber = 0;
marks = 0.0;
Student(string n) {
name = n;
rollNumber = 0;
marks = 0.0;
name = n;
rollNumber = r;
marks = m;
void displayData() {
cout << "Name: " << name << "\nRoll Number: " << rollNumber << "\nMarks: " << marks << endl;
};
int main() {
Student s1;
Student s2("Vikas");
s1.displayData();
s2.displayData();
s3.displayData();
return 0;
Output:
Experiment – 7
Aim : Write a program to calculate the area of a square or rectangle using a
class.
Given condi ons: If only the length is provided, the program should calculate the area of a
square; if both length and breadth are provided, it should calculate the area of the rectangle.
Source code :
#include <iostream>
class Shape {
public:
Shape(int l) {
length = l;
breadth = l;
Shape(int l, int b) {
length = l;
breadth = b;
int calculateArea() {
};
int main() {
Shape square(5);
cout << "Area of the square: " << square.calculateArea() << endl;
cout << "Area of the rectangle: " << rectangle.calculateArea() << endl;
return 0;
}
Output:
Experiment – 8
Aim : Write a program to overload operators (`+`, `-`, `*`, `/`, `+=`) for a class
represen ng complex numbers.
Source code :
#include <iostream>
class Complex {
public:
}
Complex& operator += (const Complex& other) {
real += other.real;
imag += other.imag;
return *this;
void display() {
if (imag < 0)
cout << real << " - " << -imag << "i" << endl;
else
cout << real << " + " << imag << "i" << endl;
};
int main() {
Complex c3 = c1 + c2;
c3.display();
Complex c4 = c1 - c2;
c4.display();
Complex c5 = c1 * c2;
c5.display();
Complex c6 = c1 / c2;
c6.display();
c1 += c2;
c1.display();
return 0;}
Output:
Experiment – 9
Aim : Create a class `Ra onal` that represents a numerical value using a numerator and
denominator. Write a program to overload the `+` and `-` operators for this class.
Source code :
#include <iostream>
class Ra onal {
public:
if (denom == 0) {
denominator = 1;
}
void display() {
};
int main() {
Ra onal r3 = r1 + r2;
r3.display();
Ra onal r4 = r1 - r2;
r4.display();
return 0;
Output:
Experiment – 10
Aim : Write a program to overload operators `+` and `==` for a class represen ng
strings.
Source code :
#include <iostream>
#include <cstring>
class String {
char* str;
public:
String() : str(nullptr) {}
String(const char* s) {
strcpy(str, s);
strcpy(str, other.str);
~String() {
delete[] str;
strcpy(result, str);
strcat(result, other.str);
return String(result);
}
bool operator == (const String& other) {
};
int main() {
String s4 = s1 + s2;
s4.display();
if (s1 == s3)
else
return 0;
Output:
Experiment – 11a
Aim : Single level Inheritance (Design a base class Employee with a ributes
name and salary. Derive a class Manager from Employee that adds an a ribute
department (type: string). Provide a toString method in Manager that returns
the manager's name, department, and salary.)
Source code :
#include <iostream>
#include <string>
class Employee {
protected:
string name;
double salary;
public:
};
private:
string department;
public:
string toString() {
return "Manager Name: " + name + "\nDepartment: " + department + "\nSalary: " +
to_string(salary);
};
int main() {
Manager mgr("Ankit Sharma", 75000.50, "Marke ng");
return 0;
Output:
Experiment – 11b
Aim : Mul -level Inheritance (Create a base class Employee with a ributes
name and salary. Derive a class Manager from Employee with an addi onal
department a ribute. Then, create a further derived class Execu ve that
inherits from Manager, overriding the toString method to prepend "Execu ve"
to the informa on provided by Manager's toString.)
Source code :
#include <iostream>
#include <string>
class Employee {
protected:
string name;
double salary;
public:
virtual ~Employee() {}
};
string department;
public:
string toString() {
return "Name: " + name + ", Department: " + department + ", Salary: " + to_string(salary);
};
int main() {
Manager mgr("Vikas Yadav", 80000, "IT");
return 0;
Output:
Experiment – 11c
Aim : Mul ple Inheritance (Design a class Employee with a ributes name and salary.
Create another class Manager with an a ribute department. Then, create a class Execu ve
that inherits from both Employee and Manager. The Execu ve class should override the
toString method to prepend "Execu ve" to the informa on displayed by combining Employee
and Manager details.)
Source code :
#include <iostream>
#include <string>
class Employee {
protected:
string name;
double salary;
public:
virtual ~Employee() {}
};
protected:
string department;
public:
return "Name: " + name + ", Department: " + department + ", Salary: " + to_string(salary);
};
class Execu ve : public Manager {
public:
};
int main() {
return 0;
Output:
Experiment – 12
Aim : Write a program to implement virtual func on.
Source code :
#include <iostream>
#include <cmath>
class Shape {
public:
return 0.0;
};
double radius;
public:
Circle(double r) : radius(r) {}
};
public:
};
int main() {
delete shape1;
delete shape2;
return 0;
Output:
Experiment – 13a
Aim : Create two classes, D1 and D2 which store the value of distances in both
meter and cen meter. Write a program that can read values for the class objects
and add one object of D1 with another object of D2. Use a friend func on to
carry out the addi on opera on.
Source code :
#include <iostream>
class D1 {
int meters;
public:
void input() {
cout << "Enter distance for D1 (meters and cen meters): ";
};
class D2 {
int meters;
public:
void input() {
cout << "Enter distance for D2 (meters and cen meters): ";
}
friend void addDistances(D1, D2); // Friend func on declara on
};
cout << "Total Distance: " << totalMeters << " meters and " << totalCen meters << " cen meters" <<
endl;
int main() {
D1 distance1;
D2 distance2;
distance1.input();
distance2.input();
return 0;
Output:
Experiment – 13b
Aim : Create two classes DM and DB which store the value of distances. DM
stores distances in metres and cen meters and DB in feet and inches. Write a
program that can read values for the class objects and add one object of DM
with another object of DB. Use a friend func on to carry out the addi on
opera on. The object that stores the results maybe DM object or DB object.
depending on the units in which the results are required. The display should be
in the format of feet and inches or metres and cen metres depending on object
on display.
Source code :
#include <iostream>
class DB;
class DM {
int meters;
public:
void input() {
cout << "Enter distance for DM (meters and cen meters): ";
void display() {
cout << meters << " meters and " << cen meters << " cen meters" << endl;
};
class DB {
int feet;
int inches;
public:
void input() {
void display() {
cout << feet << " feet and " << inches << " inches" << endl;
};
int totalCen meters = d1.meters * 100 + d1.cen meters + d2.feet * 30.48 + d2.inches * 2.54;
int main() {
DM distance1;
DB distance2;
DM resultDM;
DB resultDB;
distance1.input();
distance2.input();
resultDM.display();
resultDB.display();
return 0;
Output:
Experiment – 14
Aim : Design a class `TollBooth` with the following a ributes and methods: Data
members: `unsigned int` to hold the total number of cars, and `double` to hold
the total amount of money collected. Constructor to ini alize both members to
0. `payingCar()` member func on to increment the car count and add $0.50 to
the total cash. `nopayCar()` member func on to increment only the car count.
Source code :
#include <iostream>
class TollBooth {
private:
double totalMoney;
public:
void payingCar() {
carCount++;
totalMoney += 0.50;
void nopayCar() {
carCount++;
cout << "Total Money Collected: $" << totalMoney << endl;
};
int main() {
TollBooth booth;
booth.payingCar();
booth.payingCar();
booth.nopayCar();
booth.payingCar();
booth.display();
return 0;
Output:
Experiment – 15
Aim : Write a program to create a class template to implement stack opera ons.
Source code :
#include <iostream>
class Stack {
T* arr;
int top;
int capacity;
public:
Stack(int size) {
capacity = size;
top = -1;
~Stack() {
delete[] arr;
bool isFull() {
bool isEmpty() {
}
void push(T value) {
if (isFull()) {
return;
arr[++top] = value;
T pop() {
if (isEmpty()) {
return arr[top--];
T peek() {
if (isEmpty()) {
return arr[top];
void display() {
if (isEmpty()) {
return;
};
int main() {
Stack<int> stackInt(5);
stackInt.push(10);
stackInt.push(20);
stackInt.push(30);
stackInt.display();
stackInt.display();
Stack<double> stackDouble(3);
stackDouble.push(1.1);
stackDouble.push(2.2);
stackDouble.push(3.3);
stackDouble.display();
stackDouble.display();
return 0;
Output:
Experiment – 16
Aim : Write a program to demonstrate excep on handling.
Source code :
#include <iostream>
#include <stdexcept>
public:
};
public:
};
if (den == 0) {
if (value < 0) {
throw Nega veValueExcep on(); // Throw excep on for nega ve values
int main() {
try {
checkValue(num);
checkValue(den);
return 0;
}
Output: