POE - OOPs With C
POE - OOPs With C
POE - OOPs With C
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);
return 0;
}
3. Multilevel Inheritance
cpp
Copy
#include <iostream>
#include <string>
class Person {
protected:
std::string name;
public:
Person(std::string personName) : name(personName) {}
};
public:
Employee(std::string personName, int id) : Person(personName), empID(id) {}
};
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];
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;
}
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 showBalance() {
std::cout << "Balance: " << balance << "\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;
}
};
int main() {
Base* b; // Base class pointer
Derived d; // Derived class object
b = &d; // Base pointer pointing to derived object
class Box {
private:
int width;
public:
Box(int w) : width(w) {}
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.