Presentation is loading. Please wait.

Presentation is loading. Please wait.

Part 1 Getting Started with C++.NET Chapter 1 Hello, C++!

Similar presentations


Presentation on theme: "Part 1 Getting Started with C++.NET Chapter 1 Hello, C++!"— Presentation transcript:

1 Part 1 Getting Started with C++.NET Chapter 1 Hello, C++!

2 What Is a C++ Program? it contains exactly the same elements as any other computer program—data to store your information and blocks of code that manipulate that data.

3 C++ is a strongly typed language. If you have a variable that has been declared able to store apples, you can store only apples in it. However, this requirement is not as grim as it sounds. C++ contains many features for providing implicit conversions where necessary. This strong type checking eliminates many common programming bugs because it explicitly disallows any data type conversions that could result in data loss.

4 C++ is an efficient language. If you write code that needs to execute quickly (for example, code that sorts lists or performs complex mathematics), C++ should be your language of choice.

5 C++ is an object-oriented language. Modern programmers like the many advantages of object-oriented programming. (See Chapter 2 for more information on object-oriented programming.) C++ is one of the premier object-oriented programming languages.

6 C++ is based on C C is a well-established language. Although C++ includes some strange features—to maintain its compatibility with C—it probably wouldn’t be as popular as it is without C compatibility.

7 C++ is a case-sensitive language. If the compiler warns you about an undeclared variable, you probably typed an uppercase character instead of a lowercase character (or vice versa).

8 Free Format Language The C++ language is free-format.

9 Creating an Executable Program — Practice Start up Microsoft Visual Studio.NET. Creating a Project Adding a C++ Source File to the Project Adding C++ Code to the Source File Building the Executable( 建置 / 建置方案 ) Executing the Program ( 偵錯 / 開始 )

10

11

12

13 Creating an Executable Program — Theory Editing the Program Source Files Compiling the Source Files The C++ compiler is the tool for converting textual source files into machine code object files that have an.obj file extension.

14 Creating an Executable Program — Theory Linking the Object Files The final step in producing an executable file is to link together all object files that make up a particular project. This linking includes not only object files produced from your own source code, but also object files from system libraries such as the C++ standard library or the Microsoft Foundation Class (MFC) library. Running and Testing the Program

15 程式說明- #include The first line uses the directive #include to tell the C++ compiler to copy in the file named iostream at the beginning of the program. Why is this inclusion necessary? A golden rule of C++ is that everything must be declared before it is used, including the output stream named cout used later in the program. So why not just declare cout explicitly in this file? Because the iostream file is a separate unit, it can be included in any other program, thus making it easy to reuse the declarations. In a nutshell, this file saves a lot of typing for the programmer.

16 程式說明- using namespace std; The second line (which begins with using) tells the compiler that the standard C++ library is to be used (hence the word std—an abbreviation of standard). Many different libraries could be used in a single project; the using statement lets us tell the compiler which library we mean to use.

17 程式說明- function The rest of the program is an example of a C++ function. All blocks of code in C++ are called functions—there’s no such thing as a procedure or a subroutine. Each C++ function contains the header (the first line of this program) and the function body (all of the text between the braces { and }).

18 程式說明- function The header shows the return value of the function (in this case int, short for integer), the name of the function (main), and the list of parameters inside round brackets. This example has no parameters, so the round brackets are empty—but the brackets still must be there. All statements in C++ are terminated with a semicolon.

19 The main function A normal C++ program contains many functions (and also many classes, as discussed in Chapter 2). How does the compiler know which function should be called first? Obviously, the compiler can’t be allowed to just randomly choose a function! The rule is that the compiler will always generate code that looks for a function named main. If you omit the main function, the compiler reports an error and doesn’t create a finished executable program.

20 C++ Keywords and Identifiers A C++ keyword (also called a reserved word) is a special item of text that the compiler expects to be used in a particular way. The keywords used in the example program are using, namespace, and return. You’re not allowed to use these keywords as variable or function names—the compiler will report an error if you do.

21 C++ Keywords and Identifiers An identifier is any name that the programmer uses to represent variables and functions. An identifier must start with a letter and must contain only letters, numbers, or underscores.

22 C++ Keywords and Identifiers The following are legal C++ identifiers: My_variable AReallyLongName

23 C++ Keywords and Identifiers The following are not legal C++ identifiers: IdentifierReason for Being Invalid 0800NumberMust not start with a number You+MeMust contain only letters, numbers, and underscores returnMust not be a reserved word

24 第一章 練習題 建一專案,名稱為「 ch1-XXXXXXX 」, XXXXXXX 代表學號。 加入一 C++ 原始程式,程式輸出以下格式訊息 (選五個英文單字)至主控台: 英文單字 中文意義 例句 ================================ Celebrity 名人, 名流 a national celebrity 上傳.exe 檔至作業區

25


Download ppt "Part 1 Getting Started with C++.NET Chapter 1 Hello, C++!"

Similar presentations


Ads by Google