fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. // ================== Bank Account ==================
  7. class BankAcc
  8. {
  9. protected:
  10. string acc_holder, acc_num;
  11. double balance;
  12. public:
  13. BankAcc(string acc_holder="", string acc_num="", double balance=0)
  14. : acc_holder(acc_holder), acc_num(acc_num), balance(balance) {}
  15. void deposit(double amount) { if(amount>0) balance+=amount; }
  16. void withdraw(double amount) { if(amount>0 && amount<=balance) balance-=amount; }
  17. virtual void display()
  18. {
  19. cout<<"Holder: "<<acc_holder<<endl;
  20. cout<<"Number: "<<acc_num<<endl;
  21. cout<<"Balance: "<<balance<<endl;
  22. }
  23. string getAccHolder() { return acc_holder; }
  24. string getAccNumber() { return acc_num; }
  25. double getBalance() { return balance; }
  26. };
  27.  
  28. class SavingsAccount : public BankAcc
  29. {
  30. double interestRate;
  31. public:
  32. SavingsAccount(string acc_holder,string acc_num,double balance,double interestRate)
  33. : BankAcc(acc_holder,acc_num,balance), interestRate(interestRate) {}
  34. void addInterest() { balance+=balance*interestRate/100; }
  35. void display()
  36. {
  37. BankAcc::display();
  38. cout<<"Interest Rate: "<<interestRate<<"%"<<endl;
  39. }
  40. };
  41.  
  42. // ================== Customer ==================
  43. class Customer {
  44. private:
  45. string name;
  46. string customerID;
  47. vector<BankAcc*> accounts;
  48. public:
  49. Customer(string custName, string id) : name(custName), customerID(id) {}
  50. ~Customer() { for(auto acc : accounts) delete acc; }
  51. void addAccount(string accHolder, string accNum, double initialBalance = 0.0) {
  52. accounts.push_back(new BankAcc(accHolder, accNum, initialBalance));
  53. }
  54. void addSavingsAccount(string accHolder, string accNum, double initialBalance, double interestRate) {
  55. accounts.push_back(new SavingsAccount(accHolder, accNum, initialBalance, interestRate));
  56. }
  57. void displayAllAccounts() {
  58. cout << "\nCustomer: " << name << " (ID: " << customerID << ")\n";
  59. for(int i=0;i<accounts.size();i++) {
  60. cout << "\n--- Account " << i+1 << " ---\n";
  61. accounts[i]->display();
  62. }
  63. }
  64. double getTotalBalance() {
  65. double total = 0;
  66. for(auto acc : accounts) total += acc->getBalance();
  67. return total;
  68. }
  69. };
  70.  
  71. // ================== Student ==================
  72. class Student
  73. {
  74. protected:
  75. string name, id;
  76. double grades[20];
  77. int cnt=0;
  78. public:
  79. Student(string name="",string id="") : name(name), id(id) {}
  80. void add_grade(double g) { grades[cnt++]=g; }
  81. double calc_avg()
  82. {
  83. double sum=0;
  84. for(int i=0;i<cnt;i++) sum+=grades[i];
  85. return cnt>0 ? sum/cnt :0;
  86. }
  87. virtual void display()
  88. {
  89. cout<<"Name: "<<name<<endl;
  90. cout<<"ID: "<<id<<endl;
  91. cout<<"Grades: ";
  92. for(int i=0;i<cnt;i++) cout<<grades[i]<<" ";
  93. cout<<endl;
  94. cout<<"Average: "<<calc_avg()<<endl;
  95. }
  96. };
  97.  
  98. class GraduateStudent : public Student
  99. {
  100. string graduationProject;
  101. public:
  102. GraduateStudent(string name,string id,string graduationProject)
  103. : Student(name,id), graduationProject(graduationProject) {}
  104. void display()
  105. {
  106. Student::display();
  107. cout<<"Graduation Project: "<<graduationProject<<endl;
  108. }
  109. };
  110.  
  111. // ================== Rectangle ==================
  112. class Rectangle
  113. {
  114. protected:
  115. double length, width;
  116. public:
  117. Rectangle(double length=0,double width=0) : length(length), width(width) {}
  118. double calc_area() { return length*width; }
  119. double calc_perimeter() { return 2*(length+width); }
  120. void display()
  121. {
  122. cout<<"Length: "<<length<<endl;
  123. cout<<"Width: "<<width<<endl;
  124. }
  125. };
  126.  
  127. // ================== Shopping Cart ==================
  128. class ShoppingCart
  129. {
  130. protected:
  131. vector<pair<string,double>> items;
  132. double total=0;
  133. public:
  134. ShoppingCart() : total(0) {}
  135. void add(string name,double price)
  136. {
  137. items.push_back({name,price});
  138. total+=price;
  139. }
  140. void remove(string name)
  141. {
  142. for(int i=0;i<items.size();i++)
  143. {
  144. if(items[i].first==name)
  145. {
  146. total-=items[i].second;
  147. items.erase(items.begin()+i);
  148. break;
  149. }
  150. }
  151. }
  152. double calc() { return total; }
  153. virtual void show()
  154. {
  155. cout<<"Total Price = "<<calc()<<endl;
  156. cout<<"Items: ";
  157. for(auto &it: items) cout<<it.first<<" ";
  158. cout<<endl;
  159. }
  160. };
  161.  
  162. class DiscountCart : public ShoppingCart
  163. {
  164. double discountRate;
  165. public:
  166. DiscountCart(double discountRate) : discountRate(discountRate) {}
  167. double calc()
  168. {
  169. return ShoppingCart::calc()*(1-discountRate/100);
  170. }
  171. void show()
  172. {
  173. cout<<"Total Price after discount = "<<calc()<<endl;
  174. cout<<"Items: ";
  175. for(auto &it: items) cout<<it.first<<" ";
  176. cout<<endl;
  177. cout<<"Discount Rate: "<<discountRate<<"%"<<endl;
  178. }
  179. };
  180.  
  181. // ================== Phone ==================
  182. class Phone
  183. {
  184. protected:
  185. string brand, model;
  186. int storage, freestorage, battery;
  187. public:
  188. Phone(string brand,string model,int storage)
  189. : brand(brand), model(model), storage(storage), freestorage(storage), battery(100) {}
  190. void install(int size) { if(size<=freestorage) freestorage-=size; else cout<<"Not enough storage"<<endl; }
  191. void usebatt(int amt) { battery=max(0,battery-amt); }
  192. void charge() { battery=100; }
  193. virtual void show()
  194. {
  195. cout<<"Brand: "<<brand<<endl;
  196. cout<<"Model: "<<model<<endl;
  197. cout<<"Storage: "<<storage<<endl;
  198. cout<<"Free Storage: "<<freestorage<<endl;
  199. cout<<"Battery: "<<battery<<endl;
  200. }
  201. };
  202.  
  203. class Smartphone : public Phone
  204. {
  205. string os;
  206. public:
  207. Smartphone(string brand,string model,int storage,string os)
  208. : Phone(brand,model,storage), os(os) {}
  209. void show()
  210. {
  211. Phone::show();
  212. cout<<"OS: "<<os<<endl;
  213. }
  214. };
  215.  
  216. // ================== Temperature ==================
  217. class Temperature
  218. {
  219. protected:
  220. float cel;
  221. public:
  222. Temperature(float cel) : cel(cel) {}
  223. float kel() { return cel+273.15; }
  224. float fahr() { return cel*9/5+32; }
  225. void show()
  226. {
  227. cout<<"Celsius: "<<cel<<endl;
  228. cout<<"Fahrenheit: "<<fahr()<<endl;
  229. cout<<"Kelvin: "<<kel()<<endl;
  230. }
  231. };
  232.  
  233. // ================== Car ==================
  234. class Car
  235. {
  236. protected:
  237. string make, model;
  238. int year, mileage;
  239. public:
  240. Car(string make, string model, int year, int mileage)
  241. : make(make), model(model), year(year), mileage(mileage) {}
  242. void updateMileage(int newMileage) { if (newMileage >= mileage) mileage = newMileage; }
  243. virtual void display() { cout << make << " " << model << " " << year << " " << mileage << " km\n"; }
  244. bool needsService(int threshold) { return mileage >= threshold; }
  245. };
  246.  
  247. class ElectricCar : public Car
  248. {
  249. int battery;
  250. public:
  251. ElectricCar(string make, string model, int year, int mileage, int battery)
  252. : Car(make, model, year, mileage), battery(battery) {}
  253. void display() { Car::display(); cout << "Battery: " << battery << "%\n"; }
  254. void charge() { battery = 100; cout << "Battery fully charged!\n"; }
  255. };
  256.  
  257. // ================== Book ==================
  258. class Book
  259. {
  260. protected:
  261. string title, author, isbn;
  262. bool available;
  263. public:
  264. Book(string title, string author, string isbn)
  265. : title(title), author(author), isbn(isbn), available(true) {}
  266. void checkOut() { if (available) available = false; }
  267. void returnBook() { if (!available) available = true; }
  268. virtual void display()
  269. { cout << "Title: " << title << ", Author: " << author << ", ISBN: " << isbn
  270. << ", Availability: " << (available ? "Available" : "Checked out") << "\n"; }
  271. };
  272.  
  273. class EBook : public Book
  274. {
  275. string fileFormat;
  276. public:
  277. EBook(string title, string author, string isbn, string format)
  278. : Book(title, author, isbn), fileFormat(format) {}
  279. void display() { Book::display(); cout << "File format: " << fileFormat << "\n"; }
  280. };
  281.  
  282. // ================== Employee ==================
  283. class Employee
  284. {
  285. protected:
  286. string name;
  287. int employeeID;
  288. string department;
  289. double salary;
  290. public:
  291. Employee(string name, int id, string dept, double sal)
  292. : name(name), employeeID(id), department(dept), salary(sal) {}
  293. void updateSalary(double newSalary) { if (newSalary > 0) salary = newSalary; }
  294. virtual void display()
  295. { cout << "Name: " << name << ", Employee ID: " << employeeID
  296. << ", Department: " << department << ", Salary: $" << salary << "\n"; }
  297. double calculateBonus() { return salary * 0.10; }
  298. };
  299.  
  300. class Manager : public Employee
  301. {
  302. int teamSize;
  303. public:
  304. Manager(string name, int id, string dept, double sal, int team)
  305. : Employee(name, id, dept, sal), teamSize(team) {}
  306. void display() { Employee::display(); cout << "Team Size: " << teamSize << "\n"; }
  307. };
  308.  
  309. // ================== Dog ==================
  310. class Animal
  311. {
  312. protected:
  313. string name;
  314. int age;
  315. public:
  316. Animal(string name, int age) : name(name), age(age) {}
  317. virtual void speak() { cout << "Animal sound" << endl; }
  318. };
  319.  
  320. class Dog : public Animal
  321. {
  322. string breed;
  323. public:
  324. Dog(string name, string breed, int age) : Animal(name, age), breed(breed) {}
  325. void speak() { cout << name << " WOOF WOOF" << endl; }
  326. int human_age() { return 7*age; }
  327. void display()
  328. {
  329. cout << "Name: " << name << endl;
  330. cout << "Breed: " << breed << endl;
  331. cout << "Age: " << age << " years" << endl;
  332. cout << "Human age: " << human_age() << " years" << endl;
  333. }
  334. };
  335. // ================== Main ==================
  336. int main()
  337. {
  338. cout<<"==== Bank Account ===="<<endl;
  339. SavingsAccount acc("Ali","12345",1000,5);
  340. acc.addInterest();
  341. acc.display();
  342.  
  343. cout<<"\n==== Student ===="<<endl;
  344. GraduateStudent s("Karim","333","AI Thesis");
  345. s.add_grade(95);
  346. s.add_grade(88);
  347. s.display();
  348.  
  349. cout<<"\n==== Rectangle ===="<<endl;
  350. Rectangle r(5,5);
  351. r.display();
  352. cout<<"Area: "<<r.calc_area()<<endl;
  353.  
  354. cout<<"\n==== Shopping Cart ===="<<endl;
  355. DiscountCart cart(10);
  356. cart.add("Milk",20);
  357. cart.add("Bread",10);
  358. cart.show();
  359.  
  360. cout<<"\n==== Phone ===="<<endl;
  361. Smartphone ph("Samsung","A51",128,"Android");
  362. ph.install(2);
  363. ph.usebatt(20);
  364. ph.show();
  365.  
  366. cout<<"\n==== Temperature ===="<<endl;
  367. Temperature temp(25);
  368. temp.show();
  369.  
  370. cout<<"\n==== Car ===="<<endl;
  371. ElectricCar tesla("Tesla","Model 3",2022,15000,80);
  372. tesla.display();
  373. tesla.charge();
  374. tesla.updateMileage(20000);
  375. cout << (tesla.needsService(30000) ? "Needs service\n" : "No service needed\n");
  376.  
  377. cout<<"\n==== Book ===="<<endl;
  378. EBook ebook1("Digital Fortress","Dan Brown","978-031233516","PDF");
  379. ebook1.display();
  380. ebook1.checkOut();
  381. ebook1.display();
  382.  
  383. cout<<"\n==== Employee ===="<<endl;
  384. Manager mgr("Sara Ali",201,"IT",8000,5);
  385. mgr.display();
  386. cout<<"Annual Bonus: $"<<mgr.calculateBonus()<<endl;
  387.  
  388. cout << "\n==== Dog ====" << endl;
  389. Dog dog1("Rex", "German Shepherd", 3);
  390. dog1.speak();
  391. dog1.display();
  392.  
  393. cout<<"\n==== Customer System ===="<<endl;
  394. Customer customer1("Ahmed Mohamed", "CUST001");
  395. customer1.addAccount("Ahmed Mohamed", "ACC1001", 5000.00);
  396. customer1.addSavingsAccount("Ahmed Mohamed", "ACC1002", 10000.00, 5.0);
  397. customer1.addAccount("Ahmed Mohamed", "ACC1003", 7500.00);
  398. customer1.displayAllAccounts();
  399. cout<<"Total Balance: $"<<customer1.getTotalBalance()<<endl;
  400. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
==== Bank Account ====
Holder: Ali
Number: 12345
Balance: 1050
Interest Rate: 5%

==== Student ====
Name: Karim
ID: 333
Grades: 95 88 
Average: 91.5
Graduation Project: AI Thesis

==== Rectangle ====
Length: 5
Width: 5
Area: 25

==== Shopping Cart ====
Total Price after discount = 27
Items: Milk Bread 
Discount Rate: 10%

==== Phone ====
Brand: Samsung
Model: A51
Storage: 128
Free Storage: 126
Battery: 80
OS: Android

==== Temperature ====
Celsius: 25
Fahrenheit: 77
Kelvin: 298.15

==== Car ====
Tesla Model 3 2022 15000 km
Battery: 80%
Battery fully charged!
No service needed

==== Book ====
Title: Digital Fortress, Author: Dan Brown, ISBN: 978-031233516, Availability: Available
File format: PDF
Title: Digital Fortress, Author: Dan Brown, ISBN: 978-031233516, Availability: Checked out
File format: PDF

==== Employee ====
Name: Sara Ali, Employee ID: 201, Department: IT, Salary: $8000
Team Size: 5
Annual Bonus: $800

==== Dog ====
Rex WOOF WOOF
Name: Rex
Breed: German Shepherd
Age: 3 years
Human age: 21 years

==== Customer System ====

Customer: Ahmed Mohamed (ID: CUST001)

--- Account 1 ---
Holder: Ahmed Mohamed
Number: ACC1001
Balance: 5000

--- Account 2 ---
Holder: Ahmed Mohamed
Number: ACC1002
Balance: 10000
Interest Rate: 5%

--- Account 3 ---
Holder: Ahmed Mohamed
Number: ACC1003
Balance: 7500
Total Balance: $22500