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

Void Introduction()

{

Console::WriteLine(L"The Wonderful World of C++/CLI");

}

Int main()

{

Introduction();

return 0;

}

This would produce:

The Wonderful World of C++/CLI

Press any key to continue . . .

In the above examples, we defined the function before calling it. As an alternative, you can first declare a function and define it somewhere in your program. The main rule to observe in this case it that you must make sure the compiler can find the definition of the function when calling it. You can declare (but not define) a function inside of another function, then define the function outside. Here is an example:

using namespace System;

Int main()

{

Void IdentifyHouse();

Console::WriteLine();

return 0;

}

Void IdentifyHouse()

{

__wchar_t TypeOfHome;

Console::WriteLine(L"What Type of House Would you Like

to Purchase?");

Console::WriteLine(L"S - Single Family");

Console::WriteLine(L"T - Town House");

Console::WriteLine(L"C - Condominium");

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

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

Console::WriteLine(L"\nType of Home: {0}", TypeOfHome);

}

Once the function has been declared, you can call it even if it is defined later in the source file. Here is an example that calls the above function:

using namespace System;

int main()

{

void IdentifyHouse();

IdentifyHouse();

Console::WriteLine();

return 0;

}

void IdentifyHouse()

{

__wchar_t TypeOfHome;

Console::WriteLine(L"What Type of House Would you Like to

Purchase?");

Console::WriteLine(L"S - Single Family");

Console::WriteLine(L"T - Town House");

Console::WriteLine(L"C - Condominium");

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

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

Console::WriteLine(L"\nType of Home: {0}", TypeOfHome);

}

If you declare a function A inside of another B, that declared function A can be accessed only inside of the second function B. If you want other functions to be able to access a function, you can either define it above all functions that would call it, or declare it globally, that is, above and outside of any function. Here is an example:

using namespace System;

void IdentifyHouse();

int main()

{

IdentifyHouse();

Console::WriteLine();

return 0;

}

void IdentifyHouse()

{

__wchar_t TypeOfHome;

Console::WriteLine(L"What Type of House Would you Like

to Purchase?");

Console::WriteLine(L"S - Single Family");

Console::WriteLine(L"T - Town House");

Console::WriteLine(L"C - Condominium");

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

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

Console::WriteLine(L"\nType of Home: {0}", TypeOfHome);

}

9.1.4Practical Learning: Introducing Functions

  1. To start a new program, launch Microsoft Visual C++ 2005

  2. On the main menu, click File -> New -> Project...

  3. On the left side, make sure that Visual C++ is selected. In the Templates list, click CLR Empty Project

  4. In the Name box, replace the string with RealEstate9 and click OK

  5. To create a source file, on the main menu, click Project -> Add New Item...

  6. In the Templates list, click Source File (.cpp)

  7. In the New box, type Exercise and click Add

  8. In the empty file, type:

#include <iostream>

using namespace std;

using namespace System;