Exforsys.com
 

Sponsored Links

 

C++ Tutorials

 
Home Tutorials C++
 

C++ Inheritance

 

C++ Inheritance

Introduction

What is Inheritance?

Inheritance is the process by which new classes called derived classes are created from existing classes called base classes. The derived classes have all the features of the base class and the programmer can choose to add new features specific to the newly created derived class.



For example, a programmer can create a base class named fruit and define derived classes as mango, orange, banana, etc. Each of these derived classes, (mango, orange, banana, etc.) has all the features of the base class (fruit) with additional attributes or features specific to these newly created derived classes. Mango would have its own defined features, orange would have its own defined features, banana would have its own defined features, etc.


This concept of Inheritance leads to the concept of polymorphism.


Features or Advantages of Inheritance:

Reusability:


Inheritance helps the code to be reused in many situations. The base class is defined and once it is compiled, it need not be reworked. Using the concept of inheritance, the programmer can create as many derived classes from the base class as needed while adding specific features to each derived class as needed.


Saves Time and Effort:


The above concept of reusability achieved by inheritance saves the programmer time and effort. Since the main code written can be reused in various situations as needed.


Increases Program Structure which results in greater reliability.


Polymorphism (to be discussed in detail in later sections)


General Format for implementing the concept of Inheritance:

class derived_classname: access specifier baseclassname


For example, if the base class is exforsys and the derived class is sample it is specified as:



class sample: public exforsys



The above makes sample have access to both public and protected variables of base class exforsys. Reminder about public, private and protected access specifiers:


  • If a member or variables defined in a class is private, then they are accessible by members of the same class only and cannot be accessed from outside the class.
    .
  • Public members and variables are accessible from outside the class.
    .
  • Protected access specifier is a stage between private and public. If a member functions or variables defined in a class are protected, then they cannot be accessed from outside the class but can be accessed from the derived class.

Inheritance Example:


class exforsys
{
public:
exforsys(void) { x=0; }
void f(int n1)
{
x= n1*5;
}


void output(void) { cout<<x; }


private:
int x;
};


class sample: public exforsys
{
public:
sample(void) { s1=0; }


void f1(int n1)
{
s1=n1*10;
}


void output(void)
{
exforsys::output();
cout << s1;
}


private:
int s1;
};


int main(void)
{
sample s;
s.f(10);
s.output();
s.f1(20);
s.output();
}



The output of the above program is


50
200


In the above example, the derived class is sample and the base class is exforsys. The derived class defined above has access to all public and private variables. Derived classes cannot have access to base class constructors and destructors. The derived class would be able to add new member functions, or variables, or new constructors or new destructors. In the above example, the derived class sample has new member function f1( ) added in it. The line:



sample s;



creates a derived class object named as s. When this is created, space is allocated for the data members inherited from the base class exforsys and space is additionally allocated for the data members defined in the derived class sample.


The base class constructor exforsys is used to initialize the base class data members and the derived class constructor sample is used to initialize the data members defined in derived class.



The access specifier specified in the line:



class sample: public exforsys



Public indicates that the public data members which are inherited from the base class by the derived class sample remains public in the derived class.



Read Next: Static Functions - An Introduction



 

 

Comments


Dhiraj Sable said:

  This article is so good.
October 30, 2006, 8:03 pm

ganesh kumar said:

  abstractions methods and how we have to use the abstraction
November 19, 2006, 11:59 pm

ruban_stalin said:

  you have called the output() function twice. 4 values must have been displayed.
November 24, 2006, 12:10 am

stew said:

  you are incorrect. that is the deal with inheritance and polymorphism, the output() function is called only once each time. the compiler determines which one to execute.
December 5, 2007, 12:56 pm

ann said:

  output() function called 4 time
can explain why in secand call the value obtained zero .

December 27, 2007, 4:42 am

sunnyrulzster said:

  how did the compiler decide which output function to call base or derived class's?
May 2, 2008, 1:50 am

panid said:

  Can one class function return other class object if it is inherited?
August 14, 2008, 1:17 am

sharif uddin sk said:

  i understand this article very easily
November 10, 2008, 5:33 am

anna said:

  the output is-
50
0
50
200
November 10, 2008, 6:11 am

aditi said:

  i like to see the one program in which both constructor and destructor is using togetherly
November 26, 2008, 7:37 am

vikash said:

  above program is very nice.....
January 12, 2009, 11:07 am

sreenivasarao said:

  this program and theory is everyone understand
January 29, 2009, 12:33 am

sunpy said:

  very good!thanks!
February 9, 2009, 1:10 am

ana said:

  hello
i always read ur exforsys site and like it most.
but plz can u also give objective qns of related topics
like C,C++,java,data structure.


ana
February 10, 2009, 12:12 am

Arvind K. said:

  Please tell me

how can i access Derived class members from Base class....
/* _____ my code snippet is _____ */

class BASE
{ protected:
int x;
public:
BASE()
{ x = a+10; }
}
class DERIVED : protected BASE
{ int a;
public:
DERIVED(int i)
{ a=i; }
}

/* ______ error comes that a is not defined _____ */
May 24, 2009, 1:41 pm

Borr said:

  It is impossible to access Derived class members from Base class. There is no need to access so
July 26, 2009, 5:39 am

John sir said:

  I need your help to understand below coding style please
Code style:
Base::Base(int ,char,long):fun(int,char,long)
August 24, 2009, 3:56 pm

studyingFor Test said:

  Base::Base(int ,char,long)
> a constructor that takes in an int, char and long item types.
:fun(int,char,long)
>get the values for these items by running a method called fun that takes int char long.

fun could be a method of a base class or a higher class. should not be a method of class base.
Class base has NO meaning at this point till u put values you get from fun and construct it...after all its a constructor.

so i take it base is inherited class...hope this helps.
October 28, 2009, 2:05 pm

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape