《C++ primer 5》 chapter 1.2

来源:互联网 发布:java监控线程 编辑:程序博客网 时间:2024/06/05 11:53

1.2 input and output

note:

C++ language does not define any statements to do IO. Instead, C++ include an extensive standard library that provides IO.

Fundamental to the iostream library are two types named istream and ostream, which represent input and output streams.

The object of type istream named cin.

The object of type ostream named cout, cerr, clog.


The << operator takes two operands : the left-hand operand must be an ostream object, the right hand operand is a value to print. The operator writes the given value on the given ostream. The result of the output operator is its left-hand operand.

std::cout<< "hello"; //string literalstd::cout<<std::endl; //manipulator

endl has the effect of ending the current lineand flushing the buffer associated with that devise

std::cin>>v1>>v2;

The input operation reads two values from std::cin, storing the first in v1 and the second in v2.


Using names from standard library: the prefixstd:: indicates that the names like cout is defined insidethe namespace named std.

Avoid inadvertent collisions between the name we defined and the name inside a library.

All the names defined by the standard library are in the std namespace.


exercise:

exercise 1,3

<span style="font-size:14px;">#include<iostream>int main(){std::cout<<"Hello, World"<<std::endl;system("pause");return 0;}</span>

exercise 1.4

<span style="font-size:14px;">#include<iostream>int main(){int a=0,b=0;std::cin>>a>>b;std::cout<<a*b<<std::endl;system("pause");return 0;}</span>

exercise 1.5

<span style="font-size:14px;">std::cout<<"The sum of ";std::cout<<v1;std::cout<<" and ";std::cout<<v2;std::cout<<" is ";std::cout<<v1+v2;std::cout<<std::endl;</span>

exercise 1.6

This program is illegal because there is no ostream object in the second and the third statement.

You can add std::cout to the second and the third statement.

Or you can just remove  ";" from the first and the second line.

0 0
原创粉丝点击