fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // ============================================================================
  5. // OOP CONCEPT: CLASS & OBJECT
  6. // Class: A blueprint defining state (attributes) and behavior (functions).
  7. //
  8. // OOP PILLAR 1: ENCAPSULATION
  9. // Bundles data members and functions operating on them inside a single unit.
  10. // ============================================================================
  11. class Pizza {
  12. // ------------------------------------------------------------------------
  13. // OOP CONCEPT: DATA HIDING (Access Specifier: private)
  14. // Internal state is hidden from outside modification to enforce business rules.
  15. // ------------------------------------------------------------------------
  16. private:
  17. int basePri = 250;
  18. int nonBasePr = 400;
  19. int cheese = 50;
  20. int ncheese = 70;
  21. int topping = 80;
  22. int ntopping = 100;
  23.  
  24. // Internal state flags (Encapsulated validation)
  25. bool isCheese = false;
  26. bool isTop = false;
  27.  
  28. // ------------------------------------------------------------------------
  29. // OOP CONCEPT: ACCESS CONTROL (Access Specifier: protected)
  30. // Accessible only within this class and derived subclasses (Deluxe).
  31. // ------------------------------------------------------------------------
  32. protected:
  33. bool isVeg;
  34. int total = 0;
  35.  
  36. // ------------------------------------------------------------------------
  37. // OOP PILLAR 2: ABSTRACTION (Access Specifier: public)
  38. // Exposes essential actions while hiding implementation and calculation details.
  39. // ------------------------------------------------------------------------
  40. public:
  41. // --------------------------------------------------------------------
  42. // OOP CONCEPT: CONSTRUCTOR & 'this' POINTER
  43. // Invoked at object creation. 'this' refers to the current invoking instance.
  44. // --------------------------------------------------------------------
  45. Pizza(bool isVeg) {
  46. this->isVeg = isVeg;
  47. Base();
  48. }
  49.  
  50. // OOP CONCEPT: VIRTUAL DESTRUCTOR
  51. // Essential in base classes to prevent undefined behavior when deleting via base pointer.
  52. virtual ~Pizza() = default;
  53.  
  54. void Base() {
  55. if (this->isVeg) {
  56. this->total += this->basePri;
  57. cout << "Base Price :" << this->basePri << endl;
  58. } else {
  59. this->total += this->nonBasePr;
  60. cout << "Non Veg Base : " << this->nonBasePr << endl;
  61. }
  62. }
  63.  
  64. void Cheese() {
  65. if (this->isCheese) return;
  66. if (this->isVeg) {
  67. this->total += this->cheese;
  68. cout << "Cheese Price :" << this->cheese << endl;
  69. } else {
  70. this->total += this->ncheese;
  71. cout << "Non Veg Cheese : " << this->ncheese << endl;
  72. }
  73. this->isCheese = true;
  74. }
  75.  
  76. // --------------------------------------------------------------------
  77. // OOP PILLAR 4 (A): COMPILE-TIME POLYMORPHISM (Function Overloading)
  78. // Same function name 'Topping', different parameter list.
  79. // --------------------------------------------------------------------
  80. void Topping() {
  81. if (this->isTop) return;
  82. if (this->isVeg) {
  83. this->total += this->topping;
  84. cout << "Topping Price :" << this->topping << endl;
  85. } else {
  86. this->total += this->ntopping;
  87. cout << "Non Veg Topping : " << this->ntopping << endl;
  88. }
  89. this->isTop = true;
  90. }
  91.  
  92. // Overloaded variant: accepts quantity of extra toppings
  93. void Topping(int extraCount) {
  94. Topping(); // Add standard topping first
  95. int rate = this->isVeg ? this->topping : this->ntopping;
  96. this->total += (extraCount * rate);
  97. cout << "Extra Toppings (" << extraCount << ") Added: " << (extraCount * rate) << endl;
  98. }
  99.  
  100. // --------------------------------------------------------------------
  101. // OOP PILLAR 4 (B): RUNTIME POLYMORPHISM (Virtual Function)
  102. // 'virtual' enables dynamic dispatch via vtable so derived classes can override.
  103. // --------------------------------------------------------------------
  104. virtual double getBill() {
  105. int gst = this->total * 0.5;
  106. cout << "Total:" << this->total << endl;
  107. cout << "GST:" << gst << endl;
  108. int totalPr = this->total + gst;
  109. cout << "Bill To Pay:" << totalPr << endl;
  110. cout << " " << endl;
  111. return totalPr;
  112. }
  113. };
  114.  
  115. // ============================================================================
  116. // OOP PILLAR 3: INHERITANCE (Derived Subclass)
  117. // Models "IS-A" relationship: Deluxe IS A Pizza. Reuses Base, Cheese, Topping logic.
  118. // ============================================================================
  119. class Deluxe : public Pizza {
  120. public:
  121. // OOP CONCEPT: CONSTRUCTOR INITIALIZER LIST
  122. // Passes initialization arguments upstream to the base class constructor.
  123. Deluxe(bool isVeg) : Pizza(isVeg) {
  124. Cheese();
  125. Topping();
  126. }
  127.  
  128. // --------------------------------------------------------------------
  129. // OOP PILLAR 4 (C): RUNTIME POLYMORPHISM (Method Overriding)
  130. // Replaces base implementation to provide a Deluxe combo discount.
  131. // --------------------------------------------------------------------
  132. double getBill() override {
  133. cout << "[Deluxe Combo Applied: 50 Flat Discount]" << endl;
  134. this->total = max(0, this->total - 50);
  135. return Pizza::getBill(); // Calls base logic with adjusted total
  136. }
  137. };
  138.  
  139. int main() {
  140. // ------------------------------------------------------------------------
  141. // OBJECTS & METHOD OVERLOADING EXECUTION
  142. // ------------------------------------------------------------------------
  143. Pizza p1(false);
  144. p1.Cheese();
  145. p1.Topping(1); // Calls overloaded Topping(int) -> Compile-time Polymorphism
  146. p1.getBill();
  147.  
  148. Deluxe dp(true);
  149.  
  150. // ------------------------------------------------------------------------
  151. // RUNTIME POLYMORPHISM (Dynamic Dispatch via Base Pointer)
  152. // Base-class pointer pointing to derived-class object invokes Deluxe::getBill()
  153. // ------------------------------------------------------------------------
  154. Pizza* ptr = &dp;
  155. ptr->getBill(); // Executes Deluxe's overridden getBill() via vtable lookup
  156.  
  157. return 0;
  158. }
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
Non Veg Base : 400
Non Veg Cheese : 70
Non Veg Topping : 100
Extra Toppings (1) Added: 100
Total:670
GST:335
Bill To Pay:1005
               
Base Price :250
Cheese Price :50
Topping Price :80
[Deluxe Combo Applied: 50 Flat Discount]
Total:330
GST:165
Bill To Pay:495