Inheritance is a basic object oriented feature in which one class acquires and inherit the properties of another class. All the properties of the Base Class ( also known as the Parent Class or Super class ) are present in the Derived Class ( also known as the Child Class or Sub class )
Types of Inheritance :
Single Inheritance
Single inheritance is one base class and one derived class. One in which the derived class inherits the one base class either publicly, privately or protected
In this program, there are two classes father and son. The son class is derived from the father class using the public keyword. This indicates that the public members of the father class are accessible from the son class.
The father class has a public member function house() that prints the message "Have own 2BHK House." Similarly, the son class has a public member function car() that prints the message "Have own Audi Car."
In the main() function, an object o of the son class is created. Since the son class is derived from the father class, it can access the house() member function of the father class using the dot (.) operator. Hence, the house() function is called for the object o. Next, the car() member function of the son class is called using the same object o.
#include<iostream> using namespace std; //Single Inheritance in C++ Programming class father { public: void house() { cout<<"Have own 2BHK House."<<endl; } }; class son:public father { public: void car() { cout<<"Have own Audi Car."<<endl; } }; int main() { son o; o.house(); o.car(); return 0; }
Have own 2BHK House. Have own Audi Car.To download raw file Click Here
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions