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

Int main()

{

CBox ^ box = gcnew CBox;

CBox ^ one = box->Create();

one->Show();

Console::WriteLine();

return 0;

}

12.1.6Protected Methods

A public method is one that can be accessed outside of its class. A private method is one that cannot be accessed outside of its class. When using inheritance, if you want to create a method that only derived classes can access, put that method in a protected section that is preceded with protected:.

12.2Static Members of a Class

12.2.1 Static Member Variables

A class can have static member variables. To declare such a variable, precede it with the static keyword. Here is an example:

public value class CCar

{

private:

String ^ Make;

String ^ Model;

int Doors;

int Year;

double Price;

public:

void Build();

void Show();

static int Count;

};

When you declare a member variable as static and when the application starts, the compiler creates a copy of that member. This member would be maintained by the compiler while the program is running. If you declare an instance of a class, like the above vehicle variable, the static member is not part of the object: the compiler creates and maintains the static member, whether you use it or not, whether you declare a class variable or not.

In a program, if you use a class that has a static member variable, that member variable is initialize with the minimum value of its type. For example, if the member variable is of an integer type, the static member variable is initialized with 0.

As mentioned already, when a class has a static member variable, you don't need to declare an instance of that class in order to access the static member variable. When the application starts, the member variable is automatically available and you can access it when necessary. You can access it either to retrieve its value or to modify it.

To access a static member of the class outside of that class, enter its data type, followed by the name of the class, followed by the :: operator, followed by the name of the member variable. You can then, for example, retrieve the value of the static member variable. 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();

void Show();

static int Count;

};

void CCar::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());

}

Void cCar::Show()

{

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

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

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

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

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

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

}