Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Руководство_по_C++_CLI.doc
Скачиваний:
4
Добавлен:
01.07.2025
Размер:
8 Мб
Скачать
☆

7.2.2Practical Learning: Introducing Inheritance

  1. Change the file as follows:  

. . .

namespace RealEstate

{

public value class CProperty

{

public:

long PropertyNumber;

String ^ Address;

String ^ City;

String ^ State;

String ^ ZIPCode;

int Bedrooms;

float Bathrooms;

int YearBuilt;

double MarketValue;

};

}

. . .

  1. Save the file

7.2.3Inheriting From a Class

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

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: