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

Int main()

{

CRectangle ^ rect = gcnew CRectangle(24.58, -22.16);

ShowCharacteristics(rect);

Console::WriteLine();

return 0;

}

This would produce:

Rectangle Characteristics

Length: 24.58

Perimeter: 4.84

Area: -544.6928

In the same way, you can create as many read-only properties as you want.

Notice that the perimeter of our rectangle appears to be less than the length. Furthermore, the area is negative. Both values don't make sense. A getter property doesn't have to directly produce the value of the member variable. It can use a mechanism to validate a value that its corresponding member variable is holding. If the value is admissible, you can use it. If the value is not acceptable, you can either reject it or provide an appropriate alternative.

14.2.4Practical Learning: Using Read-Only Properties of a Class

  1. Display the StoreItem.h header file

  2. To transform the simple properties into read-only, change the file as follows:  

#pragma once

using namespace System;

namespace ElectronicsStore

{

public ref class CStoreItem

{

public:

// An item whose characteristics are not (yet) known

CStoreItem(void);

// An item that is known by its make, model, and unit price

CStoreItem(long itmNbr, String ^ make,

String ^ model, double unitPrice);

// An item that is known by its name and unit price

CStoreItem(long itmNbr, String ^ name, double unitPrice);

// An item completely defined

CStoreItem(long itmNbr, __wchar_t category,

String ^ make, String ^ model,

double discountRate, double unitPrice);

~CStoreItem();

private:

long nbr;

__wchar_t ^ cat;

String ^ mk;

String ^ mdl;

String ^ nm;

double discount;

double price;

public:

property long ItemNumber

{

long get() { return nbr; }

}

property __wchar_t ^ Category

{

__wchar_t ^ get() { return cat; }

}

property String ^ Make

{

String ^ get() { return mk; }

}

property String ^ Model

{

String ^ get() { return mdl; }

}

property String ^ Name

{

String ^ get() { return nm; }

}

property double DiscountRate

{

double get() { return discount; }

}

property double UnitPrice

{

double get() { return price; }

}

};

}

  1. Execute the application to test it

  2. Notice that you receive many errors because of the assignments in the Exercise.cpp file

  3. Access the Exercise.cpp file and change it as follows:  

#include "StoreItem.h"

using namespace System;

using namespace ElectronicsStore;

static void DescribeStoreItem(CStoreItem ^ %);

static void DescribeStoreItem(CStoreItem ^ %, const int);

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(513497, L'T', L"Uniden",

L"8x8 Packet8 Broadband Internet Phone System",

-10, -145.95);

DescribeStoreItem(saleItem, 0);

Console::WriteLine();

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 again

  2. Close the DOS window