1.函数的定义:返回类型(return type)、函数名(function name)、形参列表(parameter,允许为空)和函数体(function body)。
2.c++程序必须包含一个main函数。main函数的返回值必须为int。
int main()
{
return 0;
}
3.iostream库 istream: cin;ostream: cout、cerr、clog
4.命名空间 将库定义的名字放在一个单一位置的机制。命名空间可以帮助避免不经意的名字冲突。C++标准库定义的名字在命名空间std中。
5.控制流
// while语句
while (condition)
{
statement
}
// for语句
for (init statement; condition; expression)
{
statement
}
// if语句
if (condition)
{
statement
}
else if (condition)
{
statement
}
else
{
statement
}
6.类简介
- 一种用于定义自己的数据结构及其相关操作的机制。
- 每个类都定义了一个新的类型,类型名就是类名。
- 类的作者定义了类对象可以执行的所有动作。
- 成员函数是定义为类的一部分的函数(方法)。
书店程序
#includeusing namespace std; #include"Sales_item.h" int main() { Sales_item total; // 保存下一条交易记录的变量 // 读入第一条交易记录,并确保有数据可以处理 if (cin >> total) { Sales_item trans; // 保存和的变量 while (cin >> trans) { // 如果我们仍在处理相同的书 if (trans.isbn() == total.isbn()) { total += trans; // 更新总销售额 } else { // 打印前一本书的结果 cout << total << endl; total = trans; // total现在表示下一本书的销售额 } } cout << total << endl; // 打印最后一本书的结果 } else { cerr << "No data?!" << endl; return -1; } return 0; }



