Fun with C plus plus part 3-Inheritance

1. Introduction

In my previous blog, we discussed about the concept of classes. We’ll be taking up inheritance in this section. As usual, this will be done in an easy to understand manner ☺️ .

2. Inheritance

What comes to your mind when you think of family inheritance? Children acquiring what belongs to parents, materialistically. And if you think genetically, kids naturally inheriting the features and qualities of parents. Simple, isn’t it ☺️ . 

Classes are no different. Parents represent Base Class. Kids are Derived Class. Kids inherit the features of the parent.

3. Representation

For the parent/kid example, lets define the classes as

Class parentBase


protected: // protected members are //accessible by derived class

int age;

char parentName[NAMESIZE];


parentBase()// constructor


//setting parent name as GOD  🙂

strcpy (parentName, “GOD”); 


}; //Parent class definition over

class kidDerived:public parentBase 


char kidName[NAMESIZE];


void setAge(int kidAge)


age = kidAge;


void showParentName()


cout<<“parent name is: “<<parentName<<endl;


void showAge()


cout <<” kid’s age is : ” <<age<<endl;


}; //derived class definition over

And now, lets define the  the main() function

int main(void)


kidDerived objKid;




return 0;


In the above example, we create an object of derived class, kidDerived . 

Derived class has access to protected members of the base class. The functions of derived class therefore can modify the protected variables of the base class, namely age and parentName .

Public members can be accessed by objects, therefore functions showParentName(), setAge(), and showAge() are accessible by object objKid.

4. Access Specifiers

Access specifiers define the scope of access and visibility of members of base class.

  1. Public: public members of a class are accessible by all. Derived classes, and objects of derived classes can access the public members of base class.
  2. Protected: protected members of base class can be accessed by it’s derived class only. Objects of derived class cannot access protected members of base class.
  3. Private: private members are inaccessible to all i.e. derived class and objects of derived class cannot access private members of base class.


    Friends, I hope I was successful in raising the curtain from inheritance. I am sure you feel complacent with it now 😀 . In our next blog, we’ll be taking up another example of inheritance, to derive classes for various types of social media ☺️ .


    1 thought on “        Fun with C plus plus part 3-Inheritance

    Leave a Reply

    Fill in your details below or click an icon to log in: Logo

    You are commenting using your account. Log Out /  Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out /  Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out /  Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out /  Change )


    Connecting to %s

    %d bloggers like this:
    search previous next tag category expand menu location phone mail time cart zoom edit close