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

Int main()

{

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

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

CSingleFamily ^ house = CreateListing();

system("cls");

Console::WriteLine(strTitle1);

Console::WriteLine(strTitle2);

Console::WriteLine("=//= Property Listing =//=");

Console::WriteLine("Property #: {0}", house->PropertyNumber);

Console::WriteLine("Address: {0}\n {1}, {2}

{3}", house->Address, house->City,

house->State, house->ZIPCode);

Console::WriteLine("Condition: {0}", house->Condition);

Console::WriteLine("Style: {0}", house->Style);

Console::WriteLine("Bedrooms: {0}", house->Bedrooms);

Console::WriteLine("Bathrooms: {0}", house->Bathrooms);

Console::WriteLine("Stories: {0}", house->Stories);

Console::WriteLine("Year Built: {0}", house->YearBuilt);

Console::WriteLine("Garage Spaces: {0} SqFt",

house->NumberOfGarageSpaces);

Console::WriteLine("Lot Size: {0:F}",

house->LotSizeInSqFt);

Console::WriteLine("Market Value: {0:C}", house->MarketValue);

Console::WriteLine();

return 0;

}

  1. Execute the application to test it

  2. Close the DOS window

11.2Passing a Class Type

11.2.1 Passing a Tracking Reference

Like a regular data type, a class can be passed to a function as argument. You can pass an argument as a tracking reference. To do this, in the parentheses of the function, type the name of the class, followed by the % operator, followed by a name for the argument. This would be done as follows:

Void DescribeProperty(cHouse % home);

In the body of the function, you can either ignore the argument and not use it at all, or you can process or use the argument as you see fit. At a minimum, you can retrieve the values of its member variables, using the period operator. As mentioned for the regular types, when (only) declaring a function that takes a class type as argument, you can omit the name of the argument.

When calling the function, pass the argument with the * operator. Here is an example:

using namespace System;

public value class CCar

{

public:

String ^ Make;

String ^ Model;

int Doors;

int Year;

double Price;

};

CCar % Build();

void Show(CCar % car);

int main()

{

Console::WriteLine(L"Enter Car Information");

CCar ^ vehicle = Build();

Console::WriteLine(L"\nCar Characteristics");

Show(*vehicle);

Console::WriteLine();

return 0;

}

CCar % Build()

{

CCar car;

Console::Write(L"Enter Make: ");

car.Make = Console::ReadLine();

Console::Write(L"Enter Model: ");

car.Model = Console::ReadLine();

Console::Write(L"# of Doors: ");

car.Doors = int::Parse(Console::ReadLine());

Console::Write(L"Year Made: ");

car.Year = int::Parse(Console::ReadLine());

Console::Write(L"Enter Price: ");

car.Price = double::Parse(Console::ReadLine());

CCar % veh = car;

return veh;

}

Void Show(cCar % car)

{

Console::WriteLine(L"Make: {0}", car.Make);

Console::WriteLine(L"Model: {0}", car.Model);

Console::WriteLine(L"Doors: {0}", car.Doors);

Console::WriteLine(L"Year: {0}", car.Year);

Console::WriteLine(L"Value: {0:C}", car.Price);

}

In the same way, you can pass as many tracking references as you want to a function.

If a function doesn't modify an argument, you can pass it as a constant. This also applies to tracking references. Here is an example:

using namespace System;

public value class CCar

{

public:

String ^ Make;

String ^ Model;

int Doors;

int Year;

double Price;

};

CCar % Build();

void Show(const CCar % car);

int main()

{

Console::WriteLine(L"Enter Car Information");

CCar ^ vehicle = Build();

Console::WriteLine(L"\nCar Characteristics");

Show(*vehicle);

Console::WriteLine();

return 0;

}

CCar % Build()

{

CCar car;

. . .

CCar % veh = car;

return veh;

}

void Show(const CCar % car)

{

. . .

}