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

Int main()

{

typedef double Salary;

Salary Monthly;

Salary Hourly, Weekly;

double WeeklyHours;

double *PHourly;

Hourly = 12.55;

WeeklyHours = 42.50;

PHourly = &Hourly;

Weekly = Hourly * WeeklyHours;

Console::WriteLine("Payroll Preparation");

Console::WriteLine("Enter Employee's Weekly Hours: ");

Console::WriteLine("Employee Payroll");

Console::Write("Weekly Hours: ");

Console::WriteLine(WeeklyHours);

Console::Write("Hourly Salary: ");

Console::WriteLine(*PHourly);

Console::Write("Weekly Salary: ");

Console::WriteLine(Weekly);

Console::Write("Monthly Salary: ");

Console::WriteLine(Monthly);

return 0;

}

This would produce:

Payroll Preparation

Enter Employee's Weekly Hours:

Employee Payroll

Weekly Hours: 42.5

Hourly Salary: 12.55

Weekly Salary: 533.375

Monthly Salary: 0

Notice that the Monthly variable is still holding 0 and it has not been used throughout the program.

4.3.2The new Operator

Instead of letting the compiler provide memory when your program comes up, you can ask it to use memory only when necessary. In that case, the compiler would allocate memory when the program is executing. This is called dynamic allocation. Dynamic allocation allows you to use memory "as needed" and not "as required".

To dynamically assign memory, the C++ language provides the new operator. Here is the syntax to use it:

PointerName = new DataType;

The PointerName must be a declared pointer. You could have declared it previously or you can declare it when performing memory allocation.

The assignment operator "=" and the new keyword are required. They let the compiler know that you are about to request memory.

The DataType parameter can be any of those we are already familiar with, but it must be the same as the variable it is pointing to. This means that, if PointerName points to an integer variable, the data type must be an integer:

using namespace System;

Int main()

{

typedef double Salary;

Salary Monthly;

Salary Hourly, Weekly;

double WeeklyHours;

double *PHourly;

Hourly = 12.55;

WeeklyHours = 42.50;

PHourly = &Hourly;

*PHourly = 14.25;

Weekly = Hourly * WeeklyHours;

Console::WriteLine("Payroll Preparation");

Console::WriteLine("Employee Payroll");

Console::Write("Weekly Hours: ");

Console::WriteLine(WeeklyHours);

Console::Write("Hourly Salary: ");

Console::WriteLine(*PHourly);

Console::Write("Weekly Salary: ");

Console::WriteLine(Weekly);

Console::Write("Monthly Salary: ");

Console::WriteLine(Monthly);

PHourly = new double;

return 0;

}

Whenever you (decide to) dynamically allocate memory to a pointer variable, if the pointer had been previously initialized, the value that was stored in that pointer is lost (obsolete). Furthermore, if the pointer was pointing to an already declared variable, the relationship is broken: the pointer would not point to that variable anymore. In other words, both the pointer and the variable it was pointing to do not hold the same value anymore. The pointed to variable keeps its value because it is independent from the pointer (it is the pointer that is pointing to the variable and not vice-versa).