计算机程序设计C++(第11周基础练习)

计算机程序设计C++ MOOC

测试与作业C++基础练习100题

##第十一周基本练习

本周内容为类的继承

  1. 继承点类定义圆类
    在这里插入图片描述
#include <iostream>

using namespace std;

class POINT
{
private:
	double x, y;
public:
	POINT(){ x = 0; y = 0; }
	POINT(double a, double b){ x = a; y = b; }
	void set(double a, double b){ x = a; y = b; };
	void show()
	{
		cout << "x=" << x << ' ' << "y=" << y;
	}
};

class CIRCLE :public POINT
{
private:
	double radius;
public:
	CIRCLE() :POINT(){ radius = 0; }
	CIRCLE(double a, double b, double r) :POINT(a, b){ radius = r; }
	void set(double a, double b, double r){ POINT::set(a, b); radius = r; }
	void show()
	{
		POINT::show(); cout << " r=" << radius;
	}
};

int main()
{
	CIRCLE a, b(1, 2, 3);
	double x, y, r;
	a.show(); cout << endl;
	b.show(); cout << endl;
	cin >> x >> y >> r;
	a.set(x, y, r);
	a.show(); cout << endl;
	return 0;
}
  1. 继承点类定义矩形类
    在这里插入图片描述
#include <iostream>

using namespace std;

class POINT
{
private:
	double x, y;
public:
	POINT(){ x = 0; y = 0; }
	POINT(double a, double b){ x = a; y = b; }
	void set(double a, double b){ x = a; y = b; };
	void show()
	{
		cout << "x=" << x << ' ' << "y=" << y;
	}
};

class RECT :public POINT
{
private:
	double width, height;
public:
	RECT() :POINT(){ width = 0; height = 0; }
	RECT(double a, double b, double c, double d) :POINT(a, b){ width = c; height = d; }
	void set(double a, double b, double c, double d){ POINT::set(a, b); width = c; height = d; }
	void show()
	{
		POINT::show();
		cout << " width=" << width << " height=" << height;
	}
};


int main()
{
	RECT a, b(1, 2, 10, 5);
	double x, y, w, h;
	a.show(); cout << endl;
	b.show(); cout << endl;
	cin >> x >> y >> w >> h;
	a.set(x, y, w, h);
	a.show(); cout << endl;
	return 0;
}
  1. 继承房间类定义教室类
    在这里插入图片描述
#include <iostream>
#include <string.h>

using namespace std;

class ROOM
{
private:
	char RoomId[20];
public:
	ROOM(){ strcpy(RoomId, "0000"); }
	ROOM(char *id){ strcpy(RoomId, id); }
	void set(char *id){ strcpy(RoomId, id); }
	void show()
	{ 
		cout <<"Number:"<< RoomId;
	}
}; 

class CLASSROOM :public ROOM
{
private:
	int seats;
public:
	CLASSROOM() :ROOM(){ seats = 0; }
	CLASSROOM(char *id, int s) :ROOM(id){ seats = s; }
	void set(char *id, int s){ ROOM::set(id); seats = s; }
	void show()
	{ 
		ROOM::show();
		cout << " seats:" << seats;
	}
};

int main()
{
	CLASSROOM a, b("工程馆505",130);
	char number[20];
	int seats;
	a.show(); cout << endl;
	b.show(); cout << endl;
	cin >> number >> seats;
	a.set(number, seats);
	a.show(); cout << endl;
	return 0;
}
  1. 继承车辆类定义客车类
    在这里插入图片描述
#include <iostream>
#include <string.h>

using namespace std;

class VEHICLE
{
private:
	char Number[20], Owner[20];
public:
	VEHICLE(){ strcpy(Number, "00000"); strcpy(Owner, "unknown"); }
	VEHICLE(char *n, char *o){ strcpy(Number, n); strcpy(Owner, o); }
	void set(char *n, char *o){ strcpy(Number, n); strcpy(Owner, o); }
	void show()
	{
		cout << "Number:" << Number << " owner:" << Owner;
	}
};

class BUS :public VEHICLE
{
private:
	int seats;
public:
	BUS() :VEHICLE(){ seats = 0; };
	BUS(char *n, char *o, int s) :VEHICLE(n, o){ seats = s; }
	void set(char *n, char *o, int s){ VEHICLE::set(n, o); seats = s; }
	void show()
	{
		VEHICLE::show();
		cout << " seats:" << seats;
	}
};

int main()
{
	BUS a, b("SHAN-1234567", "ZhangSan", 40);
	char number[20], name[20];
	int seats;
	a.show(); cout << endl;
	b.show(); cout << endl;
	cin >> number >> name >> seats;
	a.set(number, name, seats);
	a.show(); cout << endl;
	return 0;
}

以上为第十一周的基础练习。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值