Wednesday, March 28, 2018

Write a C++ program to illustrate Single Inheritance.


AIM: Write a C++ program to illustrate Single Inheritance.
THEORY:
Single inheritance enables a derived class to inherit properties and behavior from a single parent class. It allows a derived class to inherit the properties and behavior of a base class, thus enabling code reusability as well as adding new features to the existing code. This makes the code much more elegant and less repetitive. Inheritance is one of the key features of object-oriented programming (OOP).

Single inheritance is safer than multiple inheritance if it is approached in the right way. It also enables a derived class to call the parent class implementation for a specific method if this method is overridden in the derived class or the parent class constructor.
The inheritance concept is used in many programming languages, including C++, Java, PHP, C#, and Visual Basic. To implement inheritance, C++ uses the ":" operator, while Java and PHP use the "extend" keyword, and Visual Basic uses the keyword "inherits." Java and C# enable single inheritance only, while other languages like C++ support multiple inheritance.
SOURCE CODE:
#include<iostream>
using namespace std;
class A
{
            protected:
            char name[10];
            int age;
};
class B:public A
{
            public:
            int h,w;
            void get_data()
            {
                        cout<<"\n enter name and age:";
                        cin>>name>>age;
                        cout<<"\n enter weight and height: ";
                        cin>>w>>h;
                        }         
                        void show()
                        {
                                    cout<<name<<endl<<age<<endl<<w<<endl<<h<<endl;
                        }
};
int main()
{
            B C;
            C.get_data();
            C.show();
}

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home