Experiment
Objective Tasks Expected Output
No.
Define a class Student with
Display student details for
private data members (name,
Introduction to 2 objects, e.g., Name:
roll_no, marks) and public
1 Classes and Alice, Roll: 101, Marks:
member functions to input and
Objects 85 Name: Bob, Roll: 102,
display details. Create 2 objects
Marks: 92
and demonstrate object creation.
Extend the Student class to
Constructor messages on
include parameterized and default
creation; destructor
Constructors and constructors. Add a destructor to
2 messages on program
Destructors print a message when objects are
exit; display details for 3
destroyed. Create an array of 3
students.
objects.
Create a class BankAccount with
Input: Deposit 1000,
private members (account_no,
Withdraw 500 → Output:
Encapsulation and balance) and public methods
3 Balance = 500 (with
Access Specifiers (deposit, withdraw, getBalance).
validation for insufficient
Use getter/setter functions to
funds).
enforce encapsulation.
Design a base class Vehicle
(brand, model) and derived class Base: Vehicle - Toyota
Inheritance (Single Car inheriting publicly (add Camry Derived: Car -
4
Inheritance) fuel_type, mileage). Demonstrate Toyota Camry, Petrol, 15
method overriding for a kmpl
display() function.
Create base classes Shape and
Color, derive Rectangle from
Inheritance Area of ColoredRectangle
both (multiple), and further derive
5 (Multiple and (width=5, height=3): 15
ColoredRectangle from
Multilevel) sq units, Color: Red
Rectangle (multilevel).
Implement area calculation.
In a class MathOperations,
Polymorphism: overload a function add() to add(5, 3) → 8 add(5.5,
6 Function handle int, float, and string 2.3) → 7.8 add("Hello", "
Overloading concatenation. Call with different World") → Hello World
data types.
Create a base class Animal with
Polymorphism: Animal* ptr = new Dog();
virtual sound() method. Derive
Virtual Functions ptr->sound(); → Woof!
7 Dog and Cat classes overriding it.
and Runtime Animal* ptr = new Cat();
Use base pointer to call derived
Polymorphism ptr->sound(); → Meow!
methods.
Experiment
Objective Tasks Expected Output
No.
Overload the + operator in a class
Operator Complex (real, imag parts) to add Complex(3,4) +
8
Overloading two complex numbers. Also Complex(1,2) → (4,6)
overload << for output streaming.
Define a class Box (length, width,
height). Declare a friend function Volume: 100 (for 5x4x5
Friend Functions
9 to calculate volume and a friend box) Room area using
and Classes
class Room to access Box Box dims: 20
dimensions for area calculation.
Create a template class Stack<T>
Push 5 elements → Pop
Templates and with push/pop operations. Add
all: 5 4 3 2 1 (ints)
10 Exception try-catch for stack
Exception: Stack empty
Handling in OOP overflow/underflow exceptions.
on pop!
Test with int and char types.
Experiment 1: Introduction to Classes and Objects
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
int roll_no;
float marks;
public:
void input() {
cout << "Enter name: ";
cin >> name;
cout << "Enter roll no: ";
cin >> roll_no;
cout << "Enter marks: ";
cin >> marks;
void display() {
cout << "Name: " << name << ", Roll: " << roll_no << ", Marks: " << marks << endl;
};
int main() {
Student s1, s2; // Object creation
cout << "Enter details for Student 1:" << endl;
[Link]();
cout << "Enter details for Student 2:" << endl;
[Link]();
cout << "\nStudent Details:" << endl;
[Link]();
[Link]();
return 0;
}
Experiment 2: Constructors and Destructors
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
int roll_no;
float marks;
public:
// Default constructor
Student() {
name = "Unknown";
roll_no = 0;
marks = 0.0;
cout << "Default constructor called" << endl;
// Parameterized constructor
Student(string n, int r, float m) : name(n), roll_no(r), marks(m) {
cout << "Parameterized constructor called for " << name << endl;
void display() {
cout << "Name: " << name << ", Roll: " << roll_no << ", Marks: " << marks << endl;
// Destructor
~Student() {
cout << "Destructor called for " << name << endl;
};
int main() {
Student s1("Alice", 101, 85.0); // Parameterized
Student s2; // Default
Student arr[3] = {Student("Bob", 102, 92.0), Student("Charlie", 103, 78.0), Student()}; // Array of 3
cout << "\nStudent Details:" << endl;
[Link]();
[Link]();
for(int i = 0; i < 3; i++) {
arr[i].display();
return 0; // Destructors called here
}
Experiment 3: Encapsulation and Access Specifiers
#include <iostream>
using namespace std;
class BankAccount {
private:
long long account_no;
double balance;
public:
BankAccount(long long acc) : account_no(acc), balance(0.0) {}
// Getter
double getBalance() const {
return balance;
// Setter for deposit
void deposit(double amount) {
if (amount > 0) {
balance += amount;
cout << "Deposited: " << amount << endl;
} else {
cout << "Invalid deposit amount" << endl;
// Setter for withdraw
void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
cout << "Withdrew: " << amount << endl;
} else {
cout << "Insufficient funds or invalid amount" << endl;
void display() const {
cout << "Account No: " << account_no << ", Balance: " << balance << endl;
};
int main() {
BankAccount acc(1234567890);
[Link](1000);
[Link](500);
[Link]();
return 0;
}
Experiment 4: Inheritance (Single Inheritance)
#include <iostream>
#include <string>
using namespace std;
class Vehicle {
protected:
string brand, model;
public:
Vehicle(string b, string m) : brand(b), model(m) {}
virtual void display() const { // Virtual for overriding
cout << "Vehicle - " << brand << " " << model << endl;
};
class Car : public Vehicle {
private:
string fuel_type;
double mileage;
public:
Car(string b, string m, string f, double mi) : Vehicle(b, m), fuel_type(f), mileage(mi) {}
void display() const override { // Overriding
cout << "Car - " << brand << " " << model << ", " << fuel_type << ", " << mileage << " kmpl" <<
endl;
};
int main() {
Vehicle v("Toyota", "Camry");
Car c("Toyota", "Camry", "Petrol", 15.0);
cout << "Base: " << endl;
[Link]();
cout << "Derived: " << endl;
[Link]();
return 0;
}
Experiment 5: Inheritance (Multiple and Multilevel)
#include <iostream>
#include <string>
using namespace std;
class Shape {
protected:
double width, height;
public:
Shape(double w, double h) : width(w), height(h) {}
double area() const {
return width * height;
};
class Color {
protected:
string color;
public:
Color(string c) : color(c) {}
};
class Rectangle : public Shape, public Color { // Multiple inheritance
public:
Rectangle(double w, double h, string c) : Shape(w, h), Color(c) {}
void display() const {
cout << "Rectangle Area: " << area() << " sq units" << endl;
}
};
class ColoredRectangle : public Rectangle { // Multilevel inheritance
public:
ColoredRectangle(double w, double h, string c) : Rectangle(w, h, c) {}
void display() const {
Rectangle::display();
cout << "Color: " << color << endl;
};
int main() {
ColoredRectangle cr(5.0, 3.0, "Red");
[Link]();
return 0;
}
Experiment 6: Polymorphism: Function Overloading
#include <iostream>
#include <string>
using namespace std;
class MathOperations {
public:
int add(int a, int b) {
return a + b;
double add(double a, double b) {
return a + b;
string add(string a, string b) {
return a + b;
};
int main() {
MathOperations math;
cout << "add(5, 3) → " << [Link](5, 3) << endl;
cout << "add(5.5, 2.3) → " << [Link](5.5, 2.3) << endl;
cout << "add(\"Hello\", \" World\") → " << [Link]("Hello", " World") << endl;
return 0;
Experiment 7: Polymorphism: Virtual Functions and Runtime Polymorphism
#include <iostream>
using namespace std;
class Animal {
public:
virtual void sound() const { // Virtual function
cout << "Animal sound" << endl;
virtual ~Animal() {} // Virtual destructor for proper cleanup
};
class Dog : public Animal {
public:
void sound() const override {
cout << "Woof!" << endl;
};
class Cat : public Animal {
public:
void sound() const override {
cout << "Meow!" << endl;
};
int main() {
Animal* ptr1 = new Dog();
Animal* ptr2 = new Cat();
ptr1->sound();
ptr2->sound();
delete ptr1;
delete ptr2;
return 0;
Experiment 8: Operator Overloading
#include <iostream>
using namespace std;
class Complex {
private:
double real, imag;
public:
Complex(double r = 0, double i = 0) : real(r), imag(i) {}
Complex operator+(const Complex& other) const { // Overload +
return Complex(real + [Link], imag + [Link]);
// Friend overload for <<
friend ostream& operator<<(ostream& os, const Complex& c) {
os << "(" << [Link] << "," << [Link] << ")";
return os;
};
int main() {
Complex c1(3, 4), c2(1, 2);
Complex c3 = c1 + c2;
cout << "Complex(3,4) + Complex(1,2) → " << c3 << endl;
return 0;
Experiment 9: Friend Functions and Classes
#include <iostream>
using namespace std;
class Box {
private:
double length, width, height;
public:
Box(double l, double w, double h) : length(l), width(w), height(h) {}
// Friend function
friend double volume(const Box& b);
// Friend class
friend class Room;
};
double volume(const Box& b) { // Friend function accesses private
return [Link] * [Link] * [Link];
class Room {
public:
double calculateFloorArea(const Box& b) { // Friend class accesses private
return [Link] * [Link];
};
int main() {
Box box(5, 4, 5);
cout << "Volume: " << volume(box) << endl;
Room room;
cout << "Room area using Box dims: " << [Link](box) << endl;
return 0;
Experiment 10: Templates and Exception Handling in OOP
#include <iostream>
#include <stdexcept>
using namespace std;
template <class T>
class Stack {
private:
T arr[5]; // Fixed size for simplicity
int top;
public:
Stack() : top(-1) {}
void push(T val) {
if (top < 4) {
arr[++top] = val;
} else {
throw runtime_error("Stack overflow!");
T pop() {
if (top >= 0) {
return arr[top--];
} else {
throw runtime_error("Stack empty!");
};
int main() {
try {
// Test with int
Stack<int> intStack;
for (int i = 1; i <= 5; i++) [Link](i);
cout << "Push 5 elements → Pop all: ";
for (int i = 0; i < 5; i++) {
cout << [Link]() << " ";
cout << endl;
// Test pop on empty
[Link]();
} catch (const exception& e) {
cout << "Exception: " << [Link]() << endl;
// Test with char (brief)
Stack<char> charStack;
[Link]('A');
cout << "Pop char: " << [Link]() << endl;
return 0;