ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Inheritance in C++ Programming Language

Updated on February 19, 2017

Inheritance

Inheritance is one of the major concept in Object Oriented Programming world.Inheritance allows reuse and sharing of code in true sense.This helps the developer to save lot of time and effort.

Inheritance is probably the most powerful feature of OOPS.It is the process of creating the new class called Derived class from existing class called Base class.The derived class inherits all the features of base class but also it can add more beautiful features and improvements of its own.The base class is unchanged by this process.The main advantage of inheritance is used to get re-usability i.e code re-usability.Revising existing code saves the time and money.

Reusability and Extensibility are the two most important features of Object oriented programming paradigm.It is always nice if we can reuse something that already exists instead of creating it fresh.

Base & Derived Classes

The base class is also called Ancestor class,Parent class or Super class.

A class which can be derived from more than one classes, which means it can inherit data and functions from multiple base classes is called derived class.

The class which is inherited from the base class or parent class is called Derived class,Descendant class or Sub class.

A derived class may itself be a base class from which additional classes can be derived.There is no specific limit on the number of classes that may be derived from one another,which forms a class hierarchy.

What happens when Classes are inherited?

  1. All non private variables and methods of base class/classes become part of the derived class.
  2. The variables and methods thus inherited are not required to be defined unless you want to override their behaviour or data.

Access Specifiers

Access Specifiers are the access control rules or the visibility modes.There are three types of access specifiers-:

  • Public Access Specifiers
  • Protected Access Specifiers
  • Private Access Specifiers

The access specifiers are used when we are inheriting the class.The access specifiers is optional and can be public,private or protected.Default access specifiers is private.The role of the access specifier is described below-:

  • When public access specifier is used,the public members of the base class remain public member of the derived class. Similarly the protected members of base class remains protected member of derived class.
  • When private access specifier is used,the public and protected members of base class become private members of derived class.
  • When protected access specifier is used the, public members of base class become protected members of the derived class and protected members of the base class remain protected members of the derived class.

Access Specifier Behaviour

Access Specifier
Accessible from own class
Accesible from derived class
Accessible from outside class
public
Yes
Yes
Yes
private
Yes
Yes
No
protected
Yes
No
No

Types of Inheritance

There are many ways to inherit from classes.Here are different types

  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hybrid Inheritance
  • Hierarchical Inheritance

Single Inheritance

In this kind of Inheritance,there is one base class and one derived class.When only one class is derived from a single base class,such derivation of a class is known as Single Inheritance.Further the derived class is not used as a base class for other class derivation.

class X
{
public:
void show()
{
cout<<"Base Class";
}
};
class Y:public X
{
public:
void display()
{
cout<<"Derived Class";
}
};
void main()
{
Y t;
t.show();
t.display;
}

Output:
Base Class
Derived Class

Multiple Inheritance

When a class is derived from two or more than two base classes the derived class inherits the properties of all the base classes than this type of derivation is known as Multiple Inheritance. The derived class is not used further in defining another class.

class A
{
public:
void show()
{
cout<<"Base Class A";
}
};
class B
{
public:
void show1()
{
cout<<"Base Class B";
}
};
class C:public A,public B
{
public:
void show2()
{
cout<<"Derived Class of Base classes A and B";
}
};
void main()
{
C g;
g.show();
g.show1();
g.show2();
}

Output:
Base Class A
Base Class B
Derived Class of Base classes A and B

Multilevel Inheritance

When a class is derived from a single base class and further can be used as the base class for deriving another class.This derivation can be continued to any level.

class X
{
public:
void show()
{
cout<<"Base Class X";
}
};
class Y:public X
{
public:
void show1()
{
cout<<"Derived Class Y of Base class X";
}
};
class Z:public Y
{
public:
void show2()
{
cout<<"Derived Class Z of Base class Y";
}
};
void main()
{
Z t;
t.show();
t.show1();
t.show2();
}

Output:
Base Class X
Base Class Y
Derived Class Z of Base class Y

Hierarchical Inheritance

When more than one class is derived from the same base class.Further the derived classes are not used as base class.

class W
{
public:
void show()
{
cout<<"Base Class";
}
};
class X:public W
{
public:
void show1()
{
cout<<"Derived class X of W";
}
};
class Y:public W
{
public:
void show2()
{
cout<<"Derived class Y of W";
}
};
class Z:public W
{
public:
void show3()
{
cout<<"Derived class Z of W";
}
};
void main()
{
X g;
g.show();
g.show1();

Y t;
t.show();
t.show2();

Z r;
r.show();
r.show3();
}

Output:
Base Class 
Derived class X of W
Base Class
Derived class Y of W
Base Class
Derived class Z of W

Hybrid Inheritance

When the class is derived involving two or more forms of inheritance than this type of derivation is known as Hybrid derivation. In this we can use single,multilevel and multiple inheritance.

class X
{
public:
void show()
{
cout<<"Base Class";
}
};
class Y:public X
{
public:
void show1()
{
cout<<"Derived class Y of X";
}
};
class W
{
public:
void show2()
{
cout<<"Base class W";
}
};
class Z:public Y,public W
{
public:
void show3()
{
cout<<"Derived class Z of Y and W";
}
};
void main()
{
Z g;
g.show();
g.show1();
g.show2();
g.show3();
}

Output:
Base Class
Derived Class Y of Base Class W
Derived Class Z of Y and W

Had you ever used the feature of Inheritance

See results

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)