Inheritance in C++

Inheritance is the key concept of Object Oriented Programming. In which there is one class is parent class and others classes are inherited from this parent class. In our example have a Parent class and then two classes are inherited from that class which is child_One and Child_Two.

Source Code

#include <iostream>
using namespace std;
class Parent
{
    protected:
    float Number_1, Number_2;
    public:
    Parent(): Number_1(0.0), Number_2(0.0)
    {
    cout<<"Enter Number_1: ";
    cin>>Number_1;
    cout<<"Enter Number_2: ";
    cin>>Number_2;
    }
};
class Child_One : public Parent  
{
    public:
    float mul()
    {
    return Number_1*Number_2;
    }
};
class Child_Two : public Parent
{
    public:
    float mul()
    {
    return 2*(Number_1+Number_2);
    }
};

int main()
{
    cout<<"\n\n\t ************ INHERITANCE IN C++ ************ \n\n\n";
    cout<<"Enter Numbers for Child_One.\n";
    Child_One a;
    cout<<endl<<"\n Child_One = "<<a.mul()<<endl<<endl;
    cout<<"Enter Numbers for Child_Two.\n";
    Child_Two p;
    cout<<"\nChild_Two = "<<p.mul()<<endl;
     return 0;
}

Output of the Program


Inheritance in C++

Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment