Broad Network


Inheritance in C++ Classes

Object Oriented Programming in C++ – Part 4

Forward: Inheritance is the ability to define new classes using existing classes as a basis. In explain that in this part of the series.

By: Chrysanthus Date Published: 23 Aug 2012

Introduction

This is part 4 of my series, Object Oriented Programming in C++. Inheritance is the ability to define new classes using existing classes as a basis. I explain that in this part of the series.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

New Members
Remember, a member is a property or a method. You can have a class with its members. Then you want a new class that will have those same members and new members. Are you going to describe (created) a new class retyping the same old members of the existing class plus new members? C++ exists in such a way that you can have a class with its members and then a new related class with the same members as those of the existing class and with its own new members. So, if you want a class that simply has extra members in addition to what an existing class has, you inherit (see below) it from the existing class adding the new members.

Example
The following code shows a class with two properties and one method. The method adds the values of the two properties:

#include <iostream>
using namespace std;

class Calculator
    {
        public:
        int num1;
        int num2;

        Calculator(int ident1, int ident2)
            {
                num1 = ident1;
                num2 = ident2;
            }

        int add ()
            {
                int sum = num1 + num2;
                return sum;
            }
    };


int main()
    {
        Calculator myObject(6,7);
        int result = myObject.add();
        cout << result;

        return 0;
    }

Imagine that you want a class that would square a sum (a sum is the addition of two values) and add a fixed value (say 5) to the square. We already have a class that does summing of two values. So, we can derive a class from this existing class. The derived class is the inherited class. It will have an additional property, which holds the fixed value (5). It will have an additional method that squares the sum and adds the fixed value. It inherits the two properties and the add() method of the existing class. The syntax to derive one class from another is:

    class derivedClassName: public baseClassName
        {
            //new members
        };

You begin with the keyword, class. This is followed by the name of the derived (inherited) class. Then you have a colon. Next you have the keyword, public. This word can be replaced by two other words (see later). Then you have the name of the existing class. The existing class is called the base class. We say the derived class is inherited from the base class. After the base class name is typed above, you have to describe (code) the derived class (new properties and methods) within curly braces. The following code shows how you derive a class using the above-mentioned base and derived class outline:

#include <iostream>
using namespace std;

class Calculator
    {
        public:
        int num1;
        int num2;

        int add()
            {
                int sum = num1 + num2;
                return sum;
            }
    };

class ChildCalculator: public Calculator
    {
        public:
        int fixedVal;

        int square(int answer)
            {
                int finalVal = answer * answer + fixedVal;
                return finalVal;
            }
    };


int main()
    {
        ChildCalculator myChildObj;
        myChildObj.num1 = 2;
        myChildObj.num2 = 3;
        int result = myChildObj.add();
        myChildObj.fixedVal = 5;
        int endResult = myChildObj.square(result);
        cout << endResult;

        return 0;
    }

The base class calculator has two properties and one method. The derived class has one property and one method. The value that will be assigned to the property of the derived class is considered as the fixed value. The method of the derived class, squares its argument and then adds the value of its property to the square.

Now, in the main function: The first line instantiates a derived object from the corresponding derived class. In this code, no object has been instantiated from the base class; that is not necessary as the derived class inherits all the members of the base class. The next statement in the main function assigns a value to one of the inherited attributes of the derived object (class). The following statement does a similar thing. The fourth statement, which comes after, calls the inherited add() method of the derived object and the values of the inherited properties are summed. The return value of the inherited add() method is assigned to the fundamental object, result.

The fifth statement assigns a fixed value of 5 to the only property that belongs to the derived object (class). The statement after, calls the square() method that belongs sorely to the derived object (class), sending the returned value (result) of the inherited method as argument. The square method squares the sum (result) and adds the fixed value; so strictly speaking, this method should not really be given the name, square. The returned value of the square() method is displayed by the cout statement, next.

So a derived class has inherited members that it can use. It can also have its own new members. A derived object is instantiated from the derived class. A base object is instantiated from the base class. The instantiated derived class and the instantiated base class are normally independent.

You can still derive a class (and corresponding object) from a derived class to have a grandchild. In this case the former child class becomes the base class and the new child class becomes the derived class. The chain can grow downward.

What is inherited from Base Class?
The members (attributes and methods) of base class are inherited.

What is not inherited from Base Class?
- The constructor function and the destructor function are not inherited from the base class. However, the default constructor and default destructor for the derived class are there, as they would exist for any class, be it base class or derived class.
- The operators you defined for the base class are not inherited. However, the default assignment (=) operator for the derived class is there. I have not defined any operator in this tutorial series.
- Friends (see later) are not inherited.

That is it for this part of the series; we continue in the next part.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message