Receive all updates via Facebook. Just Click the Like Button Below

Home >> Software >> C++ >> Inheritance in C++

Question:

What is a base class? Explain with an example using C++.

Answer

Inheritance is one of the important features of OOP which allows us to make hierarchical classifications of classes. In this, we can create a general class which defines the most common features. Other more specific classes can inherit this class to define those features that are unique to them. In this case, the class from which other classes are inherited is referred as base class.

For example, a general class vehicle can be inherited by more specific classes car and bike. The class vehicle is base class in this case.

class Base
{
int a;
public:
Base()
{
a = 1;
cout <<”inside Base class”;
}
};

class Derived:: public Base //class Derived is inheriting class Base publically
{
int b;
public:
Derived()
{
b = 1;
cout <<”inside Derived class”;
}
};


Share this question

0 Responses to What is a base class? Explain with an example using C++.


    Leave a Reply

    Your email address will not be published. Required fields are marked *




    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

    Enter the sum of these numbers: 3 + 5 =