List of programs in OOPs with C++
1. Considering an EMPLOYEE class contains the following data members: Employee number,
Employee name, Basic, DA, IT, Net Salary, and print data members using appropriate function.
#include <iostream>
using namespace std;
class Employee {
int empNo;
string empName;
float basic, DA, IT, netSalary;
public:
void setData(int no, string name, float basicSalary, float da, float it) {
empNo = no;
empName = name;
basic = basicSalary;
DA = da;
IT = it;
netSalary = basic + DA - IT;
}
void printData() {
cout << "Employee Number: " << empNo << endl;
cout << "Employee Name: " << empName << endl;
cout << "Basic Salary: " << basic << endl;
cout << "DA: " << DA << endl;
cout << "IT: " << IT << endl;
cout << "Net Salary: " << netSalary << endl;
}
};
int main() {
Employee emp;
emp.setData(1, "John Doe", 50000, 10000, 5000);
emp.printData();
return 0;
}
2. Create a C++ program that demonstrates constructor overloading by assuming the desired
parameters.
#include <iostream>
using namespace std;
class Example {
int a, b;
public:
Example() { // Default constructor
a = 0;
b = 0;
}
Example(int x) { // Parameterized constructor with one argument
a = x;
b = 0;
}
Example(int x, int y) { // Parameterized constructor with two arguments
a = x;
b = y;
}
void display() {
cout << "a = " << a << ", b = " << b << endl;
}
};
int main() {
Example e1; // Calls default constructor
Example e2(10); // Calls constructor with one parameter
Example e3(10, 20); // Calls constructor with two parameters
e1.display();
e2.display();
e3.display();
return 0;
3. Write a C++ program for creating multilevel inheritance.
#include <iostream>
using namespace std;
class A {
public:
void displayA() {
cout << "This is class A" << endl;
}
};
class B : public A {
public:
void displayB() {
cout << "This is class B, derived from A" << endl;
}
};
class C : public B {
public:
void displayC() {
cout << "This is class C, derived from B" << endl;
}
};
int main() {
C obj;
obj.displayA();
obj.displayB();
obj.displayC();
return 0;
4. Create a C++ program that displays the Name, Roll number, and Grades of three Students who
appeared in the examination. Create an array of class objects. Read and display the array contents.
#include <iostream>
using namespace std;
class Student {
string name;
int rollNo;
char grade;
public:
void setData(string n, int r, char g) {
name = n;
rollNo = r;
grade = g;
}
void displayData() {
cout << "Name: " << name << ", Roll No: " << rollNo << ", Grade: " <<
grade << endl;
}
};
int main() {
Student students[3];
students[0].setData("Alice", 101, 'A');
students[1].setData("Bob", 102, 'B');
students[2].setData("Charlie", 103, 'A');
for (int i = 0; i < 3; i++) {
students[i].displayData();
}
return 0;
}
5. Create the class Shape in C++ and overload the function to return the perimeters of the different
shapes.
#include <iostream>
using namespace std;
class Shape {
public:
double perimeter(int side) {
return 4 * side; // Square
}
double perimeter(int length, int width) {
return 2 * (length + width); // Rectangle
}
double perimeter(double radius) {
return 2 * 3.1416 * radius; // Circle
}
};
int main() {
Shape s;
cout << "Perimeter of square: " << s.perimeter(5) << endl;
cout << "Perimeter of rectangle: " << s.perimeter(10, 20) << endl;
cout << "Perimeter of circle: " << s.perimeter(7.5) << endl;
return 0;
}
6. Create a C++ program that illustrates the use of a Constructor with a default argument.
#include <iostream>
using namespace std;
class Example {
int a;
public:
Example(int x = 0) { // Default argument in constructor
a = x;
}
void display() {
cout << "Value of a: " << a << endl;
}
};
int main() {
Example e1; // Uses default argument
Example e2(100); // Passes an argument
e1.display();
e2.display();
return 0;
}
7. Create a C++ program to implement an Account Class with member functions to Compute
Interest, Show Balance, Withdraw amount, and Deposit amount from the Account.
#include <iostream>
using namespace std;
class Account {
float balance;
public:
Account(float initialBalance) {
balance = initialBalance;
}
void deposit(float amount) {
balance += amount;
cout << "Deposited: " << amount << endl;
}
void withdraw(float amount) {
if (amount <= balance) {
balance -= amount;
cout << "Withdrawn: " << amount << endl;
} else {
cout << "Insufficient funds!" << endl;
}
}
void computeInterest(float rate) {
balance += balance * rate / 100;
}
void showBalance() {
cout << "Current Balance: " << balance << endl;
}
};
int main() {
Account acc(1000);
acc.deposit(500);
acc.withdraw(300);
acc.computeInterest(5);
acc.showBalance();
return 0;
}
8. Create a C++ program to demonstrate Virtual functions.
#include <iostream>
using namespace std;
class Base {
public:
virtual void show() {
cout << "Base class show function" << endl;
}
void display() {
cout << "Base class display function" << endl;
}
};
class Derived : public Base {
public:
void show() {
cout << "Derived class show function" << endl;
}
void display() {
cout << "Derived class display function" << endl;
}
};
int main() {
Base *bptr;
Derived d;
bptr = &d;
bptr->show(); // Calls Derived's show function due to virtual
bptr->display(); // Calls Base's display function (not virtual)
return 0;
}
9. Create a C++ program to demonstrate the Friend function.
#include <iostream>
using namespace std;
class B;
class A {
int a;
public:
A(int x) : a(x) {}
friend void add(A, B); // Friend function declaration
};
class B {
int b;
public:
B(int y) : b(y) {}
friend void add(A, B);
};
void add(A objA, B objB) {
cout << "Sum of A and B: " << objA.a + objB.b << endl;
}
int main() {
A objA(10);
B objB(20);
add(objA, objB); // Calls friend function
return 0;
10. Write a program in C++ to Create a file and add contents to it.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream file;
file.open("example.txt");
if (file.is_open()) {
file << "This is a sample file content.\n";
file << "C++ file handling example.\n";
file.close();
cout << "File created and contents added successfully." << endl;
} else {
cout << "File could not be opened." << endl;
}
return 0;
}