Page 82 - Modular_V2.0_C++_Flikpbook
P. 82

Vehicle()

                      {
                          move = 'M';  // Represents movement
                      }

                  };
                  class Car : public Vehicle  // Car inherits from Vehicle

                  {
                  public:

                      char honk;
                      // Derived class                                                     DOSBox 0.74, Cpu speed: max 100%

                      Car()                                                            Move: M
                                                                                       Honk: H
                      {

                          honk = 'H';  // Represents honking
                      }

                  };
                  void main()
                  {

                      clrscr();
                      Car myCar;

                      cout<< "Move: " << myCar.move;
                      cout<< "\nHonk: " << myCar.honk;

                      getch();
                  }
                  This program demonstrates inheritance  where  the Car class  inherits from the  Vehicle class.

                  The base class (Vehicle) has move, representing movement (‘M'), while the derived class (Car)
                  adds honk, representing honking (‘H'). The Car object accesses both properties, showing how
                  inheritance allows reusing and extending functionality.
                  Polymorphism

                  The term polymorphism is derived from two Greek words: “poly" meaning many and “morph"

                  meaning forms. In Object-Oriented Programming (OOP), polymorphism is a feature that allows
                  an object to behave differently in different situations. C++ supports polymorphism in three ways:
                  1.   Function Overloading: The process of defining multiple functions with the same name but
                      with different numbers, types, or sequences of parameters.

                  2.  Operator Overloading: The process of using an operator for multiple actions.





                   80
                          Touchpad MODULAR (Ver. 2.0)
   77   78   79   80   81   82   83   84   85   86   87