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

Int main()

{

String ^ strTitle = L"=-= Nearson Electonics =-=\n"

L"******* Store Items ******";

CStoreItem ^ saleItem = gcnew CStoreItem();

Console::WriteLine(L"==/==A store item with

default values==/==");

DescribeStoreItem(saleItem, 0);

Console::WriteLine();

Console::WriteLine(L"==/==A store item completely

defined==/==");

saleItem = gcnew CStoreItem();

saleItem->ItemNumber = 513497;

saleItem->Category = L'T';

saleItem->Make = L"Uniden";

saleItem->Model = L"8x8 Packet8 Broadband Internet

Phone System";

saleItem->DiscountRate = -10;

saleItem->UnitPrice = -145.95;

DescribeStoreItem(saleItem, 0);

return 0;

}

// This function is used when an item is specified by its make and model

void DescribeStoreItem(CStoreItem ^ %item)

{

Console::WriteLine(L"Store Item Description");

Console::WriteLine(L"Item Number: {0}", item->ItemNumber);

Console::WriteLine(L"Category: {0}", item->Category);

Console::WriteLine(L"Make {0}", item->Make);

Console::WriteLine(L"Model: {0}", item->Model);

Console::WriteLine(L"Name: {0}", item->Name);

Console::WriteLine(L"Discount Rate: {0:P}", item->DiscountRate);

Console::WriteLine(L"Unit Price: {0:C}", item->UnitPrice);

}

// This function is used when an item is specified by its name

void DescribeStoreItem(CStoreItem ^ %item, const int)

{

Console::WriteLine(L"Store Item Description");

Console::WriteLine(L"Item Number: {0}", item->ItemNumber);

Console::WriteLine(L"Category: {0}", item->Category);

Console::WriteLine(L"Make {0}", item->Make);

Console::WriteLine(L"Model: {0}", item->Model);

Console::WriteLine(L"Discount Rate: {0:P}", item->DiscountRate);

Console::WriteLine(L"Unit Price: {0:C}", item->UnitPrice);

}

  1. Execute the application to test it. This would produce:  

==/==A store item with default values==/==

Store Item Description

Item Number: 0

Category:

Make

Model:

Discount Rate: 0.00 %

Unit Price: $0.00

==/==A store item completely defined==/==

Store Item Description

Item Number: 513497

Category: T

Make Uniden

Model: 8x8 Packet8 Broadband Internet Phone System

Discount Rate: -1,000.00 %

Unit Price: ($145.95)

Press any key to continue . . .

  1. Close the DOS window

14.2.3Read-Only Properties

One of the roles of a property is to allow the other members of the class or the other class and functions of an application to find out what value the property is holding. Such a property is referred to as "getter". The formula to create a property that provides the value of its member variable is:

modifier property type property_name

{

modifier type get();

}

Notice that the property is created like a namespace: it has a body and curly brackets but no parentheses like a function. In the body of the property, there is a type of method named get. This name is required. Because the property is meant to return a value, the get() method returns a data type. Because get() is a method, it must have a body delimited with curly brackets. Here is an example from our CRectangle class:

public ref class CRectangle

{

private:

double len;

double hgt;

public:

property double Length

{

double get() {}

}

};

Use the body of the get method to implement the necessary behavior of the property getter. The simplest way consists of returning its corresponding private member variable. Here is an example:

public ref class CRectangle

{

private:

double len;

public:

property double Length

{

double get() { return len; }

}

};

When a property includes only a get() method, such a property is referred to as a read-only because the outside classes and functions can only retrieve the value of the property but they cannot change it. A classic example is the area of a rectangle. There is no need or reason for outside classes or functions to modify this value. They can only retrieve it if they need it. For this reason, you can create such a method as read-only. Here is an example:

public ref class CRectangle

{

private:

double len;

double hgt;

public:

CRectangle(double l, double h);

public:

property double Length

{

double get() { return len; }

}

property double Area

{

double get()

{

return len * hgt;

}

}

};

After creating a property, you can access it like you would any member variable. You can first declare a variable of the class, a pointer to the class, or its handle, and use either the period or the arrow operator. Here is an example:

using namespace System;

public ref class CRectangle

{

private:

double len;

double hgt;

public:

CRectangle();

CRectangle(double l, double h);

public:

property double Length

{

double get() { return len; }

}

property double Perimeter

{

double get()

{

return 2 * (len + hgt);

}

}

property double Area

{

double get()

{

return len * hgt;

}

}

};

CRectangle::CRectangle()

: len(0.00), hgt(0.00)

{

}

CRectangle::CRectangle(double l, double h)

: len(l), hgt(h)

{

}

void ShowCharacteristics(CRectangle ^ %recto)

{

Console::WriteLine(L"Rectangle Characteristics");

Console::WriteLine(L"Length: {0}", recto->Length);

Console::WriteLine(L"Perimeter: {0}", recto->Perimeter);

Console::WriteLine(L"Area: {0}", recto->Area);

}