Inheritance |
|
The ability to use already created classes is one of the strengths of C++. If you have a ready made class, you can construct a new one using the existing characteristics. Inheritance is the ability to create an object using, or based on, another. The new or inherited object is also said to derive from the other class. There are various ways you can inherit a class: using an operating system object, basing an object on an existing C++ class, or creating a class from a Microsoft .NET Framework class. To start, you should define a class. This means that it should have functionality, valuable properties, and behaviors that other classes can use with little concern as to how the class is built, but trusting that it can handle the desired assignment of a parent. Here is an example of a class called CRectangle:
Here is the source file:
To test the class, we can declare a CRectangle variable in the _tmain() function. We will also use the class' properties to make sure they respond:
This would produce: Rectangle Characteristics Length: 25.55 Height: 20.82 Perimeter: 92.74 Area: 531.951 Press any key to continue
Once you have a defined class, you can apply its behavior as the starting point of another class. The basic syntax on inheriting from a class is: [AccessLevel] __gc structORclass NewClass : AccessLevel ParentClass If you want the class to be accessed by other assemblies, you can start by specifying the optional public keyword. If you want the class to be managed, you should specify the __gc keyword. Type the keyword struct or class as the type of object you are creating. That's the structOrClass factor in our syntax. Specify the name of the class as the NewClass factor. The colon (:) is read “is based on”. It lets the compiler know that the new object gets its foundation from another object. This operator is required. The word AccessLevel specifies whether the object will use the public or private (or protected (coming soon)) level of access. The most common inheritance, which is also the most or only one we will use, is the public inheritance. If you are creating a class whose parent is a structure, you can omit this access level because a structure is public by default. Otherwise, you should specify the type of access. The ParentClass is the name of the class that the new one is based on or is inheriting from. When inheriting from another class, the new class is considered a child. It has access to the member variables of the public section(s) of the parent object. The inheriting object will not have access to the private members of the parent. As an example, we can create a CBox class based on the CRectangle class we created earlier:
The source file is used to define the class. It makes sure that the new object has access to the desired properties of the parent. This is done using the access methods of the parent class called from the new constructors:
To test the new class, we will use the _tmain() function:
This would produce: Rectangle Characteristics Length: 25.55 Height: 20.82 Perimeter: 92.74 Area: 531.951 Characteristics of the box Length: 6.55 Height: 5.25 Width: 5.75 Area: 70.2 Volume: 197.728125 Press any key to continue
In the past, we learned that the public level allows the client of a class to access any member of a public section of a class. We also learned to hide other members by declaring them in the private section, which prevents the clients of the class from accessing such variables. You can create a special access level that allows only the children or derived objects of a class to have access to certain members of the parent class. This new access level is called protected. Therefore, to create a protected section in a class, type the protected keyword followed by a colon. Anything after that keyword until another access level will abide by the rules of a protected variable or method. To allow the children of our CRectangle class to have a special permission in accessing some members of the parent class while the clients of CRectangle don't access those particular members, we can place those members in a protected section as follows:
Since only the access level of the member variables has changed, nothing needs to be done in the implementation of the CRectangle class. On the other hand, by making the member variables of the CRectangle class protected, the CBox class and its public members now have access to their parent’s protected members. This allows us the change the source file of the CBox object as follows:
As you can see, the derived class now has access to the protected member variables of the parent class and can use them to safely perform the needed calculations.
A class can be created inside of another class. This means that a class can be nested in another's body. To do this, the nested class can be created as it would be outside. Suppose you create a class as follows:
You can make such a class nested in another class. At the current standards of C++ and Managed C++, nesting a class in another class does not grant special access privileges to the nesting or parent class. Just because a class is nested does not mean that the nested class has immediate access to the members of the nesting class. Normally, this feature is supposed to change in the next standard of the languages. As of now, you must still declare a variable of the nested class in the nesting class. Here is an example:
In the same way, you can nest as many classes as you wish in another class and you can nest as many classes inside of other nested classes if you judge it necessary. To implement a method of a nested class, you can do so inline, that is, in the body of the class. If you want to implement a method of a nested class outside of the class, you must qualify it completely. Here is an example:
To declare a variable of a nested class, you must qualify it, once again, to let the compiler know where the class was created. Here is an example: // This is the main project file for VC++ application project // generated using an Application Wizard. #include "stdafx.h" #include ".\test.h" #using <mscorlib.dll> using namespace System; int _tmain() { // TODO: Please replace the sample code below with your own. COutside *parent = new COutside; COutside::CInside *ins = new COutside::CInside; Console::WriteLine(S""); return 0; } This would produce: - The Parent Class - - The Child Class - Press any key to continue
Class inheritance that involves namespaces relies on qualification, like the calling of the members of a namespace. To derive a class from a class member of a namespace, type the name of the namespace, followed by the access operator "::", and followed by the name of the base namespace. For example, imagine you had created the CRectangle object as follows:
To inherit from this class, the compiler will need to know the namespace in which CRectangle was created. Therefore, you can inherit a CBox object as follows:
If you need to call the class that was defined in a different namespace, remember to qualify its name with the scope access operator. Here is an example:
Alternatively, as mentioned in the past, to use the contents of a namespace, prior to calling a member of that namespace, you can type using namespace followed by the name of the namespace.
The most common inheritance consists of a class deriving its foundation from another class. This is referred to as single inheritance. C++ allows a class to be based on more than one class. This is referred to as multiple inheritance. When a class is based on many other classes, it benefits
from the member variables and methods of those classes. It has direct access to the public
and protected members of each parent class.
In most cases you should avoid multiple inheritance. As an example, Managed C++ supports multiple inheritance only in the issue of interface that we will learn soon.
|
|
||
Previous | Copyright © 2004 FunctionX, Inc. | Next |
|