#include <iostream>
#include <string>
#include <vector>
using namespace std;
// ================== Bank Account ==================
class BankAcc
{
protected:
string acc_holder, acc_num;
double balance;
public:
BankAcc(string acc_holder="", string acc_num="", double balance=0)
: acc_holder(acc_holder), acc_num(acc_num), balance(balance) {}
void deposit(double amount) { if(amount>0) balance+=amount; }
void withdraw(double amount) { if(amount>0 && amount<=balance) balance-=amount; }
virtual void display()
{
cout<<"Holder: "<<acc_holder<<endl;
cout<<"Number: "<<acc_num<<endl;
cout<<"Balance: "<<balance<<endl;
}
};
class SavingsAccount : public BankAcc
{
double interestRate;
public:
SavingsAccount(string acc_holder,string acc_num,double balance,double interestRate)
: BankAcc(acc_holder,acc_num,balance), interestRate(interestRate) {}
void addInterest() { balance+=balance*interestRate/100; }
void display()
{
BankAcc::display();
cout<<"Interest Rate: "<<interestRate<<"%"<<endl;
}
};
// ================== Student ==================
class Student
{
protected:
string name, id;
double grades[20];
int cnt=0;
public:
Student(string name="",string id="") : name(name), id(id) {}
void add_grade(double g) { grades[cnt++]=g; }
double calc_avg()
{
double sum=0;
for(int i=0;i<cnt;i++) sum+=grades[i];
return cnt>0 ? sum/cnt :0;
}
virtual void display()
{
cout<<"Name: "<<name<<endl;
cout<<"ID: "<<id<<endl;
cout<<"Grades: ";
for(int i=0;i<cnt;i++) cout<<grades[i]<<" ";
cout<<endl;
cout<<"Average: "<<calc_avg()<<endl;
}
};
class GraduateStudent : public Student
{
string graduationProject;
public:
GraduateStudent(string name,string id,string graduationProject)
: Student(name,id), graduationProject(graduationProject) {}
void display()
{
Student::display();
cout<<"Graduation Project: "<<graduationProject<<endl;
}
};
// ================== Rectangle ==================
class Rectangle
{
protected:
double length, width;
public:
Rectangle(double length=0,double width=0) : length(length), width(width) {}
double calc_area() { return length*width; }
double calc_perimeter() { return 2*(length+width); }
void display()
{
cout<<"Length: "<<length<<endl;
cout<<"Width: "<<width<<endl;
}
};
// ================== Shopping Cart ==================
class ShoppingCart
{
protected:
vector<pair<string,double>> items;
double total=0;
public:
ShoppingCart() : total(0) {}
void add(string name,double price)
{
items.push_back({name,price});
total+=price;
}
void remove(string name)
{
for(int i=0;i<items.size();i++)
{
if(items[i].first==name)
{
total-=items[i].second;
items.erase(items.begin()+i);
break;
}
}
}
double calc() { return total; }
virtual void show()
{
cout<<"Total Price = "<<calc()<<endl;
cout<<"Items: ";
for(auto &it: items) cout<<it.first<<" ";
cout<<endl;
}
};
class DiscountCart : public ShoppingCart
{
double discountRate;
public:
DiscountCart(double discountRate) : discountRate(discountRate) {}
double calc()
{
return ShoppingCart::calc()*(1-discountRate/100);
}
void show()
{
cout<<"Total Price after discount = "<<calc()<<endl;
cout<<"Items: ";
for(auto &it: items) cout<<it.first<<" ";
cout<<endl;
cout<<"Discount Rate: "<<discountRate<<"%"<<endl;
}
};
// ================== Phone ==================
class Phone
{
protected:
string brand, model;
int storage, freestorage, battery;
public:
Phone(string brand,string model,int storage)
: brand(brand), model(model), storage(storage), freestorage(storage), battery(100) {}
void install(int size) { if(size<=freestorage) freestorage-=size; else cout<<"Not enough storage"<<endl; }
void usebatt(int amt) { battery=max(0,battery-amt); }
void charge() { battery=100; }
virtual void show()
{
cout<<"Brand: "<<brand<<endl;
cout<<"Model: "<<model<<endl;
cout<<"Storage: "<<storage<<endl;
cout<<"Free Storage: "<<freestorage<<endl;
cout<<"Battery: "<<battery<<endl;
}
};
class Smartphone : public Phone
{
string os;
public:
Smartphone(string brand,string model,int storage,string os)
: Phone(brand,model,storage), os(os) {}
void show()
{
Phone::show();
cout<<"OS: "<<os<<endl;
}
};
// ================== Temperature ==================
class Temperature
{
protected:
float cel;
public:
Temperature(float cel) : cel(cel) {}
float kel() { return cel+273.15; }
float fahr() { return cel*9/5+32; }
void show()
{
cout<<"Celsius: "<<cel<<endl;
cout<<"Fahrenheit: "<<fahr()<<endl;
cout<<"Kelvin: "<<kel()<<endl;
}
};
// ================== Car ==================
class Car
{
protected:
string make, model;
int year, mileage;
public:
Car(string make, string model, int year, int mileage)
: make(make), model(model), year(year), mileage(mileage) {}
void updateMileage(int newMileage) { if (newMileage >= mileage) mileage = newMileage; }
virtual void display() { cout << make << " " << model << " " << year << " " << mileage << " km\n"; }
bool needsService(int threshold) { return mileage >= threshold; }
};
class ElectricCar : public Car
{
int battery;
public:
ElectricCar(string make, string model, int year, int mileage, int battery)
: Car(make, model, year, mileage), battery(battery) {}
void display() { Car::display(); cout << "Battery: " << battery << "%\n"; }
void charge() { battery = 100; cout << "Battery fully charged!\n"; }
};
// ================== Book ==================
class Book
{
protected:
string title, author, isbn;
bool available;
public:
Book(string title, string author, string isbn)
: title(title), author(author), isbn(isbn), available(true) {}
void checkOut() { if (available) available = false; }
void returnBook() { if (!available) available = true; }
virtual void display()
{ cout << "Title: " << title << ", Author: " << author << ", ISBN: " << isbn
<< ", Availability: " << (available ? "Available" : "Checked out") << "\n"; }
};
class EBook : public Book
{
string fileFormat;
public:
EBook(string title, string author, string isbn, string format)
: Book(title, author, isbn), fileFormat(format) {}
void display() { Book::display(); cout << "File format: " << fileFormat << "\n"; }
};
// ================== Employee ==================
class Employee
{
protected:
string name;
int employeeID;
string department;
double salary;
public:
Employee(string name, int id, string dept, double sal)
: name(name), employeeID(id), department(dept), salary(sal) {}
void updateSalary(double newSalary) { if (newSalary > 0) salary = newSalary; }
virtual void display()
{ cout << "Name: " << name << ", Employee ID: " << employeeID
<< ", Department: " << department << ", Salary: $" << salary << "\n"; }
double calculateBonus() { return salary * 0.10; }
};
class Manager : public Employee
{
int teamSize;
public:
Manager(string name, int id, string dept, double sal, int team)
: Employee(name, id, dept, sal), teamSize(team) {}
void display() { Employee::display(); cout << "Team Size: " << teamSize << "\n"; }
};
int main()
{
cout<<"==== Bank Account ===="<<endl;
SavingsAccount acc("Ali","12345",1000,5);
acc.addInterest();
acc.display();
cout<<"\n==== Student ===="<<endl;
GraduateStudent s("Karim","333","AI Thesis");
s.add_grade(95);
s.add_grade(88);
s.display();
cout<<"\n==== Rectangle ===="<<endl;
Rectangle r(5,5);
r.display();
cout<<"Area: "<<r.calc_area()<<endl;
cout<<"\n==== Shopping Cart ===="<<endl;
DiscountCart cart(10);
cart.add("Milk",20);
cart.add("Bread",10);
cart.show();
cout<<"\n==== Phone ===="<<endl;
Smartphone ph("Samsung","A51",128,"Android");
ph.install(2);
ph.usebatt(20);
ph.show();
cout<<"\n==== Temperature ===="<<endl;
Temperature temp(25);
temp.show();
cout<<"\n==== Car ===="<<endl;
ElectricCar tesla("Tesla","Model 3",2022,15000,80);
tesla.display();
tesla.charge();
tesla.updateMileage(20000);
cout << (tesla.needsService(30000) ? "Needs service\n" : "No service needed\n");
cout<<"\n==== Book ===="<<endl;
EBook ebook1("Digital Fortress","Dan Brown","978-031233516","PDF");
ebook1.display();
ebook1.checkOut();
ebook1.display();
cout<<"\n==== Employee ===="<<endl;
Manager mgr("Sara Ali",201,"IT",8000,5);
mgr.display();
cout<<"Annual Bonus: $"<<mgr.calculateBonus()<<endl;
}