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

Int main()

{

float s, l, w;

s = 15.25f;

l = 28.12f;

w = 10.35f;

Console::WriteLine("The area of the square is {0}", Area(s));

Console::WriteLine("\nThe area of the rectangle is {0}",

Area(l, w));

return 0;

}

Here is the result of running the program:

The rea of the square is 232.562

The area of the rectangle is 291.042

You can also declare overloaded functions as inline and/or.

10.1.2Default Arguments

Whenever a function takes an argument, that argument is required. If the calling function doesn't provide the (required) argument, the compiler would throw an error. Imagine you write a function that will be used to calculate the final price of an item after discount. The function would need the discount rate in order to perform the calculation. Such a function could look like this:

double CalculateNetPrice(double DiscountRate)

{

double OrigPrice;

Console::Write("Please enter the original price: ");

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

return OrigPrice - (OrigPrice * DiscountRate / 100);

}

Since this function expects an argument, if you do not supply it, the following program would not compile:

using namespace System;

double CalculateNetPrice(double DiscountRate)

{

double OrigPrice;

Console::Write("Please enter the original price: ");

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

return OrigPrice - (OrigPrice * DiscountRate / 100);

}

Int main()

{

double FinalPrice;

double Discount = 15; // That is 25% = 25

FinalPrice = CalculateNetPrice(Discount);

Console::WriteLine("\nAfter applying the discount");

Console::WriteLine("Final Price = {0}", FinalPrice);

return 0;

}

Here is an example of running the program:

Please enter the original price: 250.50

After applying the discount

Final Price = 212.925

Most of the time, a function such as this CalculateNetPrice() would use the same discount rate over and over again. Therefore, instead of supplying an argument all the time, C++ allows you to define an argument whose value would be used whenever the function is not provided with a value for the argument.

To give a default value to an argument, when declaring the function, type the name of the argument followed by the assignment operator, =, followed by the default value. The CalculateNetPrice() function, with a default value, could be defined as:

using namespace System;

double CalculateNetPrice(double DiscountRate = 20);

Int main()

{

double FinalPrice;

FinalPrice = CalculateNetPrice();

Console::WriteLine("\nAfter applying the discount");

Console::WriteLine("Final Price = {0}", FinalPrice);

return 0;

}

double CalculateNetPrice(double DiscountRate)

{

double OrigPrice;

Console::Write("Please enter the original price: ");

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

return OrigPrice - (OrigPrice * DiscountRate / 100);

}

Here is an example of running the program:

Please enter the original price: 120.15

After applying the discount

Final Price = 90.1125

If a function takes more than one argument, you can provide a default argument for each and select which ones would have default values. If you want all arguments to have default values, when defining the function, type each name followed by = and followed by the desired value. Here is an example:

using namespace System;

double CalculateNetPrice(double Tax = 5.75, double Discount = 25,

double OrigPrice = 245.55);