C++程序设计(第3版)谭浩强 第8章 习题

1.请检查下面的程序,找出其中的错误(先不要上机,在纸面上作人工检查),并改正。然后上机调试,使之能正常运行。运行时从键盘输入时、分、秒的值,检查输出是否正确。

【解】

#include <iostream>
using namespace std;
class Time
{
public:
	void set_time();
	void show_time();
private:
	int hour;
	int minute;
	int sec;
};
Time t;
int main()
{
	t.set_time();
	t.show_time();
}
void Time::set_time()
{
	cin >> t.hour;
	cin >> t.minute;
	cin >> t.sec;
}
void Time::show_time()
{
	cout << t.hour << ":" << t.minute << ":" << t.sec << endl;
}

//12 34 56↙
//12:34 : 56

2.改写例8.1程序,要求:

(1) 将数据成员改为私有的;

(2) 将输入和输出的功能改为由成员函数实现;

(3) 在类体内定义成员函数。

【解】

#include <iostream>
using namespace std;
class Time
{
public:
	void set_time(void)
	{
		cin >> hour;
		cin >> minute;
		cin >> sec;
	}
	void show_time(void)
	{
		cout << hour << ":" << minute << ":" << sec << endl;
	}
private:
	int hour;
	int minute;
	int sec;
};
Time t;
int main()
{
	t.set_time();
	t.show_time();
	return 0;
}

3.在第2题的基础上进行如下修改: 在类体内声明成员函数,而在类外定义成员函数。

【解】

#include < iostream >
using namespace std;
class Time
{
public:
	void set_time(void);					//在类体内声明成员函数
	void show_time(void);					//在类体内声明成员函数
private:
	int hour;
	int minute;
	int sec;
};
void Time::set_time(void)					//在类外定义成员函数
{
	cin >> hour;
	cin >> minute;
	cin >> sec;
}
void Time::show_time(void)					//在类外定义成员函数
{
	cout << hour << ":" << minute << ":" << sec << endl;
}
Time t;
int main()
{
	t.set_time();
	t.show_time();
	return 0;
}

4.在第8.3.3节中分别给出了包含类定义的头文件student.h,包含成员函数定义的源文件student.cpp以及包含主函数的源文件main.cpp。请对该程序完善化,在类中增加一个对数据成员赋初值的成员函数set_value。上机调试并运行。

【解】

class Student
{
public:
	void display();
	void set_value();
private:
	int num;
	char name[20];
	char sex;
};


//student.cpp
//4.在第8.3.3节中分别给出了包含类定义的头文件student.h,
//包含成员函数定义的源文件student.cpp以及包含主函数的源文件main.cpp。
//请对该程序完善化,在类中增加一个对数据成员赋初值的成员函数set_value。
//上机调试并运行。

#include <iostream>
using namespace std;
#include "student.h"						//不要漏写此行
void Student::display()					//在类外定义display类函数
{
	cout << "num:" << num << endl;
	cout << "name:" << name << endl;
	cout << "sex:" << sex << endl;
}
void Student::set_value()
{
	cin >> num;
	cin >> name;
	cin >> sex;
}

//main.cpp
//4.在第8.3.3节中分别给出了包含类定义的头文件student.h,
//包含成员函数定义的源文件student.cpp以及包含主函数的源文件main.cpp。
//请对该程序完善化,在类中增加一个对数据成员赋初值的成员函数set_value。
//上机调试并运行。

#include <iostream>
using namespace std;
#include "student.h"						//即"student.h"
int main()
{
	Student stud;
	stud.set_value();
	stud.display();
	return 0;
}

//101↙						(输入学号)
//Li↙						(输入姓名)
//f↙ 						(输入性别)
//num : 101					(输出学号)
//name : Li					(输出姓名)
//sex : f					(输出性别)

5.将例8.4改写为一个多文件的程序:

(1) 将类定义放在头文件arraymax.h中;

(2) 将成员函数定义放在源文件arraymax.cpp中;

(3) 主函数放在源文件file1.cpp中。

请编写完整的程序,上机调试并运行。

【解】

class Array_max
{
public:
	void set_value();
	void max_value();
	void show_value();
private:
	int array[10];
	int max;
};


