面向对象程序设计-实验五 类的继承及派生类的使用

2022/5/2
1.设计一个点类Point, 再设计一个矩形类,矩形类使用Point类的两个坐标点作为矩形的对角顶点。在矩形类中使用内联函数输出矩形4个顶点坐标;再定义一个函数计算矩形的面积。要求写出Point和矩形两个类的完整定义,并写出主函数(main函数)的调用代码。
#include<iostream>
using namespace std;
class Point {
public:
	Point(int xx1 = 0, int yy1 = 0, int xx2 = 0, int yy2 = 0) :x1(xx1), y1(yy1), x2(xx2), y2(yy2) {}
	//构造函数
	friend class Rectangle;
protected:
	int x1, x2;//坐标1
	int y1, y2;//坐标2
};
class Rectangle :public Point {
public:
	Rectangle(int x1, int y1, int x2, int y2) :Point(x1, y1, x2, y2) {}
	//构造函数
	void display();
	void area();
private:
	int x3, x4;//坐标3
	int y3, y4;//坐标4
};
inline void Rectangle::display() {
	int x3 = x1, x4 = x2, y3 = y2, y4 = y1;
	cout << "(" << x1 << "," << y1 << ")" << endl;
	cout << "(" << x2 << "," << y2 << ")" << endl;
	cout << "(" << x3 << "," << y3 << ")" << endl;
	cout << "(" << x4 << "," << y4 << ")" << endl;
}//输出坐标
void Rectangle::area() {
	cout << "矩形的面积为:" << ((max(x1, x2) - min(x1, x2)) * (max(y1, y2) - min(y1, y2))) << endl;
}//计算面积
int main() {
	int a, b, c, d;
	cout << "请输入两个坐标:" << endl;
	cin >> a >> b >> c >> d;
	Rectangle r(a, b, c, d);
	cout << "矩形四个坐标顶点为:" << endl;
	r.display();
	r.area();
	return 0;
}
2.创建类Car和类MotorBike,其中,类Car中包含产地,颜色,座位数,车门数量,类MotorBike包含产地,颜色和气缸数。创建基类Vehicle,基类包含了类Car和类MotorBike的共有成员。main函数分别建立两个数组,数组大小均为10,一个数组为Car类型,另外一个为MotorBike。要求实现以下功能:

1)动态建立Car对象,添加到Car数组;
2)动态建立MotorBike对象,添加到MotorBike数组;
3)显示所有Car的信息;
4)显示所有MotorBike的信息;
5)移除所给出特定产地和颜色的Car;
6)显示给定颜色(如红色)的所有Vehicle的信息;

