Click Here!
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


Comments

The example shown deals with only three combinations of data types. You could quite easily create other overloaded functions that use mixed data types. You could create as many as you like covering all possibilities of input. A more advanced way around selecting differing data types is covered within the topic of template classes in Chapter 6, “Template Classes.”

5.7 Correctly make use of inheritance?

Problem

Beginning C++ programmers often run into several problems when trying to use inherited classes. The most common mistake is forgetting to declare the inheritance from a base class. The programmer must also be aware that in order for member variables to be inherited, they must be declared as protected and not private. These errors might cause other developers using your base class to try to re-declare the member variables in the derived class because they cannot gain access to the private base class member variables. By making this error, you are including a class that is not being used.

Technique

When designing a class, it is very important to decide whether the class will be used in the future via inheritance. If it will be, you must make sure that the data members are protected and not private. Also, if you are going to allow the member functions to be used and altered, you must declare them as virtual functions.

To declare the inheritance of a class, you must use the syntax shown in Figure 5.18.


Figure 5.18  Alpha class inheriting from Beta base class.

You will be creating a new class called Alpha that inherits all the member variables and functions found in class Beta, and makes them public to the new derived class.

Also, the way you use the constructor is slightly different. Because you are inheriting from another class (the base class), Beta needs to have some information passed to it. As you know, you use a constructor to pass information into a class. The following syntax is used to call a derived class’s constructor.

Alpha::Alpha(x,y,z):Beta(x,y)

Here, you use the standard syntax of a constructor but you add a colon to the end and add a call to the base class constructor. Also, you can pass on some of the data (in this case, x and y).

Steps

1.  In this example, you are going to create a derived class called Alpha, which inherits from class Beta. Remember that Beta is the base class, so it will be designed first.
#include <iostream.h>

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

Note that the data members are protected not private. This allows them to be accessed by derived classes.
2.  The next step is to create the member function definitions for the class.
void Beta::Calc(void)
{
t    otal = Val1 * Val2;
}

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

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

These are straightforward definitions. The constructor accepts two integer inputs. The Calc function multiplies the two values together and stores the result in total.
3.  To test the class, create a main function to invoke the class.
void main(void)
{
    int x,y;
    cout << “Enter a Number “;
    cin >> x;
    cout << endl << “Enter another Number “;
    cin >>y;

    Beta First(x,y);
    First.Calc();
    First.Show();
}
4.  Now, you must design the derived class that will inherit from class Beta.
class Alpha : public Beta{
                protected:
                    int Val3;
                    int Newtotal;

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

You can see in the header line of the class declaration that you specify that the class inherits from class Beta. This makes Beta’s member functions and member variables public to it.
5.  Next, you must add member function definitions for the new derived class. Remember that you need to design only the new functions required for this new class because the functions for the base class are already written and tested. At no time should you alter the base class.
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;
}

Take special note of the constructor design. The derived class Alpha requires three inputs, but it passes on two of them to the base class Beta. Also, note that the CalcII function is using member variables Val1 and Val2, which actually belong to class Beta not class Alpha. This is allowed because the class is designed to have protected member variables. Therefore, all member variables are public within the classes.
6.  The final stage is to alter the main program to make use of the new derived class Alpha. Remember, it is the Alpha class that you will use. Alpha will be able to call member functions from the base class if required.
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
}

In this example, the user enters three integers. An instance of derived class Alpha is created and passed the three values. Alpha’s constructor passes on two of the values to base class Beta. After the data is in the correct member variable, the calculations can be performed. To prove that all member functions are available to the derived class, a call is made to each one.

How It Works

When a class is designed, the programmer must decide whether the class is to be used via inheritance. If so, the data members must be declared to be protected. After the class is designed and tested, the programmer can forget about it because she knows that the class does its job.

Because the class has been tested, if it is to be used as a base class the programmer should simply be able to use it and not worry whether it will work.

When designing a derived class, it is very important to remember to specify the name of the base class in the header of the derived class. Also, specify that the base class is public to the new derived class.

During the design of the derived class constructor, the programmer must pass on the correct number of inputs to the base class constructor. This is the correct way to pass parameters to the base 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.