fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5. // ================== Bank Account ==================
  6. class BankAcc
  7. {
  8. protected:
  9. string acc_holder, acc_num;
  10. double balance;
  11. public:
  12. BankAcc(string acc_holder="", string acc_num="", double balance=0)
  13. : acc_holder(acc_holder), acc_num(acc_num), balance(balance) {}
  14. void deposit(double amount) { if(amount>0) balance+=amount; }
  15. void withdraw(double amount) { if(amount>0 && amount<=balance) balance-=amount; }
  16. virtual void display()
  17. {
  18. cout<<"Holder: "<<acc_holder<<endl;
  19. cout<<"Number: "<<acc_num<<endl;
  20. cout<<"Balance: "<<balance<<endl;
  21. }
  22. };
  23. class SavingsAccount : public BankAcc
  24. {
  25. double interestRate;
  26. public:
  27. SavingsAccount(string acc_holder,string acc_num,double balance,double interestRate)
  28. : BankAcc(acc_holder,acc_num,balance), interestRate(interestRate) {}
  29. void addInterest() { balance+=balance*interestRate/100; }
  30. void display()
  31. {
  32. BankAcc::display();
  33. cout<<"Interest Rate: "<<interestRate<<"%"<<endl;
  34. }
  35. };
  36. // ================== Student ==================
  37. class Student
  38. {
  39. protected:
  40. string name, id;
  41. double grades[20];
  42. int cnt=0;
  43. public:
  44. Student(string name="",string id="") : name(name), id(id) {}
  45. void add_grade(double g) { grades[cnt++]=g; }
  46. double calc_avg()
  47. {
  48. double sum=0;
  49. for(int i=0;i<cnt;i++) sum+=grades[i];
  50. return cnt>0 ? sum/cnt :0;
  51. }
  52. virtual void display()
  53. {
  54. cout<<"Name: "<<name<<endl;
  55. cout<<"ID: "<<id<<endl;
  56. cout<<"Grades: ";
  57. for(int i=0;i<cnt;i++) cout<<grades[i]<<" ";
  58. cout<<endl;
  59. cout<<"Average: "<<calc_avg()<<endl;
  60. }
  61. };
  62. class GraduateStudent : public Student
  63. {
  64. string graduationProject;
  65. public:
  66. GraduateStudent(string name,string id,string graduationProject)
  67. : Student(name,id), graduationProject(graduationProject) {}
  68. void display()
  69. {
  70. Student::display();
  71. cout<<"Graduation Project: "<<graduationProject<<endl;
  72. }
  73. };
  74. // ================== Rectangle ==================
  75. class Rectangle
  76. {
  77. protected:
  78. double length, width;
  79. public:
  80. Rectangle(double length=0,double width=0) : length(length), width(width) {}
  81. double calc_area() { return length*width; }
  82. double calc_perimeter() { return 2*(length+width); }
  83. void display()
  84. {
  85. cout<<"Length: "<<length<<endl;
  86. cout<<"Width: "<<width<<endl;
  87. }
  88. };
  89. // ================== Shopping Cart ==================
  90. class ShoppingCart
  91. {
  92. protected:
  93. vector<pair<string,double>> items;
  94. double total=0;
  95. public:
  96. ShoppingCart() : total(0) {}
  97. void add(string name,double price)
  98. {
  99. items.push_back({name,price});
  100. total+=price;
  101. }
  102. void remove(string name)
  103. {
  104. for(int i=0;i<items.size();i++)
  105. {
  106. if(items[i].first==name)
  107. {
  108. total-=items[i].second;
  109. items.erase(items.begin()+i);
  110. break;
  111. }
  112. }
  113. }
  114. double calc() { return total; }
  115. virtual void show()
  116. {
  117. cout<<"Total Price = "<<calc()<<endl;
  118. cout<<"Items: ";
  119. for(auto &it: items) cout<<it.first<<" ";
  120. cout<<endl;
  121. }
  122. };
  123. class DiscountCart : public ShoppingCart
  124. {
  125. double discountRate;
  126. public:
  127. DiscountCart(double discountRate) : discountRate(discountRate) {}
  128. double calc()
  129. {
  130. return ShoppingCart::calc()*(1-discountRate/100);
  131. }
  132. void show()
  133. {
  134. cout<<"Total Price after discount = "<<calc()<<endl;
  135. cout<<"Items: ";
  136. for(auto &it: items) cout<<it.first<<" ";
  137. cout<<endl;
  138. cout<<"Discount Rate: "<<discountRate<<"%"<<endl;
  139. }
  140. };
  141. // ================== Phone ==================
  142. class Phone
  143. {
  144. protected:
  145. string brand, model;
  146. int storage, freestorage, battery;
  147. public:
  148. Phone(string brand,string model,int storage)
  149. : brand(brand), model(model), storage(storage), freestorage(storage), battery(100) {}
  150. void install(int size) { if(size<=freestorage) freestorage-=size; else cout<<"Not enough storage"<<endl; }
  151. void usebatt(int amt) { battery=max(0,battery-amt); }
  152. void charge() { battery=100; }
  153. virtual void show()
  154. {
  155. cout<<"Brand: "<<brand<<endl;
  156. cout<<"Model: "<<model<<endl;
  157. cout<<"Storage: "<<storage<<endl;
  158. cout<<"Free Storage: "<<freestorage<<endl;
  159. cout<<"Battery: "<<battery<<endl;
  160. }
  161. };
  162. class Smartphone : public Phone
  163. {
  164. string os;
  165. public:
  166. Smartphone(string brand,string model,int storage,string os)
  167. : Phone(brand,model,storage), os(os) {}
  168. void show()
  169. {
  170. Phone::show();
  171. cout<<"OS: "<<os<<endl;
  172. }
  173. };
  174. // ================== Temperature ==================
  175. class Temperature
  176. {
  177. protected:
  178. float cel;
  179. public:
  180. Temperature(float cel) : cel(cel) {}
  181. float kel() { return cel+273.15; }
  182. float fahr() { return cel*9/5+32; }
  183. void show()
  184. {
  185. cout<<"Celsius: "<<cel<<endl;
  186. cout<<"Fahrenheit: "<<fahr()<<endl;
  187. cout<<"Kelvin: "<<kel()<<endl;
  188. }
  189. };
  190. // ================== Car ==================
  191. class Car
  192. {
  193. protected:
  194. string make, model;
  195. int year, mileage;
  196. public:
  197. Car(string make, string model, int year, int mileage)
  198. : make(make), model(model), year(year), mileage(mileage) {}
  199. void updateMileage(int newMileage) { if (newMileage >= mileage) mileage = newMileage; }
  200. virtual void display() { cout << make << " " << model << " " << year << " " << mileage << " km\n"; }
  201. bool needsService(int threshold) { return mileage >= threshold; }
  202. };
  203. class ElectricCar : public Car
  204. {
  205. int battery;
  206. public:
  207. ElectricCar(string make, string model, int year, int mileage, int battery)
  208. : Car(make, model, year, mileage), battery(battery) {}
  209. void display() { Car::display(); cout << "Battery: " << battery << "%\n"; }
  210. void charge() { battery = 100; cout << "Battery fully charged!\n"; }
  211. };
  212. // ================== Book ==================
  213. class Book
  214. {
  215. protected:
  216. string title, author, isbn;
  217. bool available;
  218. public:
  219. Book(string title, string author, string isbn)
  220. : title(title), author(author), isbn(isbn), available(true) {}
  221. void checkOut() { if (available) available = false; }
  222. void returnBook() { if (!available) available = true; }
  223. virtual void display()
  224. { cout << "Title: " << title << ", Author: " << author << ", ISBN: " << isbn
  225. << ", Availability: " << (available ? "Available" : "Checked out") << "\n"; }
  226. };
  227. class EBook : public Book
  228. {
  229. string fileFormat;
  230. public:
  231. EBook(string title, string author, string isbn, string format)
  232. : Book(title, author, isbn), fileFormat(format) {}
  233. void display() { Book::display(); cout << "File format: " << fileFormat << "\n"; }
  234. };
  235. // ================== Employee ==================
  236. class Employee
  237. {
  238. protected:
  239. string name;
  240. int employeeID;
  241. string department;
  242. double salary;
  243. public:
  244. Employee(string name, int id, string dept, double sal)
  245. : name(name), employeeID(id), department(dept), salary(sal) {}
  246. void updateSalary(double newSalary) { if (newSalary > 0) salary = newSalary; }
  247. virtual void display()
  248. { cout << "Name: " << name << ", Employee ID: " << employeeID
  249. << ", Department: " << department << ", Salary: $" << salary << "\n"; }
  250. double calculateBonus() { return salary * 0.10; }
  251. };
  252. class Manager : public Employee
  253. {
  254. int teamSize;
  255. public:
  256. Manager(string name, int id, string dept, double sal, int team)
  257. : Employee(name, id, dept, sal), teamSize(team) {}
  258. void display() { Employee::display(); cout << "Team Size: " << teamSize << "\n"; }
  259. };
  260. int main()
  261. {
  262. cout<<"==== Bank Account ===="<<endl;
  263. SavingsAccount acc("Ali","12345",1000,5);
  264. acc.addInterest();
  265. acc.display();
  266.  
  267. cout<<"\n==== Student ===="<<endl;
  268. GraduateStudent s("Karim","333","AI Thesis");
  269. s.add_grade(95);
  270. s.add_grade(88);
  271. s.display();
  272.  
  273. cout<<"\n==== Rectangle ===="<<endl;
  274. Rectangle r(5,5);
  275. r.display();
  276. cout<<"Area: "<<r.calc_area()<<endl;
  277.  
  278. cout<<"\n==== Shopping Cart ===="<<endl;
  279. DiscountCart cart(10);
  280. cart.add("Milk",20);
  281. cart.add("Bread",10);
  282. cart.show();
  283.  
  284. cout<<"\n==== Phone ===="<<endl;
  285. Smartphone ph("Samsung","A51",128,"Android");
  286. ph.install(2);
  287. ph.usebatt(20);
  288. ph.show();
  289.  
  290. cout<<"\n==== Temperature ===="<<endl;
  291. Temperature temp(25);
  292. temp.show();
  293.  
  294. cout<<"\n==== Car ===="<<endl;
  295. ElectricCar tesla("Tesla","Model 3",2022,15000,80);
  296. tesla.display();
  297. tesla.charge();
  298. tesla.updateMileage(20000);
  299. cout << (tesla.needsService(30000) ? "Needs service\n" : "No service needed\n");
  300.  
  301. cout<<"\n==== Book ===="<<endl;
  302. EBook ebook1("Digital Fortress","Dan Brown","978-031233516","PDF");
  303. ebook1.display();
  304. ebook1.checkOut();
  305. ebook1.display();
  306.  
  307. cout<<"\n==== Employee ===="<<endl;
  308. Manager mgr("Sara Ali",201,"IT",8000,5);
  309. mgr.display();
  310. cout<<"Annual Bonus: $"<<mgr.calculateBonus()<<endl;
  311. }
Success #stdin #stdout 0s 5312KB
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