fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5. // 1️⃣ 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. // 2️⃣ 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. // 3️⃣ 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. // 4️⃣ 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. // 5️⃣ 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. // 6️⃣ 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. //////////////////////////////////////////
  191. int main()
  192. {
  193. cout<<"==== Bank Account ===="<<endl;
  194. SavingsAccount acc("Ali","12345",1000,5);
  195. acc.addInterest();
  196. acc.display();
  197.  
  198. cout<<"\n==== Student ===="<<endl;
  199. GraduateStudent s("Karim","333","AI Thesis");
  200. s.add_grade(95);
  201. s.add_grade(88);
  202. s.display();
  203.  
  204. cout<<"\n==== Rectangle ===="<<endl;
  205. Rectangle r(5,5);
  206. r.display();
  207. cout<<"Area: "<<r.calc_area()<<endl;
  208.  
  209. cout<<"\n==== Shopping Cart ===="<<endl;
  210. DiscountCart cart(10);
  211. cart.add("Milk",20);
  212. cart.add("Bread",10);
  213. cart.show();
  214.  
  215. cout<<"\n==== Phone ===="<<endl;
  216. Smartphone ph("Samsung","A51",128,"Android");
  217. ph.install(2);
  218. ph.usebatt(20);
  219. ph.show();
  220.  
  221. cout<<"\n==== Temperature ===="<<endl;
  222. Temperature temp(25);
  223. temp.show();
  224. }
Success #stdin #stdout 0.01s 5276KB
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