What is inheritance in OOPM ?

 Inheritance

One of the most useful aspects of object-oriented programming is code reusability. As the name suggests Inheritance is the process of forming a new class from an existing class that is from the existing class called as a base class, a new class is formed called as derived class.

This is a very important concept of object-oriented programming since this feature helps to reduce the code size.

To acquire or inherit or derive the properties and behavior by new class from exsiting class is 

known as inheritance. Inheritance is one of the characteristic or feature of
object-oriented programming.

Inheritance provides the capability of reuse the existing class.The new class which inherits  properties is known as derived class and existing   class from where properties are inherited is known as Base class.

Inheritance establishes the “Kind-of” relationship between Base and derived class.that is derived class is kind-of base class.For example car is kind-of vehicle,Rectangle is kind-of shape.


Syntax of inheritance in C++

class derived_class : mode_of_inheritance  Base_Class

            {           private :

                                                Data members

                        public :

                                    member functions

            };          

Mode_of_inheritance will be any one of the following

                                                public

                                                private

                                                protected

Example:

                        class rectangle : public shape

                        {           private :

                                                            Data members

                                    public :

                                                            member functions

                        };

 

#include <iostream> 

using namespace std; 

 class Account { 

   public: 

   float salary = 60000;  

 }; 

   class Programmer: public Account { 

   public: 

   float bonus = 5000;   

   };      

int main(void) { 

     Programmer p1;  

cout<<"Salary: "<<p1.salary<<endl;   

     cout<<"Bonus: "<<p1.bonus<<endl;   

    return 0; 

} 


Post a Comment

Thankyou

Previous Post Next Post