![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
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.
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 classs 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
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.
|
![]() |
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.
|