目录
第一章 开始
1.1 编写一个简单的C++程序
int main ()
{
return 0;
}
每个C++程序都必须有一个main函数, main函数的返回值类型必须为int型。函数的定义包含四部分:返回类型、函数名、括号包围的形参列表以及函数体。注意,大多数C++语句都要以分号结束。
1.2 初识输入输出
iostream库:包含istream和ostream,分别表示输入流和输出流。
标准输入输出对象:标准输入(cin)、标准输出(cout)、输出警告和错误信息(cerr)、输出程序运行一般性信息(clog)。
#include <iostream> //一般系统头文件用尖括号表示,用户自己编写的则用双引号。一般将所有的#include指令都放在源文件开头位置。
int main()
{
std::cout << "Enter two numbers: " << std::endl; //标准输出,把右边的数据给cout对象,用 << 符号,endl表示结束当前行,并将与设备关联的缓冲区的内容刷到设备中。
int v1= 0, v2 = 0;
std::cin >> v1 >> v2; //标准输入,把左边输入的数据给后面的变量,所以用 >> 符号。
std::cout << "The sum of " << v1 << " and " << v2
<< " is " << v1 + v2 << std::endl;
return 0;
}
1.3 注释简介
单行注释:以双斜线 // 开始,以换行符结束,常用于半行和单行注释。
界定符注释:以 /* 开始,以 */ 结束,通常用于多行注释。为了美观,从第二行每行以星号空格开始,注释从第二行开始书写。注意:界定符注释不能嵌套使用。
/*
* 功能:注释示例
* 作者:今天也要加油鸭!
* 版本号:v1.0
* 最后更新时间:2021/12/14
*/
int main ()
{
return 0;
}
1.4 控制流
while语句:
while (condition)
statement
for语句:
for(int val = 1; val <= 10; ++val)
sum += val;
读取数量不定的输入数据:
while (std::cin >> value) //遇到文件结束符,或遇到一个无效的输入,条件变为假,终止输入,Windows中文件结束符为:Ctrl+Z。
sum += value;
if-else语句:
if (condition){
statement;
} else {
}
1.5 类简介
点运算符(.):只能用于类类型的对象,其左侧运算对象必须是一个类类型的对象,右侧运算对象必须是该类型的一个成员名,运算结果为右侧运算对象指定的成员。
调用运算符(()):用来调用一个函数,里面放置实参列表。
1.6 书店程序
//Sales_item.h
#ifndef SALESITEM_H
// we're here only if SALESITEM_H has not yet been defined
#define SALESITEM_H
// Definition of Sales_item class and related functions goes here
#include <iostream>
#include <string>
class Sales_item {
// these declarations are explained section 7.2.1, p. 270
// and in chapter 14, pages 557, 558, 561
friend std::istream& operator>>(std::istream&, Sales_item&);
friend std::ostream& operator<<(std::ostream&, const Sales_item&);
friend bool operator<(const Sales_item&, const Sales_item&);
friend bool
operator==(const Sales_item&, const Sales_item&);
public:
// constructors are explained in section 7.1.4, pages 262 - 265
// default constructor needed to initialize members of built-in type
Sales_item(): units_sold(0), revenue(0.0) { }
Sales_item(const std::string &book):
bookNo(book), units_sold(0), revenue(0.0) { }
Sales_item(std::istream &is) { is >> *this; }
public:
// operations on Sales_item objects
// member binary operator: left-hand operand bound to implicit this pointer
Sales_item& operator+=(const Sales_item&);
// operations on Sales_item objects
std::string isbn() const { return bookNo; }
double avg_price() const;
// private members as before
private:
std::string bookNo; // implicitly initialized to the empty string
unsigned units_sold;
double revenue;
};
// used in chapter 10
inline
bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs)
{ return lhs.isbn() == rhs.isbn(); }
// nonmember binary operator: must declare a parameter for each operand
Sales_item operator+(const Sales_item&, const Sales_item&);
inline bool
operator==(const Sales_item &lhs, const Sales_item &rhs)
{
// must be made a friend of Sales_item
return lhs.units_sold == rhs.units_sold &&
lhs.revenue == rhs.revenue &&
lhs.isbn() == rhs.isbn();
}
inline bool
operator!=(const Sales_item &lhs, const Sales_item &rhs)
{
return !(lhs == rhs); // != defined in terms of operator==
}
// assumes that both objects refer to the same ISBN
Sales_item& Sales_item::operator+=(const Sales_item& rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
// assumes that both objects refer to the same ISBN
Sales_item
operator+(const Sales_item& lhs, const Sales_item& rhs)
{
Sales_item ret(lhs); // copy (|lhs|) into a local object that we'll return
ret += rhs; // add in the contents of (|rhs|)
return ret; // return (|ret|) by value
}
std::istream&
operator>>(std::istream& in, Sales_item& s)
{
double price;
in >> s.bookNo >> s.units_sold >> price;
// check that the inputs succeeded
if (in)
s.revenue = s.units_sold * price;
else
s = Sales_item(); // input failed: reset object to default state
return in;
}
std::ostream&
operator<<(std::ostream& out, const Sales_item& s)
{
out << s.isbn() << " " << s.units_sold << " "
<< s.revenue << " " << s.avg_price();
return out;
}
double Sales_item::avg_price() const
{
if (units_sold)
return revenue/units_sold;
else
return 0;
}
#endif
#include <iostream> //标准库头文件
#include "Sales_item.h" //自定义的头文件
int main()
{
Sales_item total;
if (std::cin >> total) {
Sales_item trans;
while (std::cin >> trans) {
if (total.isbn() == trans.isbn())
total += trans;
else {
std::cout << total << std::endl;
total = trans;
}
}
std::cout << total << std::endl;
} else {
std::cerr << "No data?!" << std::endl;
return -1;
}
return 0;
}
1.7 其他
- 在C++中,=表示赋值,==表示相等。
- 除非特别需要,尽量使用 ++i 而不是 i++。