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 a class using, or based on, another. The new or inherited class is also said to derive, or be derived, from the other class. To start with inheritance, you should define a class. This means that it should have functionality, characteristics, and behaviors that other classes can use with little concerns as to how the class was built, but trusting that it can handle the desired assignment of a parent. To When creating the class, it must be made with ref instead of value. Here is an example: public ref class CProperty { public: int Bedrooms; double Bathrooms; int YearBuilt; double Value; };
Once you have a class, you can apply its behavior as the starting point of another object. The basic syntax on inheriting from a class is: AccessType ref structORclass ClassName : AccessLevel ParentClass The optional AccessType can be public or private as we saw in Lesson 1. The ref keyword is required (if you are creating a managed class). The word structORclass will be replaced by struct or class depending on the class type you are trying to create. The ClassName element represents the name of the class you are creating. The colon (:) is read "is based on". It lets the compiler know that the new class gets its foundation from another class. The word AccessLevel specifies whether the class will use the public or private level of access. If you are creating a managed class, this must be specified as public. The ParentClass is the name of the class that the new class is based on or is inheriting from. When inheriting from another class, the new class is considered a child. It has access to the public member(s) of the parent class. The inheriting class will not have access to the private members of the parent class. Here is an example: public ref class CProperty { public: int Bedrooms; double Bathrooms; int YearBuilt; double Value; }; public ref class CHouse : public CProperty { }; In the body of the child class, you can define the new members as you see fit. You can create the class' own public and private members. Each member of the child class would have access to all the members of the child class and the public members of the parent class. To use the class in a function such as main(), first declare a handle to the class, access its public members and the public members of its base class. Here is an example: using namespace System; public ref class CProperty { public: int Bedrooms; double Bathrooms; int YearBuilt; double Value; }; public value class Classification { public: long PropertyNumber; int Condition; }; public ref class CHouse : public CProperty { public: Classification Class; Byte Stories; int GarageCanAccomodate; }; int main() { CHouse ^ Townhouse = gcnew CHouse; Townhouse->Class.PropertyNumber = 697234; Townhouse->Class.Condition = 3; Townhouse->Stories = 2; Townhouse->Bedrooms = 2; Townhouse->Bathrooms = 1.5; Townhouse->YearBuilt = 1977; Townhouse->Value = 388450; Console::WriteLine(L"=//= Real Estate - Catalog =//="); Console::WriteLine(L"-- Property Type: Townhouse --"); Console::Write(L"Property #: "); Console::WriteLine(Townhouse->Class.PropertyNumber); Console::Write(L"Condition: "); Console::WriteLine(Townhouse->Class.Condition); Console::Write(L"Number of Bedrooms: "); Console::WriteLine(Townhouse->Bedrooms); Console::Write(L"Number of Levels: "); Console::WriteLine(Townhouse->Stories); Console::Write(L"Number of Bathrooms: "); Console::WriteLine(Townhouse->Bathrooms); Console::Write(L"Year Built: "); Console::WriteLine(Townhouse->YearBuilt); Console::Write(L"Property Value: "); Console::WriteLine(Townhouse->Value); Console::WriteLine(); return 0; } This would produce: =//= Real Estate - Catalog =//= -- Property Type: Townhouse -- Property #: 697234 Condition: 3 Number of Bedrooms: 2 Number of Levels: 2 Number of Bathrooms: 1.5 Year Built: 1977 Property Value: 388450 Press any key to continue . . . In the same way, you can inherit one class from another class that itself was inherited from another class. Also, you can inherit different classes from a common class. Here are examples: public ref class CProperty { public: int Bedrooms; double Bathrooms; int YearBuilt; double Value; }; public value class Classification { public: long PropertyNumber; int Condition; }; public ref class CHouse : public CProperty { }; public ref class CSingleFamily : public CHouse { }; public ref class CTownhouse : public CHouse { }; public ref class CCondominium : public CProperty { };
In previous sections, we learned that the public level allows the client of a class to access any member of the public section of the class. We also learned to hide other members by declaring them as private, which prevents the clients of class from accessing such variables. You can create a special access level that allows only the children of a class to have access to certain members of the parent class. This new access level is called protected and created with that keyword. To allow the children of a class to have a special permission in accessing some members of the parent class, you can place those members in a protected section. Here is an example: public ref class CProperty { protected: int Bedrooms; double Bathrooms; int YearBuilt; double Value; }; Only the classes derived can access the protected members of a class.
You can inherit a class that belongs to a namespace. To do this, type the name of the namespace, followed by the :: operator, and followed by the name of the base namespace. Here is an example: using namespace System; namespace RealEstate { public ref class CProperty { }; } public ref class CHouse : public RealEstate::CProperty { }; public ref class CCondominium : public RealEstate::CProperty { }; |
|
||
Previous | Copyright © 2006-2007 FunctionX, Inc. | Next |
|