home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
[an error occurred while processing this directive]
Previous Table of Contents Next


After this is done, the programmer has access to all member variables and functions of both classes.

If required, the programmer can call functions from both the base and derived class, thus extending the capability of the program.

// Inheritance
#include <iostream.h>

class Beta    {
                protected:
                    int Val1,Val2;
                    int total;
                public:
                    void Calc(void);
                    void Show(void);
                    Beta(int x, int y);
            };

class Alpha : public Beta{
                protected:
                    int Val3;
                    int Newtotal;

                public:
                    void CalcII(void);
                    void ShowII(void);
                    Alpha(int z,int x,int y);
                };

void Beta::Calc(void)
{
    total = Val1 * Val2;
}

void Beta::Show(void)
{
    cout << “The total is “ << total << endl;
}

Beta::Beta(int x, int y)
{
    Val1 = x;
    Val2 = y;
}

void Alpha::CalcII(void)
{
    Newtotal = Val1 * Val2 * Val3;
}

void Alpha::ShowII(void)
{
cout << “The newtotal is “ << Newtotal << endl;
}
Alpha::Alpha(int z, int x, int y):Beta(x,y)
{
    Val3 = z;
}

void main(void)
{
    int x,y,z;
    cout << “Enter a Number “;
    cin >> x;
    cout << endl << “Enter another Number “;
    cin >>y;
       cout << endl << “Enter a third Number “;
       cin >> z;

    Alpha First(z,x,y);
    First.Calc();     //Base Class
       First.CalcII();   //Derived Class
    First.Show();     //Base Class
       First.ShowII();   //Derived Class
}

Comments

The most common problem beginning developers encounter is forgetting to set the data members of a base class as protected. If this is not done, developers have difficulty later when trying to inherit from the class. Another common mistake is forgetting to add the extra call to the base constructor at the end of the header for the derived constructor. Because of these mistakes, many C++ programmers end up redefining data members and member functions in a derived class that already exists in the base class. By doing so, they miss the whole point of being able to inherit something that already exists.

5.8 Pass parameters back through the C++ inheritance mechanism to parent classes?

Problem

One of the most difficult things encountered by C++ programmers is the construction of a coherent inheritance model. They find constructors an especially difficult practical exercise and become terribly confused by parameters and where they are eventually stored.

Technique

The sample program later in this How-To creates a simple OOP program that creates a base class named Triangle and a child class named Prism. Triangles are two-dimensional objects with three sides. Prisms have three dimensions and have triangular side profiles; thus a prism is a triangle with depth (see Figure 5.19). The basic characteristics of a triangle apply to a prism, so those properties can be inherited and extended to create a prism. The ability to extend existing characteristics is the fundamental feature of inheritance.


Figure 5.19  The relationship between a triangle and a prism.

Steps

1.  The Triangle class contains three protected member variables named base, height, and area, respectively. As their names imply, these member variables will store the base and height dimensions of a triangle. base and height can be used to calculate the area of the triangle.
protected:
    double base;
    double height;
    double area;
2.  The constructor is used to initialize the base and height data members when a triangle object is created.
Triangle(double b, double h)
{
    base = b;
    height = h;
}

This is shown in Figure 5.20.


Figure 5.20  The constructor passes data to the protected member variables within the Triangle class.

3.  As a side issue, if you are writing classes that will be inherited, declare your data members as protected if you want them to be used by the derived classes.
4.  The Triangle class contains two public member functions. The first is CalcArea, which is used to calculate the area of the triangle. It also stores the result in the protected member variable area.
void CalcArea(void)
{
    area = (base * height) / 2;
}
5.  The second method, ShowArea, is used to print the area to the screen.
void ShowArea(void)
{
    cout << “The area is : “ << area << endl;
}
6.  There is nothing too dramatic about those two member functions. The Triangle class could be used on its own to calculate the areas of triangles by simply instantiating an object and using the CalcArea() and ShowArea() member functions. The apparent confusion comes when you inherit those basic characteristics and write the Prism class. Many beginners find it difficult to grasp the concept that although the member variables base, height, and area, and the member function ShowArea are declared within the Triangle class, they are freely available to use in any Prism object.
7.  The class has only one protected member variable named depth. However, because of inheritance, it also has copies of the member variables base and height available to it. The constructor for Prism has to deal with the instantiation of these member variable when it creates a Prism object. It is done with the following code fragment:
Prism(double d, double b, double h):Triangle(b,h)
{
    depth = d;
}
8.  The Prism constructor accepts three data items. It knows what to do with the first one d, but has no instructions about the second two, b and h. Because the Prism constructor doesn’t know what to do with them, it passes them to the Triangle constructor, which is attached to the end of the Prism constructor definition (see Figure 5.21). The Prism constructor is satisfied and couldn’t care less what the Triangle constructor does with the two spare values. If the Triangle constructor can deal with the two values, it will do so (and in fact does in our case). If not, it will flag a compilation error.
9.  Here is an alternative way of looking at the construction of an inherited object (see Figure 5.22).


Figure 5.21  The Prism constructor passes data its own member variable depth, but passes the remaining data to the protected data members within the Triangle class.


Figure 5.22  An alternative view of the Prism constructor passing its own member variable depth, but passing the remaining data to the protected member variables within the Triangle class.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.