0%

C++语言程序设计(郑莉第五版)第七课后习题代码实现

第七章 类的继承

7-5 定义一个哺乳动物类Mammal,再由此派生出狗类Dog,定义一个Dog类的对象,观察基类与派生类的构造函数与析构函数的调用顺序。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//定义哺乳动物类mammal
class mammal {
public:
mammal() {
cout << "constructing mammal..." << endl;
}
~mammal() {
cout << "destructing mammal..." << endl;
}
private:
};
//定义狗类Dog
class Dog : public mammal {
public:
Dog() {
cout << "constructing dog..." << endl;
}
~Dog() {
cout << "destructing dog..." << endl;
}
private:
};

int main() {
Dog dog;
return 0;
}
//constructing mammal...
//constructing dog...
//destructing dog...
//destructing mammal...

11-2 cout、cerr和clog有何区别?

  • cout是标准输出流。
  • cerr是标准错误输出流,没有缓冲,发给它的内容立即被输出。
  • clog类似于cerr但有缓冲,缓冲区满时被输出。

11-3 使用I/O流以文本方式建立一个文件test1.txt,写入字符“已成功写入文件!”,用其他字处理程序打开,看看是否正确写入。

1
2
3
4
5
6
int main() {
ofstream file("test1.txt", ios_base::out);
file << "已成功写入文件!";
file.close();
return 0;
}

11-4 使用I/O流以文本方式打开11-3题建立的文件test1.txt,读出其内容并显示出来,看看是否正确。

1
2
3
4
5
6
7
8
int main() {
ifstream file("test1.txt", ios_base::in);
string line;
file >> line;
cout << line;////输出结果:已成功写入文件!
file.close();
return 0;
}

11-5 使用I/O流以文本方式打开11-3题建立的文件test1.txt,在文件后面添加字符“已成功添加字符!”,然后读出整个文件的内容显示出来,看看是否正确。

1
2
3
4
5
6
7
8
9
10
11
int main() {
ofstream fileout("test1.txt", ios_base::out|ios_base::app);
fileout << "已成功添加字符!";
fileout.close();
ifstream filein("test1.txt", ios_base::in);
string line;
filein >> line;
cout << line;//输出结果:已成功写入文件!已成功添加字符!
filein.close();
return 0;
}

11-6 定义一个Dog类,包含体重和年龄两个成员变量及相应的成员函数,声明一个实例dog1,体重为5,年龄为10,使用I/O流把dog1的状态写入磁盘文件,再声明一个实例dog2,通过读文件把dog1的状态赋给dog2.分别使用文本方式和二进制方式操作文件,看看结果有何不同;再看看磁盘文件的ASCII码有何不同。

Dog类声明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Dog {
private:
double weight, age;
public:
Dog(double weight = 0, double age = 0) :weight(weight), age(age) {};
void setWeight(double w) { weight = w; }
void setAge(double a) { age = a; }
double getWeight() { return weight; }
double getAge() { return age; }
};
ostream& operator <<(ostream &out, Dog& dog) {
out << dog.getWeight() << ' ' << dog.getAge();
return out;
}
istream& operator >>(istream &in, Dog &dog) {
double w, a;
in >> w >> a;
dog.setWeight(w);
dog.setAge(a);
return in;
}

main()函数实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
int main() {
Dog dog1(5, 10);
ofstream fileout("DogLog", ios_base::out | ios_base::binary);
fileout << dog1;
fileout.close();

Dog dog2;
ifstream filein("DogLog", ios_base::in|ios_base::binary);
filein >> dog2;
filein.close();
cout << "dog2:" << dog2 << endl;
return 0;
}

11-7 观察下面的程序,说明每条语句的作用,写出程序执行的结果。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
using namespace std;
int main(){
ios_base::fmtflags original_flags = cout.flags();//1
cout << 812 << '|';
cout.setf(ios_base::left, ios_base::adjustfield);//2:设置为左对齐,填充字符填充右边
cout.width(10);//3:设置下一次输出的数值字符宽度为10,不足用空格补齐
cout << 813 << 815 << '\n';
cout.unsetf(ios_base::adjustfield);//4:取消设置的格式标志
cout.precision(2);
cout.setf(ios_base::uppercase | ios_base::scientific);//5:以科学计数法显示,并使用大写字母E(采用科学计数法表示,上行指令执行为显示小数点后两位)
cout << 831.0;
cout.flags(original_flags);//6
return 0;
}