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>
#include <string>
class EMPLOYEE {
private:
int employeeNumber;
std::string employeeName;
double basic;
double da;
double it;
double netSalary;
public:
EMPLOYEE(int number, std::string name, double basicSalary)
: employeeNumber(number), employeeName(name), basic(basicSalary) {
da = 0.5 * basic; // Assuming DA is 50% of basic
it = 0.1 * (basic + da); // Assuming IT is 10% of (basic + DA)
netSalary = basic + da - it;
}
void printDetails() {
std::cout << "Employee Number: " << employeeNumber << std::endl;
std::cout << "Employee Name: " << employeeName << std::endl;
std::cout << "Basic Salary: " << basic << std::endl;
std::cout << "DA: " << da << std::endl;
std::cout << "IT: " << it << std::endl;
std::cout << "Net Salary: " << netSalary << std::endl;
}
};
int main() {
EMPLOYEE emp1(1001, "John Doe", 50000);
emp1.printDetails();
return 0;
}
2. Create a C++ program that demonstrates constructor overloading by assuming the desired
parameters.
#include <iostream>
#include <string>
class Box {
private:
double length;
double width;
double height;
public:
// Constructor with no parameters
Box() : length(1), width(1), height(1) {}
// Constructor with one parameter
Box(double size) : length(size), width(size), height(size) {}
// Constructor with three parameters
Box(double l, double w, double h) : length(l), width(w), height(h) {}
double volume() {
return length * width * height;
}
void display() {
std::cout << "Box: " << length << " x " << width << " x " << height <<
std::endl;
std::cout << "Volume: " << volume() << std::endl;
}
};
3. Write a C++ program for creating multilevel inheritance.
#include <iostream>
#include <string>
class Animal {
protected:
std::string name;
public:
Animal(std::string n) : name(n) {}
void eat() { std::cout << name << " is eating." << std::endl; }
};
class Mammal : public Animal {
public:
Mammal(std::string n) : Animal(n) {}
void breathe() { std::cout << name << " is breathing." << std::endl; }
};
class Dog : public Mammal {
public:
Dog(std::string n) : Mammal(n) {}
void bark() { std::cout << name << " is barking." << std::endl; }
};
int main() {
Dog myDog("Buddy");
myDog.eat(); // Inherited from Animal
myDog.breathe(); // Inherited from Mammal
myDog.bark(); // Dog's own method
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>
#include <string>
class Student {
private:
std::string name;
int rollNumber;
char grade;
public:
void readData() {
std::cout << "Enter name: ";
std::getline(std::cin, name);
std::cout << "Enter roll number: ";
std::cin >> rollNumber;
std::cout << "Enter grade: ";
std::cin >> grade;
std::cin.ignore(); // Clear the newline from the input buffer
}
void displayData() {
std::cout << "Name: " << name << ", Roll Number: " << rollNumber << ", Grade: "
<< grade << std::endl;
}
};
int main() {
const int numStudents = 3;
Student students[numStudents];
std::cout << "Enter details for " << numStudents << " students:" << std::endl;
for (int i = 0; i < numStudents; ++i) {
std::cout << "\nStudent " << i + 1 << ":" << std::endl;
students[i].readData();
}
std::cout << "\nStudent Details:" << std::endl;
for (int i = 0; i < numStudents; ++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>
#include <cmath>
class Shape {
public:
double perimeter(double side) {
return 4 * side; // Square
}
double perimeter(double length, double width) {
return 2 * (length + width); // Rectangle
}
double perimeter(double a, double b, double c) {
return a + b + c; // Triangle
}
double perimeter(double radius, bool isCircle) {
if (isCircle) {
return 2 * M_PI * radius; // Circle
}
return 0; // Invalid
}
};
int main() {
Shape shape;
std::cout << "Square perimeter: " << shape.perimeter(5) << std::endl;
std::cout << "Rectangle perimeter: " << shape.perimeter(4, 6) << std::endl;
std::cout << "Triangle perimeter: " << shape.perimeter(3, 4, 5) << std::endl;
std::cout << "Circle perimeter: " << shape.perimeter(7, true) << std::endl;
return 0;
}
6. Create a C++ program that illustrates the use of a Constructor with a default argument.
#include <iostream>
#include <string>
class Car {
private:
std::string brand;
std::string model;
int year;
public:
// Constructor with default argument for year
Car(std::string b, std::string m, int y = 2023) : brand(b), model(m), year(y) {}
void display() {
std::cout << year << " " << brand << " " << model << std::endl;
}
};
int main() {
Car car1("Toyota", "Corolla"); // Uses default year 2023
Car car2("Honda", "Civic", 2022); // Specifies the year
std::cout << "Car 1: ";
car1.display();
std::cout << "Car 2: ";
car2.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>
#include <string>
class Account {
private:
std::string accountNumber;
double balance;
double interestRate;
public:
Account(std::string accNum, double initialBalance, double rate)
: accountNumber(accNum), balance(initialBalance), interestRate(rate) {}
void computeInterest() {
double interest = balance * (interestRate / 100);
balance += interest;
std::cout << "Interest added: $" << interest << std::endl;
}
void showBalance() {
std::cout << "Account " << accountNumber << " balance: $" << balance <<
std::endl;
}
void withdraw(double amount) {
if (amount > balance) {
std::cout << "Insufficient funds!" << std::endl;
} else {
balance -= amount;
std::cout << "Withdrawn: $" << amount << std::endl;
}
}
void deposit(double amount) {
balance += amount;
std::cout << "Deposited: $" << amount << std::endl;
}
};
int main() {
Account acc("123456", 1000, 2.5); // Account number, initial balance, interest
rate
acc.showBalance();
acc.deposit(500);
acc.showBalance();
acc.withdraw(200);
acc.showBalance();
acc.computeInterest();
acc.showBalance();
return 0;
}
8. Create a C++ program to demonstrate Virtual functions.
#include <iostream>
class Animal {
public:
virtual void makeSound() {
std::cout << "The animal makes a sound" << std::endl;
}
};
class Dog : public Animal {
public:
void makeSound() override {
std::cout << "The dog barks" << std::endl;
}
};
class Cat : public Animal {
public:
void makeSound() override {
std::cout << "The cat meows" << std::endl;
}
};
void animalSound(Animal* animal) {
animal->makeSound();
}
int main() {
Animal* animal1 = new Animal();
Animal* dog = new Dog();
Animal* cat = new Cat();
std::cout << "Using base class pointer:" << std::endl;
animalSound(animal1);
animalSound(dog);
animalSound(cat);
delete animal1;
delete dog;
delete cat;
return 0;
}
9. Create a C++ program to demonstrate the Friend function.
#include <iostream>
class Box {
private:
double length;
double width;
double height;
public:
Box(double l, double w, double h) : length(l), width(w), height(h) {}
// Friend function declaration
friend double calculateVolume(const Box& box);
};
// Friend function definition
double calculateVolume(const Box& box) {
return box.length * box.width * box.height;
}
int main() {
Box myBox(3.0, 4.0, 5.0);
std::cout << "Volume of myBox: " << calculateVolume(myBox) << std::endl;
return 0;
}
10. Write a program in C++ to Create a file and add contents to it.
#include <iostream>
#include <fstream>
#include <string>
int main() {
std::string filename = "example.txt";
std::ofstream file(filename);
if (file.is_open()) {
file << "This is the first line.\n";
file << "Here's the second line.\n";
file << "And this is the third line.\n";
file.close();
std::cout << "File '" << filename << "' has been created and content has been
added." << std::endl;
} else {
std::cout << "Unable to open the file." << std::endl;
return 1;
}
// Reading and displaying the file contents
std::ifstream readFile(filename);
std::string line;
std::cout << "\nFile contents:" << std::endl;
if (readFile.is_open()) {
while (getline(readFile, line)) {
std::cout << line << std::endl;
}
readFile.close();
} else {
std::cout << "Unable to open the file for reading." << std::endl;
return 1;
}
return 0;
}