第九章 类和对象练习题(上)

类和对象算是真正面向对象语言特有的东西

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

需要指出错误的代码:

#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( );
}
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:
	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( );

	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) 将数据成员改为私有的;
(2) 将输入和输出的功能改为由成员函数实现;
(3) 在类体内定义成员函数

待改写的代码:

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

改写之后的代码:

#include <iostream>
using namespace std;
class Time
{
private:
    int hour;
    int minute;
    int sec;
public:
	void set(void)
	{
		cin>>hour;
		cin>>minute;
		cin>>sec;
	}
	void get(void)
	{
		cout<<hour<<" : "<<minute<<" : "<<sec;
	}
};
int main()
{
	Time t1;
    Time &t2=t1;
	t2.set();
	t2.get();

	return 0;
 }

运行结果:
在这里插入图片描述

-------------------------------------------作业-------------------------------------------------

1. 构建一个类,含有三个数据成员,分别表示一个长文体的长、宽、高;含有一个成员函数,用来计算长方体的体积。

代码:

#include <iostream>
using namespace std;

class rectangle
{
	int l;
	int w;
	int h;
public:
	int volume(int i, int j, int k)
	{
		if(i<=0||j<=0||k<=0)
			cout<<"输入有误!!";
		return i*j*k;
	}
};
int main()
{
	rectangle r;
	int l=0,w=0,h=0;
	cout<<"请依次输入长 宽 高:"<<endl;
	cin>>l;
	cin>>w;
	cin>>h;
	
	cout<<"长方体的面积为:"<<r.volume(l,w,h)<<endl;

	return 0;
}

运行结果:

在这里插入图片描述
2. 设计一个学生类,包含学生的姓名,数学、物理、英语课程成绩,计算学生的平均成绩。

代码:

#include <iostream>
using namespace std;

class student
{
	char name[10];
	int math;
	int physical;
	int english;
public:
	void average(void)
	{
		cout<<"Name: "<<endl;
		cin>>name;
		cout<<"Math: "<<endl;
		cin>>math;
		cout<<"Physical: "<<endl;
		cin>>physical;
		cout<<"English: "<<endl;
		cin>>english;

		cout<<name<<" 的平均分是:"<<(math+physical+english)/3<<endl;
	}
};
int main()
{
	student s;
	s.average();

	return 0;
}

运行结果:
在这里插入图片描述

3.一圆形游泳池,现在需要在其周围建一个圆形过道,并在其四周围上栅栏。栅栏价格为35元/米,过道造价为20元/平方米,过道宽度为3米。游泳池半径由键盘输入。用面向对象的方法设计圆形类Circle,计算并输出过道和栅栏的造价

代码:

#include <iostream>
#include<cmath>
using namespace std;


class Circle
{
	double radius;

public:
	void price(void)
	{
		cout<<"请输入游泳池的半径: ";
		cin>>radius;

		cout<<"过道的造价:"<<(pow(radius+3,2)-pow(radius,2))*3.14*20<<" ¥"<<endl;

		cout<<"栅栏的造价:"<<(radius+3)*3.14*2*35<<" ¥"<<endl;
	}
};
int main()
{
	Circle c;
	c.price();

	return 0;
}

运行结果:
在这里插入图片描述

千里之行,始于足下!
原文地址:https://www.cnblogs.com/MINAIot/p/13040991.html