Subject: Object Oriented Programming [C++]
Name: ANKIT SUNIL KHARADE
Roll No: 53
Practical No: 1- Write a program in C++ to sort the numbers in an array using separate
functions for read, display, sort and swap.
#include <iostream>
using namespace std;
// Function to read the numbers into the array
void readArray(int arr[], int size) {
cout << "Enter " << size << " numbers:" << endl;
for (int i = 0; i < size; ++i) {
cin >> arr[i];
// Function to display the array
void displayArray(int arr[], int size) {
cout << "Array elements: ";
for (int i = 0; i < size; ++i) {
cout << arr[i] << " ";
cout << endl;
// Function to swap two elements
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
// Function to sort the array using Bubble Sort
void sortArray(int arr[], int size) {
for (int i = 0; i < size - 1; ++i) {
for (int j = 0; j < size - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]); // Swap if elements are in the wrong order
}}}}
int main() {
int size;
// Ask user for the size of the array
cout << "Enter the size of the array: ";
cin >> size;
int arr[size]; // Declare the array
// Call functions
readArray(arr, size); // Read the array
cout << "Before sorting:" << endl;
displayArray(arr, size); // Display the unsorted array
sortArray(arr, size); // Sort the array
cout << "After sorting:" << endl;
displayArray(arr, size); // Display the sorted array
return 0;
Call By value
#include <iostream>
using namespace std;
void callByValue(int num) {
num = num + 10; // Modify the copy of num
cout << "Inside callByValue, num = " << num << endl;
int main() {
int x = 5;
cout << "Before callByValue, x = " << x << endl;
callByValue(x); // Pass by value
cout << "After callByValue, x = " << x << endl; // x is not changed
return 0;
}
Call By reference
#include <iostream>
using namespace std;
void callByReference(int &num) {
num = num + 10; // Modify the original num
cout << "Inside callByReference, num = " << num << endl;
int main() {
int x = 5;
cout << "Before callByReference, x = " << x << endl;
callByReference(x); // Pass by reference
cout << "After callByReference, x = " << x << endl; // x is changed
return 0;
}
Subject: Object Oriented Programming [C++]
Name: ANKIT SUNIL KHARADE
Roll No: 53
Practical No: 2- Write a C++ program that illustrates the concept of Function over
loading.
#include <iostream>
using namespace std;
// Function to add two integers
int add(int a, int b) {
return a + b;}
// Function to add three integers
int add(int a, int b, int c) {
return a + b + c; }
// Function to add two floating-point numbers
float add(float a, float b) {
return a + b; }
int main() {
int x = 10, y = 20, z = 30;
float p = 10.5, q = 20.3;
// Calling the overloaded add function for integers (2 arguments)
cout << "Sum of " << x << " and " << y << " = " << add(x, y) << endl;
// Calling the overloaded add function for integers (3 arguments)
cout << "Sum of " << x << ", " << y << " and " << z << " = " << add(x, y, z) << endl;
// Calling the overloaded add function for floats
cout << "Sum of " << p << " and " << q << " = " << add(p, q) << endl;
return 0;}
Subject: Object Oriented Programming [C++]
Name: ANKIT SUNIL KHARADE
Roll No: 53
Practical No: 3- Write a program in C++ to perform following operations on complex
numbers Add, Subtract, Multiply, Divide, Complex conjugate. Design the class for
complex number representation and the operations to be performed.
#include <iostream>
using namespace std;
class Complex {
private:
float real, imag;
public:
// Constructor to initialize real and imaginary parts
Complex(float r = 0, float i = 0) : real(r), imag(i) {}
// Function to display the complex number
void display() const {
cout << real;
if (imag >= 0) cout << " + " << imag << "i";
else cout << " - " << -imag << "i";
cout << endl;
// Overload the + operator to add two complex numbers
Complex operator + (const Complex &other) {
return Complex(real + [Link], imag + [Link]);
// Overload the - operator to subtract two complex numbers
Complex operator - (const Complex &other) {
return Complex(real - [Link], imag - [Link]);
// Overload the * operator to multiply two complex numbers
Complex operator * (const Complex &other) {
return Complex(real * [Link] - imag * [Link],
real * [Link] + imag * [Link]);
// Overload the / operator to divide two complex numbers
Complex operator / (const Complex &other) {
float denominator = [Link] * [Link] + [Link] * [Link];
float realPart = (real * [Link] + imag * [Link]) / denominator;
float imagPart = (imag * [Link] - real * [Link]) / denominator;
return Complex(realPart, imagPart);
// Function to find the complex conjugate
Complex conjugate() {
return Complex(real, -imag);
};
int main() {
Complex num1(4, 5), num2(2, 3);
cout << "Complex Number 1: ";
[Link]();
cout << "Complex Number 2: ";
[Link]();
// Add the complex numbers
Complex sum = num1 + num2;
cout << "Sum: ";
[Link]();
// Subtract the complex numbers
Complex diff = num1 - num2;
cout << "Difference: ";
[Link]();
// Multiply the complex numbers
Complex product = num1 * num2;
cout << "Product: ";
[Link]();
// Divide the complex numbers
Complex quotient = num1 / num2;
cout << "Quotient: ";
[Link]();
// Complex conjugate of num1
Complex conj1 = [Link]();
cout << "Complex Conjugate of num1: ";
[Link]();
return 0;
}
Subject: Object Oriented Programming [C++]
Name: ANKIT SUNIL KHARADE
Roll No: 53
Practical No: 4- Write a program in C++ to implement Stack. Design the class for
stack and the operations to be performed on stack.
#include <iostream>
using namespace std;
class Stack {
private:
int *arr; // Array to store stack elements
int top; // Index of the top element in the stack
int capacity; // Maximum capacity of the stack
public:
// Constructor to initialize the stack with a given capacity
Stack(int size) {
capacity = size;
arr = new int[capacity]; // Dynamically allocate memory for the stack
top = -1; // Stack is empty initially
// Destructor to free dynamically allocated memory
~Stack() {
delete[] arr;
// Function to check if the stack is empty
bool isEmpty() {
return top == -1;
// Function to check if the stack is full
bool isFull() {
return top == capacity - 1;
// Function to add an element to the stack (push)
void push(int value) {
if (isFull()) {
cout << "Stack Overflow! Unable to push " << value << endl;
} else {
arr[++top] = value; // Increment top and add the value to the stack
cout << value << " pushed to stack." << endl;
// Function to remove the top element from the stack (pop)
int pop() {
if (isEmpty()) {
cout << "Stack Underflow! Unable to pop." << endl;
return -1;
} else {
int poppedValue = arr[top--]; // Return the top value and decrement top
return poppedValue;
// Function to return the top element of the stack (peek)
int peek() {
if (isEmpty()) {
cout << "Stack is empty!" << endl;
return -1;
} else {
return arr[top]; // Return the top element without removing it
// Function to display all the elements in the stack
void display() {
if (isEmpty()) {
cout << "Stack is empty!" << endl;
} else {
cout << "Stack elements: ";
for (int i = 0; i <= top; i++) {
cout << arr[i] << " ";
cout << endl;
};
int main() {
// Create a stack of capacity 5
Stack stack(5);
// Perform operations on the stack
[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link](50);
[Link](60); // This should cause a Stack Overflow
[Link]();
cout << "Top element is: " << [Link]() << endl;
cout << "Popped element: " << [Link]() << endl;
cout << "Popped element: " << [Link]() << endl;
[Link]();
[Link](60);
[Link]();
return 0;
}
Subject: Object Oriented Programming [C++]
Name: ANKIT SUNIL KHARADE
Roll No: 53
Practical No: 5- Write a program in C++ to perform following operations on complex
numbers Add, Subtract, Multiply, Divide. Use operator overloading for these
operations.
#include <iostream>
using namespace std;
class Complex {
private:
float real, imag; // real and imaginary parts
public:
// Constructor to initialize the complex number
Complex(float r = 0, float i = 0) : real(r), imag(i) {}
// Overload the + operator to add two complex numbers
Complex operator + (const Complex &other) {
return Complex(real + [Link], imag + [Link]);
// Overload the - operator to subtract two complex numbers
Complex operator - (const Complex &other) {
return Complex(real - [Link], imag - [Link]);
// Overload the * operator to multiply two complex numbers
Complex operator * (const Complex &other) {
return Complex(real * [Link] - imag * [Link],
real * [Link] + imag * [Link]);
Complex operator / (const Complex &other) {
float denominator = [Link] * [Link] + [Link] * [Link];
float realPart = (real * [Link] + imag * [Link]) / denominator;
float imagPart = (imag * [Link] - real * [Link]) / denominator;
return Complex(realPart, imagPart);
void display() const {
cout << real;
if (imag >= 0) cout << " + " << imag << "i";
else cout << " - " << -imag << "i";
cout << endl;
}};
int main() {
// Create two complex numbers
Complex num1(4, 5), num2(2, 3);
cout << "Complex Number 1: ";
[Link]();
cout << "Complex Number 2: ";
[Link]();
// Add the complex numbers
Complex sum = num1 + num2;
cout << "Sum: ";
[Link]();
// Subtract the complex numbers
Complex diff = num1 - num2;
cout << "Difference: ";
[Link]();
// Multiply the complex numbers
Complex product = num1 * num2;
cout << "Product: ";
[Link]();
// Divide the complex numbers
Complex quotient = num1 / num2;
cout << "Quotient: ";
[Link]();
return 0;
Subject: Object Oriented Programming [C++]
Name: ANKIT SUNIL KHARADE
Roll No: 53
Practical No: 6- Write a program in C++ to Read and Display the information of
Employee using Multiple Inheritance. Use Basic Info and Department Info as a base
class.
#include <iostream>
#include <string>
using namespace std;
// Base class for storing basic employee information
class BasicInfo {
protected:
string name;
int employeeID;
int age;
public:
// Constructor to initialize basic information
BasicInfo(string n, int id, int a) : name(n), employeeID(id), age(a) {}
// Function to display basic information
void displayBasicInfo() const {
cout << "Name: " << name << endl;
cout << "Employee ID: " << employeeID << endl;
cout << "Age: " << age << endl;
};
// Base class for storing department-related information
class DepartmentInfo {
protected:
string departmentName;
string position;
public:
// Constructor to initialize department information
DepartmentInfo(string deptName, string pos) : departmentName(deptName), position(pos) {}
// Function to display department information
void displayDepartmentInfo() const {
cout << "Department: " << departmentName << endl;
cout << "Position: " << position << endl;
};
// Derived class that inherits from both BasicInfo and DepartmentInfo
class Employee : public BasicInfo, public DepartmentInfo {
public:
// Constructor to initialize all information
Employee(string n, int id, int a, string deptName, string pos)
: BasicInfo(n, id, a), DepartmentInfo(deptName, pos) {}
// Function to display all employee information
void displayEmployeeInfo() const {
displayBasicInfo(); // Display basic information from BasicInfo
displayDepartmentInfo(); // Display department information from DepartmentInfo
};
int main() {
// Create an Employee object
Employee emp("John Doe", 101, 30, "Software Development", "Software Engineer");
// Display the employee's information
cout << "Employee Information:\n";
[Link]();
return 0;
}
Subject: Object Oriented Programming [C++]
Name: ANKIT SUNIL KHARADE
Roll No: 53
Practical No: 7- Write a program in C++ to implement string class. Write constructors,
destructor, Accepts function and Display function.
#include <iostream>
#include <cstring>
using namespace std;class MyString {
private:
char* str; // Pointer to hold the dynamically allocated string
public:
// Constructor: Initializes the string to an empty string
MyString() {
str = new char[1]; // Allocating memory for an empty string
str[0] = '\0'; // Null-terminate the string
// Constructor: Initializes the string with a given string
MyString(const char* inputStr) {
str = new char[strlen(inputStr) + 1]; // Allocating memory for the string
strcpy(str, inputStr); // Copy the given string into str
// Destructor: Deallocates the memory used by the string
~MyString() {
delete[] str; // Free the allocated memory
// Function to accept a string from the user
void Accepts() {
char temp[100]; // Temporary array to store the input string
cout << "Enter a string: ";
[Link](temp, 100); // Read the string from user
delete[] str; // Free the previous memory
str = new char[strlen(temp) + 1]; // Allocate memory for the new string
strcpy(str, temp); // Copy the new string into str
// Function to display the string
void Display() const {
cout << "String: " << str << endl;
};
int main() {
// Create a MyString object using default constructor
MyString str1;
[Link](); // Accept input for str1
[Link](); // Display str1
// Create a MyString object using parameterized constructor
MyString str2("Hello, World!");
[Link](); // Display str2
return 0;
}
Subject: Object Oriented Programming [C++]
Name: ANKIT SUNIL KHARADE
Roll No: 53
Practical No: 8- : Write a C++ program that illustrates run time polymorphism by using
virtual functions.
#include <iostream>
using namespace std;
// Base class
class Shape {
public:
// Virtual function to calculate area (will be overridden by derived classes)
virtual void area() {
cout << "Calculating area of shape..." << endl;
};
// Derived class for Circle
class Circle : public Shape {
private:
double radius;
public:
// Constructor to initialize radius
Circle(double r) : radius(r) {}
// Overriding area function for Circle
void area() override {
cout << "Area of Circle: " << 3.14 * radius * radius << endl; }};
// Derived class for Rectangle
class Rectangle : public Shape {
private:
double length, width;
public:
// Constructor to initialize length and width
Rectangle(double l, double w) : length(l), width(w) {}
// Overriding area function for Rectangle
void area() override {
cout << "Area of Rectangle: " << length * width << endl;}};
class Triangle : public Shape {
private:
double base, height;
public:
// Constructor to initialize base and height
Triangle(double b, double h) : base(b), height(h) {}
// Overriding area function for Triangle
void area() override {
cout << "Area of Triangle: " << 0.5 * base * height << endl; }};
int main() {
// Creating objects of derived classes
Circle circle(5);
Rectangle rectangle(4, 6);
Triangle triangle(3, 7);
// Base class pointers to derived class objects
Shape* shapePtr;
// Pointing to Circle and calling area
shapePtr = &circle;
shapePtr->area(); // Calls Circle's area()
// Pointing to Rectangle and calling area
shapePtr = &rectangle;
shapePtr->area(); // Calls Rectangle's area()
// Pointing to Triangle and calling area
shapePtr = ▵
shapePtr->area(); // Calls Triangle's area()
return 0;
}
Subject: Object Oriented Programming [C++]
Name: ANKIT SUNIL KHARADE
Roll No: 53
Practical No: 9- : Write a C++ program which use try and catch for exception handling.
#include <iostream>
#include <stdexcept> // For standard exceptions
using namespace std;
class DivisionByZeroException : public exception {
public:
const char* what() const noexcept override {
return "Error: Division by zero!";
};
int divide(int a, int b) {
if (b == 0) {
throw DivisionByZeroException(); // Throw custom exception if division by zero
return a / b;
int main() {
int x, y;
cout << "Enter two integers: ";
cin >> x >> y;
try {
// Attempt division and handle any exceptions thrown
int result = divide(x, y);
cout << "Result: " << result << endl;
} catch (const DivisionByZeroException& e) {
// Catching custom exception for division by zero
cout << [Link]() << endl;
} catch (const exception& e) {
// Catching any other standard exceptions
cout << "An error occurred: " << [Link]() << endl;
return 0;