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

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

Embed Size (px)

Citation preview

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

Part 1 Getting Started with C++.NET

Chapter 1 Hello, C++!

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

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.

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

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.

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

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.

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

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.

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

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.

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

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).

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

Free Format Language

The C++ language is free-format.

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

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 ( 偵錯 / 開始 )

Page 10: Part 1 Getting Started with C++.NET Chapter 1 Hello, C++!
Page 11: Part 1 Getting Started with C++.NET Chapter 1 Hello, C++!
Page 12: Part 1 Getting Started with C++.NET Chapter 1 Hello, C++!
Page 13: Part 1 Getting Started with C++.NET Chapter 1 Hello, C++!

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.

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

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

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

程式說明- #include The first line uses the directive #include to tell the C++ co

mpiler 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.

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

程式說明- 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.

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

程式說明- 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 }).

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

程式說明- 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.

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

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.

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

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.

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

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.

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

C++ Keywords and Identifiers

The following are legal C++ identifiers:• My_variable

• AReallyLongName

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

C++ Keywords and Identifiers

The following are not legal C++ identifiers:

Identifier Reason for Being Invalid

0800Number Must not start with a number

You+Me Must contain only letters, numbers, and underscores

return Must not be a reserved word

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

第一章 練習題 建一專案,名稱為「 ch1-XXXXXXX 」, XXX

XXXX 代表學號。 加入一 C++ 原始程式,程式輸出以下格式訊息

(選五個英文單字)至主控台:• 英文單字 中文意義 例句• ================================

• Celebrity  名人 , 名流  a national celebrity

上傳 .exe 檔至作業區 

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