#include<iostream>
#include<string>
using namespace std;
class Vehicle {
public:
	Vehicle(){}
	Vehicle(string p, string c):produce(p),colour(c){}//构造函数
protected:
	string produce;//产地
	string colour;//颜色
};
class Car :public Vehicle {
public:
	Car(){}
	Car(string p, string c, int s, int d) :Vehicle(p, c), seats(s), doors(d) {}//构造函数
	void display();//输出信息
	string p();//返回产地
	string c();//返回颜色
private:
	int seats;//座位数
	int doors;//车门数
};
class MotorBike :public Vehicle {
public:
	MotorBike(){}
	MotorBike(string p, string c, int y) :Vehicle(p, c), cylinders(y) {}//构造函数
	void display();//输出信息
	string c();//返回颜色
private:
	int cylinders;//气缸数
};
void Car::display() {
	cout << "产地:" << produce << " 颜色:" << colour << " 座位数:" << seats << " 车门数:" << doors << endl;
}
string Car::p() {
	return produce;
}
string Car::c() {
	return colour;
}
void MotorBike::display() {
	cout << "产地:" << produce << " 颜色:" << colour << " 气缸数:" << cylinders << endl;
}
string MotorBike::c() {
	return colour;
}
int main() {
	Car* car = new Car[10]{ Car("浙江", "blue", 4, 4),Car("江苏", "yellow", 4, 4),Car("北京", "white", 6, 6),Car("重庆", "red", 4, 4),Car("河南", "green", 6, 6),Car("浙江", "blue", 4, 4),Car("浙江", "yellow", 4, 4),Car("浙江", "white", 6, 6),Car("浙江", "blue", 6, 6),Car("北京", "blue", 8, 8) };
	MotorBike* motorbike = new MotorBike[10]{ MotorBike("浙江", "blue", 4),MotorBike("江苏", "yellow", 4),MotorBike("北京", "white", 6),MotorBike("重庆", "red", 4),MotorBike("河南", "green", 6),MotorBike("浙江", "blue", 4),MotorBike("浙江", "yellow", 4),MotorBike("浙江", "white", 6),MotorBike("浙江", "blue", 6),MotorBike("北京", "blue", 6) };
	cout << "显示所有汽车信息" << endl;
	for (int i = 0; i < 10; i++) {
		car[i].display();
	}
	cout << endl;
	cout << "显示所有摩托车信息" << endl;
	for (int i = 0; i < 10; i++) {
		motorbike[i].display();
	}
	cout << endl;
	cout << "移除产地为浙江或者颜色为蓝色的汽车" << endl;
	int c = 10;
	for (int i = 0; i < c; i++) {
		if (car[i].p() == "浙江" || car[i].c() == "blue") {
			c = c - 1;
			for (int j = i; j < c; j++) {
				car[j] = car[j + 1];
			}
			i = i - 1;
		}
	}
	for (int i = 0; i < c; i++) {
		car[i].display();
	}
	cout << endl;
	cout << "显示颜色为白色的摩托车信息" << endl;
	for (int i = 0; i < 10; i++) {
		if (motorbike[i].c() == "white") {
			motorbike[i].display();
		}
	}
	return 0;
}
3.在2.的类Car中增加一个属性生产时间,要求生产时间定义为一个Date类。该题目需动态建立Car类的对象,完成对象数据成员的输入和输出。(需注意构造函数)
#include<iostream>
#include<string>
using namespace std;
class Vehicle {
public:
	Vehicle(string p, string c) :produce(p), colour(c) {}//构造函数
protected:
	string produce;//产地
	string colour;//颜色
};
class Date {
public:
	Date(int y, int m, int d):year(y),month(m),day(d){}//构造函数
	void display();//输出信息
private:
	int year;//年
	int month;//月
	int day;//日
};
class Car :public Vehicle {
public:
	Car(string p, string c, int s, int d,int y,int m,int a) :Vehicle(p, c), seats(s), doors(d),date(y,m,a) {}//构造函数
	void display();//输出信息
private:
	int seats;//座位数
	int doors;//车门数
	Date date;//生产日期
};
void Date::display() {
	cout << "生产日期:" << year << "年" << month << "月" << day << "日" << endl;
}
void Car::display() {
	cout << "产地:" << produce << " 颜色:" << colour << " 座位数:" << seats << " 车门数:" << doors << endl;
	date.display();
}
int main() {
	string produce;
	string colour;
	int seats;
	int doors;
	int year;
	int month;
	int day;
	cout << "请输入产地:";
	cin >> produce;
	cout << "请输入颜色:";
	cin >> colour;
	cout << "请输入座位数、车门数:";
	cin >> seats >> doors;
	cout << "请输入生产日期的年月日:";
	cin >> year >> month >> day;
	Car* car = new Car(produce, colour, seats, doors, year, month, day);
	cout << endl;
	cout << "输出汽车信息" << endl;
	car->display();
	return 0;
}
实验总结
  1. 注意继承和派生中构造函数的先后
  2. 注意数据成员初始化的方式
  3. 注意循环过程中判断的顺序
  4. 注意函数的作用范围
  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

闻闻闻闻笛声

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值