POE - OOPs With C

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Here’s a collection of C++ programs demonstrating various Object-

Oriented Programming (OOP) concepts based on your requests:


1. Employee Class
cpp
Copy
#include <iostream>
#include <string>

class Employee {
private:
int empNumber;
std::string empName;
float basic, da, it, netSalary;

public:
Employee(int number, std::string name, float basicSalary) {
empNumber = number;
empName = name;
basic = basicSalary;
da = basic * 0.2; // Assuming DA is 20% of Basic
it = (basic + da) * 0.1; // Assuming IT is 10% of (Basic + DA)
netSalary = (basic + da) - it;
}

void print() {
std::cout << "Employee Number: " << empNumber << "\n";
std::cout << "Employee Name: " << empName << "\n";
std::cout << "Basic: " << basic << "\n";
std::cout << "DA: " << da << "\n";
std::cout << "IT: " << it << "\n";
std::cout << "Net Salary: " << netSalary << "\n";
}
};

int main() {
Employee emp(101, "John Doe", 50000);
emp.print();
return 0;
}
2. Constructor Overloading
cpp
Copy
#include <iostream>

class Box {
private:
int length, breadth, height;

public:
Box(int l) : length(l), breadth(l), height(l) {} // Cube
Box(int l, int b, int h) : length(l), breadth(b), height(h) {} // Rectangular
box

void displayVolume() {
std::cout << "Volume: " << length * breadth * height << std::endl;
}
};

int main() {
Box cube(5);
Box rectBox(5, 10, 15);

cube.displayVolume(); // Volume of cube


rectBox.displayVolume(); // Volume of rectangular box

return 0;
}
3. Multilevel Inheritance
cpp
Copy
#include <iostream>
#include <string>

class Person {
protected:
std::string name;

public:
Person(std::string personName) : name(personName) {}
};

class Employee : public Person {


protected:
int empID;

public:
Employee(std::string personName, int id) : Person(personName), empID(id) {}
};

class Manager : public Employee {


private:
std::string department;

public:
Manager(std::string personName, int id, std::string dept) : Employee(personNam
e, id), department(dept) {}

void display() {
std::cout << "Manager Name: " << name << ", ID: " << empID << ", Departmen
t: " << department << std::endl;
}
};

int main() {
Manager mgr("Alice", 101, "HR");
mgr.display();
return 0;
}
4. Array of Class Objects for Students
cpp
Copy
#include <iostream>
#include <string>

class Student {
private:
std::string name;
int rollNumber;
char grade;

public:
void readData() {
std::cout << "Enter Name: ";
std::cin >> name;
std::cout << "Enter Roll Number: ";
std::cin >> rollNumber;
std::cout << "Enter Grade: ";
std::cin >> grade;
}

void displayData() {
std::cout << "Name: " << name << ", Roll Number: " << rollNumber << ", Gra
de: " << grade << std::endl;
}
};

int main() {
const int numStudents = 3;
Student students[numStudents];

for (int i = 0; i < numStudents; i++) {


std::cout << "Enter details for student " << (i + 1) << ":\n";
students[i].readData();
}

std::cout << "\nStudents' Details:\n";


for (int i = 0; i < numStudents; i++) {
students[i].displayData();
}

return 0;
}
5. Shape Class with Overloaded Function
cpp
Copy
#include <iostream>
#include <cmath>

class Shape {
public:
float perimeter(float side) { // Square
return 4 * side;
}

float perimeter(float length, float breadth) { // Rectangle


return 2 * (length + breadth);
}

float perimeter(float radius, bool isCircle) { // Circle


return 2 * M_PI * radius;
}
};

int main() {
Shape shape;
std::cout << "Perimeter of Square: " << shape.perimeter(5) << std::endl;
std::cout << "Perimeter of Rectangle: " << shape.perimeter(5, 10) << std::endl
;
std::cout << "Perimeter of Circle: " << shape.perimeter(5, true) << std::endl;
return 0;
}
6. Constructor with Default Argument
cpp
Copy
#include <iostream>

class Rectangle {
private:
int length, breadth;

public:
Rectangle(int l = 1, int b = 1) : length(l), breadth(b) {}

void displayArea() {
std::cout << "Area: " << length * breadth << std::endl;
}
};

int main() {
Rectangle rect1; // Uses default arguments
Rectangle rect2(5, 10); // Custom dimensions

rect1.displayArea();
rect2.displayArea();

return 0;
}
7. Account Class
cpp
Copy
#include <iostream>

class Account {
private:
float balance;

public:
Account() : balance(0) {}

void deposit(float amount) {


balance += amount;
std::cout << "Deposited: " << amount << "\n";
}

void withdraw(float amount) {


if (amount > balance) {
std::cout << "Insufficient balance!\n";
} else {
balance -= amount;
std::cout << "Withdrawn: " << amount << "\n";
}
}

void showBalance() {
std::cout << "Balance: " << balance << "\n";
}

void computeInterest(float rate) {


float interest = balance * rate / 100;
std::cout << "Interest: " << interest << "\n";
}
};

int main() {
Account acc;
acc.deposit(1000);
acc.showBalance();
acc.computeInterest(5);
acc.withdraw(200);
acc.showBalance();
return 0;
}
8. Virtual Functions
cpp
Copy
#include <iostream>

class Base {
public:
virtual void show() {
std::cout << "Base class show function called." << std::endl;
}
};

class Derived : public Base {


public:
void show() override {
std::cout << "Derived class show function called." << std::endl;
}
};

int main() {
Base* b; // Base class pointer
Derived d; // Derived class object
b = &d; // Base pointer pointing to derived object

b->show(); // Calls derived class show function


return 0;
}
9. Friend Function
cpp
Copy
#include <iostream>

class Box {
private:
int width;

public:
Box(int w) : width(w) {}

friend void printWidth(Box b);


};

void printWidth(Box b) {
std::cout << "Width: " << b.width << std::endl;
}

int main() {
Box box(10);
printWidth(box); // Accessing private member width
return 0;
}
10. File Creation and Writing
cpp
Copy
#include <iostream>
#include <fstream>

int main() {
std::ofstream outfile("example.txt");

if (outfile.is_open()) {
outfile << "Hello, this is a test file.\n";
outfile << "Writing some content to the file.\n";
outfile.close(); // Close the file
std::cout << "File created and content written successfully." << std::endl
;
} else {
std::cout << "Unable to open the file." << std::endl;
}

return 0;
}
These programs illustrate various OOP concepts in C++, including classes,
inheritance, polymorphism, constructors, and file operations. You can
compile and run these examples in any C++ development environment.

You might also like