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

11.4.3Method Local Definition

There are at least two techniques you can use to implement a method. To implement a method in the class where the method is declared, use the same techniques we used to define regular functions. When a method is a class' member, it has access to the member variables of the same class; this means that you don't need to pass the variables as arguments; you can just use any of them as if it were supplied. Here is an example:

public value class CCar

{

private:

String ^ Make;

String ^ Model;

Int Doors;

Int Year;

double Price;

public:

Void Build()

{

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

Make = Console::ReadLine();

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

Model = Console::ReadLine();

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

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

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

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

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

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

}

void Show();

};

To call a method from a declared handle of a class, you can use the -> operator just as you would proceed to access a member of a class. Here is an example:

using namespace System;

public value class CCar

{

private:

String ^ Make;

String ^ Model;

int Doors;

int Year;

double Price;

public:

void Build()

{

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

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

Make = Console::ReadLine();

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

Model = Console::ReadLine();

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

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

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

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

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

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

}

};

Int main()

{

CCar ^ vehicle = gcnew CCar;

vehicle->Build();

Console::WriteLine();

return 0;

}

11.4.4Practical Learning: Implementing a Method Locally

  1. To implement a method locally, access the StoreItem.h file and create the following method:  

#pragma once

using namespace System;

namespace ElectronicsStore

{

public ref class CStoreItem

{

private:

long ItemNumber;

__wchar_t ^ Category;

String ^ Make;

String ^ Model;

double DiscountRate;

double UnitPrice;

public:

Void CreateStoreItem()

{

Console::WriteLine(L"To create a store item,

enter its information");

Console::Write(L"Item Number: ");

ItemNumber = long::Parse(Console::ReadLine());

Console::WriteLine(L"Category");

Console::WriteLine(L"A - Audio Cables");

Console::WriteLine(L"B - Batteries");

Console::WriteLine(L"C - Cell Phones and Accessories");

Console::WriteLine(L"D - Bags and Cases");

Console::WriteLine(L"H - Headphones");

Console::WriteLine(L"M - Digital Cameras");

Console::WriteLine(L"O - Cables and Connectors");

Console::WriteLine(L"P - PDAs and Accessories");

Console::WriteLine(L"T - Telephones and Accessories");

Console::WriteLine(L"S - Surge Protector");

Console::Write(L"Your Choice? ");

Category = __wchar_t::Parse(Console::ReadLine());

Console::Write(L"Make ");

Make = Console::ReadLine();

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

Model = Console::ReadLine();

Console::Write(L"Discount Applied (enter 0 if no discount): ");

DiscountRate = double::Parse(Console::ReadLine());

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

UnitPrice = double::Parse(Console::ReadLine());

}

};

}

  1. Save the file