#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 1️⃣ 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;
}
};
// 2️⃣ 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;
}
};
// 3️⃣ 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;
}
};
// 4️⃣ 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;
}
};
// 5️⃣ 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;
}
};
// 6️⃣ 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;
}
};
//////////////////////////////////////////
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();
}