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

Int Bedrooms;

double Bathrooms;

Int YearBuilt;

double Value;

};

Only the classes derived can access the protected members of a class.

7.2.6Namespaces and Inheritance

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

{

};

7.3The Object Class

7.3.1 Introduction

To assist you with starting a program, the C++/CLI language, along with the .NET Framework, ship with many created classes. You can use most of these classes directly in your program, especially those classes that are part of the System namespace. Many other classes belong to namespaces that are part of, or nested in, the System namespace. We will study other classes in future lessons.

7.3.2An Object as a Handle

The most fundamental class of the C++/CLI language and the .NET Framework is called Object. You can declare an Object handle using the formulas we have reviewed in this and the previous lesson. Here is an example:

using namespace System;

Int main()

{

Object ^ home;

return 0;

}

After creating an Object handle, you can initialize it with just about any value. Here are examples:

using namespace System;

Int main()

{

Object ^ Integer = 97834;

Object ^ Real = 274.55;

Object ^ Character = L'G';

return 0;

}

Notice that the first variable is initialized with a natural number, the second is initialized with a decimal number, and the third is initialized with a character. Because the value of an Object variable can be vague (неопределенным), use it only if you don't know or cannot find out what type of value you want to use. Since the other data types we reviewed in the previous lessons are more precise and we now know how to create a class, use those types and techniques instead.

Just as you can declare a member variable of a class as a regular data type, you can use the Object class to create a member of a class.

You can also declare a member variable that is of type Object.

7.3.3Inheriting from the Object Class

All managed classes that you create using either the value of the ref keywords are derived from the Object class. If you want, you can reinforce this by explicitly inheriting from Object. Here is an example:

public ref class Classification : public Object

{

public:

long PropertyNumber;

int Condition;

};

7.3.4Practical Learning: Inheriting From the Object Class

  1. To inherit from the Object class, change the file as follows:

using namespace System;

namespace RealEstate

{

public ref class CProperty : public Object

{

public:

long PropertyNumber;

String ^ Address;

String ^ City;

String ^ State;

String ^ ZIPCode;

int Bedrooms;

float Bathrooms;

int YearBuilt;

double MarketValue;

};

public ref class CHouse : public CProperty

{

public:

Byte Stories;

int Condition;

__wchar_t Style;

};

}

. . .

  1. Execute the application to see the result