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

Program 1: To create and use a class.

                 #include<iostream.h>
                 #include<conio.h>

                 class Car
                 {
                 public:

                     int wheels;
                     int seats;
                     void getModel()

                     {
                           cout<< "Your car model is BS6";
                     }

                 };
                 void main()
                 {
                                                                                          DOSBox 0.74, Cpu speed: max 100%
                     Car c; // Creating an object of class Car
                                                                                      Wheels in your car: 4
                     c.wheels = 4;
                                                                                      Your car is a 5-seater
                     c.seats = 5;                                                     Your car model is BS6

                     clrscr();
                     cout<< "Wheels in your car: " << c.wheels << "\n";
                     cout<< "Your car is a " << c.seats << "-seater\n";
                     // Calling function of the class

                     c.getModel();
                     getch();

                 }
                    PRINCIPLES OF OOP


                 C++ follows all the  basic principles of object-oriented  programming  languages.  The  basic
                 principles of object-oriented programming are: data abstraction, encapsulation, inheritance
                 and polymorphism. Let us learn about these in detail.
                 Data Abstraction

                 Abstraction means to hide something. In programming, data abstraction refers to a process

                 of hiding the complex or unnecessary data and show only the data essential for the user. For
                 example, when you send a message on WhatsApp to your friend, you just type the message
                 and then tap on the send button. The phone confirms that the message has been sent, but the
                 internal processing of how the message is transmitted is hidden from you, as it is not relevant to
                 the user. Let's create a program to understand the concept better.


                                                                                                                  77
                                                                                                 OOP Concepts
   74   75   76   77   78   79   80   81   82   83   84