您的位置:首页 > 其它

!!!Chapter 1 Getting Started

2012-08-28 07:46 218 查看

1.1Writing a Simple C++ Program

A function definition specifies four elements: return type, function name, parameter list and function body.

Program files are referred to as source files, for example:

Prog.cc    prog.cxx    prog.cpp     prog.cp    prog.c
How to invoking MicroSoft compiler and running compiler from the command line:
1.    Setting the Path and Environment Variables from command line

             At the command prompt, change to the \bin subdirectory of your Visual C++ installation        

c:\VS10\VC\bin>
            Run vcvars32.bat from command line

2.    Write a .cpp program by notepad or something
3.    In command line, go to the folder used to store the .cpp file and type:

cl -Ehsc abc.cpp
       This can generate abc.exe in the same folder
4.    Run abc.exe directly and check the return value by the following command:

echo %ERRORLEVEL%

1.2 A First Look at Input/Output

Most of the input/output use iostream library.

Standard Input and output objects:

cin: standard input

cout: standard output

cerr: standard error

clog: used to generate information
Preprocessor directive is used to tells the compiler that we want to use certain library. The name inside the angle bracket is a header. For example:

#include <iostream>
Output operator (the << operator) writes its right-hand operand to the ostream that is its left-hand operand. For example:

Std::cout << "Enter two values:" << std::endl;
Endl is a manipulator, when write to output stream, it can writing a newline to the output and flushing the buffer. By flushing the buffer, we ensure that the user will see the output written to the stream immediately.

Prefix std:: indicates the names cout and endl are defined in the namespace named std. Here :: is scope operator.

1.3 A Word about Comment

There are two kinds of comments in C++: single-line and
paired.

A single-line comment starts with a double slash(//). For example:

// You can also put the comment before the code
Std::cout << "hello";  // output some thing

A paired comment used /**/. It begins with a /* and end with the next */. Comment pairDo Not Nest

We can begin each line in the comment with an asterisk, and place a comment block above the code it explains. For example:

/*
* This is comment
*/
Int main()
{
Return 0;
}
One way to ignore a section of code is to use your editor to insert single-line comment at the beginning of each line of code you want to ignore.

1.4 Control Structure

A condition is an expression that is evaluated so that its result can be tested. If the resulting value is nonzero, then the condition is true; if the value is zero then the condition is false.
A block is a sequence of statements enclosed by curly braces. For example:

{
Sum+=val;
++val;
}
We could define variable inside the for statement:

for (int val=1;val<=10; ++val)
{
Sum+=val;
}
The scope of variables inside For-loop:
If we define the variable outside for-loop, we can still use the variable after the for-loop. And the value of the variable can be modified inside the for-loop
If we define the variable inside the for-loop, we will not be able to use the variable after the for-loop
Sample Code:

How to read unknown number of inputs:
#include <iostream>
int main()
{
int sum=0, value;
while(std::cin>>value)
sum += value;
std::cout <<"sum is:" << sum<<std::endl;
return 0;
}


line 5 will check if the input is still valid. An istream becomes invalid when we hit EOF or an invalid value. For example,if we input 1.2 it will show "sum is:1"!

1.5 Introducing Classes

In C++ we define our own data structure by defining a class.

To use a class we need to know:

what is its name
where is it defined
what operations does it support

1.5.1 The sales_item Class

To use a class, we need not know anything about how it is implemented. Instead, what we need to know is what operations the class provides.

When use a class, we must make the definitions associated with the class available to the compiler. Conventionally, class types are stored in a file with a name that, like the name of a program source file. The suffix is usually .h

Operations on Sales_item objects

Every class defines a type. The type name is the same as the name of the class.

We can define a variable of a class type:

Sales_item item1;
Here item is an object of type Sales_item.

1.5.2 Member Functions

A member function is a function that is defined by a class. Member functions are sometimes referred to as the methodsof the class.

Member functions are defined once for the class but are treated as members of each object.

When we call a member function, we specify the object on which the function will operate. This syntax uses thedot operator :

item1.same_isbn
The dot operator fetches its right-hand operand from its left. The dot operator applies only to objects of class type: The left-hand operand must be an object of class type; the right-hand operand must name a member of that type.
When we use a member function as the right-hand operand of the dot operator, we usually do so to call that function.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: