Tutorials
C++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.
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)
class derived_classname: access specifier baseclassname
For example, if the base class is exforsys and the derived class is sample it is specified as:
|
The above makes sample have access to both public and protected variables of base class exforsys. Reminder about public, private and protected access specifiers:
|
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:
|
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:
|
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.
| This article is so good. |

| abstractions methods and how we have to use the abstraction |

| you have called the output() function twice. 4 values must have been displayed. |
| 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. |
|
output() function called 4 time can explain why in secand call the value obtained zero . |
| how did the compiler decide which output function to call base or derived class's? |
| Can one class function return other class object if it is inherited? |
| i understand this article very easily |
|
the output is- 50 0 50 200 |
| i like to see the one program in which both constructor and destructor is using togetherly |
| above program is very nice..... |
| this program and theory is everyone understand |
| very good!thanks! |
|
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 |
|
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 _____ */ |
| It is impossible to access Derived class members from Base class. There is no need to access so |
|
I need your help to understand below coding style please Code style: Base::Base(int ,char,long):fun(int,char,long) |
|
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. |