C++面对对象程序设计(第二版)

本人是一个学习C++不久的小菜鸟,这些题目都是自己打出来(有些不会的去网上查出来的),如果有哪里写的不对的地方,欢迎各位指正。

第二章课后习题

P-66

第一题

改之前的代码

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

 

可以看到程序中有一堆错误

 

改之后的代码

#include<iostream>
using namespace std;
class Time
{
public:						//添加了访问说明符(相关知识在C++primer第五版P-240页有),类型为公有
							//使函数在主函数里可以使用
	void set_time(void);
	void show_time(void);
private:					//这个也是访问说明符,类型为私有
	int hour;
	int minute;
	int sec;
};
Time t;
int main() {
	t.set_time();				//调用的方式不对 一个是对象名.函数名;
	t.show_time();
	system("pause");
	return 0;
}
void Time::  set_time(void) {	//改变了函数类型,因为在定义的时候函数的类型的私有的
								//剩下的就是格式错了 ,在类外定义的正确格式为	函数类型名 类名::函数名(形参)
								//其中::是作用域运算符
	cin >> t.hour;
	cin >> t.minute;
	cin >> t.sec;
}
void Time::  show_time(void) {
	cout << t.hour << ":" << t.minute << ":" << t.sec << endl;
}

 

 

第二题

例 2.1原题如下

#include<iostream>
using namespace std;
class Time {
public:
	int hour;
	int minute;
	int sec;
};
int main() {
	Time t1;
	cin >> t1.hour;
	cin >> t1.minute;
	cin >> t1.sec;
	cout << t1.hour << ":" << t1.minute << ":" << t1.sec << endl;
	system("pause");
	return 0;
}

改后的代码

#include<iostream>
using namespace std;
class Time {
public:
	void set_time(){	//定义成员函数
		cin >> hour;
		cin >> minute;
		cin >> sec;
	}
	void show_time(){	//定义成员函数
		cout << hour << ":" << minute << ":" << sec << endl;
	}
private:	//改为私有
	int hour;
	int minute;
	int sec;
};
int main() {
	Time t1;
	t1.set_time();
	t1.show_time();
	system("pause");
	return 0;
}

 

 

3

代码如下

#include<iostream>
using namespace std;
class Time {
public:
	void set_time();	//声明一下
	void show_time();	//声明一下
private:	//改为私有
	int hour;
	int minute;
	int sec;
};
void Time :: set_time(){	//函数体外定义
	cin >> hour;
	cin >> minute;
	cin >> sec;
}
void Time::show_time() {	//函数体外定义
	cout << hour << ":" << minute << ":" << sec << endl;
}
int main() {
	Time t1;
	t1.set_time();
	t1.show_time();
	system("pause");
	return 0;
}

4

这题也不难,(我一开始的时候还不会做,羞愧)

就是文件位置,文件位置改一下就ok了。

5

这题就不截图了,贴下代码吧(位置和上图一样)

//arraymax.h
using namespace std;
class Array_max {
public:
	void set_value();
	void max_value();
	void show_value();
private:
	int array[10];
	int max;
};
//arraymax.cpp
#include<iostream>
#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;
}

 

//filel.cpp
#include<iostream>
#include"arraymax.h"
using namespace std;
int main() {
	Array_max arrmax;
	arrmax.set_value();
	arrmax.max_value();
	arrmax.show_value();
	system("pause");
	return 0;
}

 

第6题

 

这题比较简单,就只贴下代码

#include<iostream>
#include<string>
using namespace std;
class Volume {
public:
	void set_volume();
	void show_volume();
private:
	float length;
	float width;
	float height;
	float volume;
};
void Volume::set_volume(){
	cout << "\nPlease put the length and width and height " << endl;
	cin >> length >> width >> height;
	volume = length * width*height;
}
void Volume::show_volume() {
	cout << volume;
}
int main() {
	Volume v1, v2, v3;
	v1.set_volume();
	v1.show_volume();
	v2.set_volume();
	v2.show_volume();
	v3.set_volume();
	v3.show_volume();
	system("pause");
	return 0;
}

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值