//8.5 arraymax.cpp
//5.将例8.4改写为一个多文件的程序:
//(1)将类定义放在头文件arraymax.h中;
//(2)将成员函数定义放在源文件arraymax.cpp中;
//(3)主函数放在源文件file1.cpp中。
//请编写完整的程序,上机调试并运行。

#include <iostream>
using namespace std;
#include "arraymax.h"
void Array_max::set_value()
{
	int i;
	for (i = 0; i < 10; i++)
		cin >> array[i];
}
void Array_max::max_value()
{
	int i;

	max = array[0];
	for (i = 1; i < 10; i++)
		if (array[i] > max) max = array[i];
}
void Array_max::show_value()
{
	cout << "max=" << max << endl;
}


//8.5 main.cpp
//5.将例8.4改写为一个多文件的程序:
//(1)将类定义放在头文件arraymax.h中;
//(2)将成员函数定义放在源文件arraymax.cpp中;
//(3)主函数放在源文件file1.cpp中。
//请编写完整的程序,上机调试并运行。

//(1)文件xt8 - 5 - 1.cpp
//xt8-5-1.cpp(file1.cpp)
#include <iostream>
using namespace std;
#include "arraymax.h"
int main()
{
	Array_max  arrmax;
	arrmax.set_value();
	arrmax.max_value();
	arrmax.show_value();
	return 0;
}

//2 14 27 9 34 67 34 43 –91 16↙					(输入10个元素的值)
//max = 67									(输出10个元素中的最大值)

6.需要求3个长方柱的体积,请编写一个基于对象的程序。数据成员包括length(长)、width(宽)、 height(高)。要求用成员函数实现以下功能:

(1) 由键盘分别输入3个长方柱的长、宽、高。

(2) 计算长方柱的体积;

(3) 输出3个长方柱的体积。

【解】

#include <iostream>
using namespace std;

class Box
{
public:
	void get_value();
	float volume();
	void display();
private:
	float length;
	float width;
	float height;
};

void Box::get_value()
{
	cout << "输入长方柱的长宽高:";
	cin >> length >> width >> height;
}

float Box::volume()
{
	return(length * width * height);
}

void Box::display()
{
	cout << volume() << endl;
}


int main()
{
	Box box1, box2, box3;
	box1.get_value();
	cout << "box1的体积:";
	box1.display();

	box2.get_value();
	cout << "box2的体积:";
	box2.display();

	box3.get_value();
	cout << "box3的体积:";
	box3.display();
	return 0;
}



//8.6.2
//8.6
//6.需要求3个长方柱的体积,请编写一个基于对象的程序。
//数据成员包括length(长)、width(宽)、 height(高)。
//要求用成员函数实现以下功能 :
//(1)由键盘分别输入3个长方柱的长、宽、高。
//(2)计算长方柱的体积;
//(3)输出3个长方柱的体积。

#include <iostream>
using namespace std;

class Box
{
public:
	void get_value();
	void volume();
	void display();
private:
	float length;
	float width;
	float height;
	float vol;
};

void Box::get_value()
{
	cout << "输入长方柱的长宽高:";
	cin >> length >> width >> height;
}

void Box::volume()
{
	vol = length * width * height;
}

void Box::display()
{
	cout << vol << endl;
}


int main()
{
	Box box1, box2, box3;
	box1.get_value();
	box1.volume();
	cout << "box1的体积:";
	box1.display();

	box2.get_value();
	box2.volume();
	cout << "box2的体积:";
	box2.display();

	box3.get_value();
	box3.volume();
	cout << "box3的体积:";
	box3.display();
	return 0;
}

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-01-19 10:42:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-19 10:42:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-19 10:42:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-19 10:42:02       18 阅读

热门阅读

  1. opencv的SIFT样例(CPP/python)

    2024-01-19 10:42:02       35 阅读
  2. NVIDIA jetson编译opencv 源码 python版本

    2024-01-19 10:42:02       28 阅读
  3. 神经网络分为哪几层?

    2024-01-19 10:42:02       33 阅读
  4. c语言0基础笔记

    2024-01-19 10:42:02       34 阅读
  5. Oracle Extractor

    2024-01-19 10:42:02       33 阅读
  6. P2717 寒假作业 题解 CDQ分治

    2024-01-19 10:42:02       32 阅读