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

7.1.6Practical Learning: Using Strings as Fields

  1. Change the Exercise.cpp source file as follows:  

using namespace System;

namespace RealEstate

{

public value class CClassification

{

public:

long PropertyNumber;

int Condition;

__wchar_t Style;

};

public value class CProperty

{

public:

CClassification ^ Class;

__wchar_t TypeOfHome;

String ^ Address;

String ^ City;

String ^ State;

String ^ ZIPCode;

int Bedrooms;

float Bathrooms;

Byte Stories;

int YearBuilt;

double MarketValue;

};

}

int main()

{

String ^ strTitle1 = L"=//= Altair Realty =//=";

String ^ strTitle2 = L"-=- Properties Inventory -=-";

RealEstate::CProperty ^ condo = gcnew RealEstate::CProperty;

RealEstate::CClassification ^ type = gcnew

RealEstate::CClassification;

type->PropertyNumber = 697234; // Unique Number

condo->Address = L"6808 Lilas Drive";

condo->City = L"Silver Spring";

condo->State = L"MD";

condo->ZIPCode = L"20904";

type->Condition = 2; // Good, may need minor repair

type->Style = L'H'; // Highrise

condo->Class = type;

condo->YearBuilt = 2002;

condo->Bathrooms = 1;

condo->Stories = 18;

condo->MarketValue = 155825;

condo->Bedrooms = 2;

condo->TypeOfHome = L'C';

Console::WriteLine(strTitle1);

Console::WriteLine(strTitle2);

Console::Write("Property #: ");

Console::WriteLine(condo->Class->PropertyNumber);

Console::Write("Address: ");

Console::WriteLine(condo->Address);

Console::Write(" ");

Console::Write(condo->City);

Console::Write(", ");

Console::Write(condo->State);

Console::Write(" ");

Console::WriteLine(condo->ZIPCode);

Console::Write("Condition: ");

Console::WriteLine(condo->Class->Condition);

Console::Write("Style: ");

Console::WriteLine(condo->Class->Style);

Console::Write("Type of Home: ");

Console::WriteLine(condo->TypeOfHome);

Console::Write("Bedrooms: ");

Console::WriteLine(condo->Bedrooms);

Console::Write("Bathrooms: ");

Console::WriteLine(condo->Bathrooms);

Console::Write("Stories: ");

Console::WriteLine(condo->Stories);

Console::Write("Year Built: ");

Console::WriteLine(condo->YearBuilt);

Console::Write("Market Value: ");

Console::WriteLine(condo->MarketValue);

Console::WriteLine();

return 0;

}

  1. Execute the application to see the result  

=//= Altair Realty =//=

-=- Properties Inventory -=-

Property #: 697234

Address: 6808 Lilas Drive

Silver Spring, MD 20904

Condition: 2

Style: H

Type of Home: C

Bedrooms: 2

Bathrooms: 1

Stories: 18

Year Built: 2002

Market Value: 155825

  1. Close the DOS window

7.2Inheritance

7.2.1 Introduction

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;

};