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

1.5.4Открытие проекта

В противовес созданию нового проекта, вы можете открыть уже существующей проект, созданный вами или слямзенный. Для открытия существующего проекта выполните команду FileOpenProject... Вы можете также выполнить команду FileOpen Solutions. Альтернативно, вы можете выбрать Open Project в окне Start Page. В любом случае появится окно Open Project, в котором вы и можете открыть проект.

1.5.5Инструкции С++

Computer programming is the art of telling the computer what to do, when to do it, and how to do it. The programmer accomplishes this by giving instructions to the computer. An example of an instruction is, "Get a number from the user", or “Display a picture on the screen". The computer carries a lot of such assignments. In C++, an assignment is called a function; it is a task you ask the computer to perform to successfully render an intended result. Like any of the objects you will be using in your programs, a function has a name. The name of a function is a letter or a combination of letters and digits. To differentiate the name of a function from other objects, the name of a function is followed by parentheses. As an assignment, a function is made of a set of instructions. These instructions are listed in a group of lines referred to as the body of the function. The body of a function starts with an opening curly bracket "{" and ends with a closing curly bracket "}"; everything in between is part of the function. Therefore, to use a function in your program, its syntax is:

FunctionName() {}

Actually, a function is a little more than that, but for now, this is enough for us.

The most fundamental function in C++ is called main. The main() function can take various forms, the easiest version is as follows:

main() {}

According to the C++ standard, the main function must return (we will learn what that word means when we study functions) a number. Based on this, it should be written as:

int main() {}

If it is written like that, then the last line in the body of main must be written as

return 0;

When we study functions, we will learn what this line means. Instead of writing everything in one line, you should spread your code to various lines to make it easier to read. Now we would have:

int main()

{

return 0;

}

In future lessons, we will expand our knowledge of functions. For now, note that, besides main(), a program can have other functions. These functions can look like main or be significantly different. The first rule is that, to reduce confusion, no other function can be called main. Here is an example:

int other()

{

return 0;

}

int main()

{

return 0;

}

In the same way, you can have as many functions as you judge necessary in your program. Here is an example:

int other()

{

return 0;

}

int another()

{

return 0;

}

int main()

{

return 0;

}

When you have created a function as done above, in order to use it in your program, you must "call" it. From main(), the simplest way to call a function consists of writing its name followed by parentheses and a semi-colon. Here is an example:

int other()

{

return 0;

}

int main()

{

other();

return 0;

}

There are various other aspects you would take into consideration. We will review them when we study functions. For now, the above code would work perfectly.

1.5.6Introduction to C++ Statements

A C++ file is made of statements. These are sequences of actions you ask C++ to perform while the program is running. You can write such a statement on one line, or you can span more than one line. By default, when you start a new line, the compiler considers that you are starting a new statement. To let the compiler know when a statement ends, you write a semi-colon at the end of the line. For example, to write one statement, you could use:

This-Would-Be-A-C++-Statement;

To span more than one line, type a semi-colon only when the statement is over. Here is an example:

A-Long-Statement-From-C++-That-Spans-

More-Than-One-Line;