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

{
                 private:

                     char name;
                     int age;
                                                                                          DOSBox 0.74, Cpu speed: max 100%
                 public:
                                                                                      Name: R
                     char info;
                     Student()

                     {
                         name = 'R';
                         age = 20;
                         info = name;
                     }

                 };
                 void main()
                 {
                     clrscr();
                     Student s;

                     cout<< "Name: " << s.info;
                     getch();
                 }
                 The program demonstrates encapsulation by keeping name and age private while exposing only
                 info. The constructor initialises name as ‘R' and assigns it to info, allowing controlled access. The

                 main() function creates a Student object and displays info, ensuring data security.
                 Inheritance

                 Similar to genetic inheritance, C++ also provides a feature called inheritance. It is one of the most
                 important features of object-oriented programming that allows a class to acquire or inherits
                 all the properties and behaviors of its parent class. The class which inherits the members of
                 another class is called derived class or sub class. On the other hand, the class whose members
                 are inherited is called base class or super class. Let's create a program to understand the concept
                 better.

                 Program 4: Demonstrating Inheritance using a Vehicle Class.
                 #include<iostream.h>
                 #include<conio.h>

                 class Vehicle
                 {
                 public:
                     char move;

                     // Base class


                                                                                                                  79
                                                                                                 OOP Concepts
   76   77   78   79   80   81   82   83   84   85   86