about-text

SoftOSet is the most remarkable programming site. SoftOSet provides the fresh conceptual and professional programs, articles, books, and other interesting and technical writings. SoftOSet is the most reliable programming site to learn the programs and the basics of the popular programming languages. SoftOSet is the hub of the informative and research based articles.

Inheritance in C++ Program





Inheritance is to inherts, means to use the properties of someone else. In C++ someone else means another  class and properties means the data and methods. Inheritance is technique using which you can use the code of already written class in a new class. This practice creates the easiness for the programmer and based on the reuseability.

 

If you want to see the practical example of program of inheritance you are at right place. We will not waste your precious time and show you how to implement the inheritance.

 

Inheritance in C++ Program

 

Code


#include<iostream>

using namespace std;

const int LEN=80;

class employee{

            private:

                        char name[LEN];

                        unsigned long number;

                        public:

                                    void getdata()

                                    {

                                                cout<<"\n  Enter Last Name :";cin>>name;

                                                cout<<"      Enter number  :";cin>>number;

                                    }

                                    void putdata() const

                                    {

                                                cout<<"\n    Name :"<<name;

                                                cout<<"\n    Number:"<<number;

                                    }

};

class manager :public employee{

            private:

                        char title[LEN];

                        double dues;

            public:

                        void getdata()

                        {

                                    employee::getdata();

                                    cout<<"Enter Title :";cin>>title;

                                    cout<<"Enter Golf Club Dues :";cin>>dues;

                        }

                        void putdata() const

                        {

                                    employee::putdata();

                                    cout<<"\n   Title :"<<title;

                                    cout<<"\n    Golf Club Dues :"<<dues;

                        }

};

class scientest :public employee{

            private:

                        int pubs;

                        public:

                                    void getdata()

                                    {

                                                employee::getdata();

                                                cout<<"  Enter Number of pub :";cin>>pubs;

                                    }

                                    void putdata() const

                                    {

                                                employee::putdata();

                                                cout<<"\n    Number of Publications :"<<pubs;

                                    }

};

class laborer:public employee{

           

};

int main()

{

            manager m1,m2;

            scientest s1;

            laborer l1;

            cout <<endl;

            cout<<"\nEnter Data for Manager 1 :";

            m1.getdata();

           

            cout<<"\nEnter Data for Manager 2 :";

            m2.getdata();

            cout<<"Enter Data for Scientist 1:";

            s1.getdata();

            cout<<"\nEnter Data for Labror 1:";

            l1.getdata();

            cout<<"Data on Manager 1:";

            m1.putdata();

           

            cout<<"Data on Manager 2:";

            m2.putdata();

           

            cout<<"Data on  Scientist 1:";

            s1.putdata();

           

            cout<<"Data on Labror 1:";

            l1.putdata();

            cout<<endl;

            return 0;

}

 

 

Output

When I have executed this code on my compiler it gives me the following output.

 

 

 

Inhertiance program in C++

 

How we have done


We have to store the data of manager, scientist and labor. I think that two things are common in all three there name and number, so I have make another class of employee in which I have taken the name and number form manager, scientist and laborer by just inheriting the employee class in them. Also each class has some of its additional unique properties.

Look at the diagram below, we have coded this diagram.

 

Inheritance diagram

 

Thanks for learning programming from SoftOSet.


Post a Comment

Feel Free to Comment Us

Previous Post Next Post

Recents