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