Homework8_ch7 继承和派生(1)

  1. 继承和派生类的设计

点类(Point),派生圆类(Circle)和矩形类(Rectangle)。如下图:

 

点类中:

私有数据成员int x int y ,分别表示横坐标和纵坐标;

      构造函数Point(),将xy都初始化为0

       构造函数Point(int xx,int yy),将xy分别初始化为xxyy的值

       析构函数~Point()

移动函数void Move(int xOff,int yOff) x沿横轴移动xOff个位置,将y沿纵轴移动yOff个位置

写数据函数void Set(int xx,int yy),将xy分别赋值为xxyy的值

输出函数void Display() 以形式“Point(x,y)”输出点的信息

矩形类公有继承点类:

       增加了:

              私有数据成员int w int h ,表示宽和高

构造函数Rectangle(),将宽和高都初始化为0;(思考:将左下角顶点初始化为了哪个点?如何实现的)

构造函数Rectangle(int xx,int yy,int ww,int hh),将左下角顶点初始化为Point(xx,yy),将宽初始化为ww,将高初始化为hh ;

析构函数~Rectangle ()

写数据函数void Set(int xx,int yy,int ww,int hh),将xy分别赋值为xxyy的值,将wh分别赋值为wwhh的值

面积函数float Area() 计算并返回矩形的面积

周长函数float Circum() 计算并返回矩形的周长

输出函数void Display() 以形式“Rectangle((x,y),w,h),面积,周长”输出矩形的信息

圆类公有继承点类:

       增加了:

              私有数据成员int r ,表示半径

构造函数Circle(),将半径初始化为0;(思考:将圆心初始化为了哪个点?如何实现的)

构造函数Circle(int xx,int yy,int rr),将圆心初始化为Pint(xx,yy),将半径初始化为rr ;

析构函数~Circle()

写数据函数void Set(int xx,int yy,int rr),将xy分别赋值为xxyy的值,将r赋值为rr的值

面积函数float Area() 计算并返回圆的面积

周长函数float Circum() 计算并返回圆的周长

输出函数void Display() 以形式“Circle((x,y),r),面积,周长”输出圆的信息

main()函数中:

创建矩形R1 左下角顶点(22),宽3,高4

以形式“Rectangle((x,y),w,h),面积,周长”输出矩形R1的信息

R1向右移动3个单位,向上移动4个单位, 再次输出其所有的信息;

创建圆C1      圆心(12),半径3

以形式“Circle((x,y),r),面积,周长”输出圆C1的信息

C1向右移动3个单位,向上移动4个单位, 再次输出其所有的信息;

创建圆的数组C2[3] 将三个圆分别设置为:

圆心(0,0),半径1

圆心(1,1),半径2

圆心(5,5),半径5

以形式“Circle((x,y),r),面积,周长”输出这3个圆的信息

//point.h
#pragma once
class Point {
public:
	Point();
	Point(int xx, int yy);
	~Point();
	void Move(int xOff, int yOff);
	void Set(int xx, int yy);
	void Display();
	int getX();
	int getY();
private:
	int x, y;
};
//circle.h
#pragma once
#include"point.h"
class Circle : public Point
{
public:
	Circle();
	Circle(int xx, int yy, int rr);
	~Circle();
	void Set(int xx, int yy, int rr);
	float Area();
	float Circum();
	void Display();
private:
	int r;
};
//rectangle.h
#pragma once
#include"point.h"
class Rectangle : public Point {
public:
	Rectangle();
	Rectangle(int xx, int yy, int ww, int hh);
	~Rectangle();
	void Set(int xx, int yy, int ww, int hh);
	//void Move(int xOff, int yOff);
	float Area();
	float Cricum();
	void Display();
private:
	int w, h;
};
//point.cpp
#include "point.h"
#include<iostream>
Point::Point()
{
	x = y = 0;
}

Point::Point(int xx, int yy)
{
	x = xx;
	y = yy;
}

Point::~Point()
{
}

void Point::Move(int xOff, int yOff)
{
	x += xOff;
	y += yOff;
}

void Point::Set(int xx, int yy)
{
	x = xx;
	y = yy;
}

void Point::Display()
{
	std::cout << "Point(" << x << "," << y << ")" << std::ends;
}

int Point::getX()
{
	return x;
}

int Point::getY()
{
	return y;
}
//circle.cpp
#include"circle.h"
#include"point.h"
#include<iostream>
#include<cmath>
const double PI = acos(-1);
Circle::Circle()
{
	this->Point::Point();
	r = 0;
}

Circle::Circle(int xx, int yy, int rr)
{
	this->Point::Point(xx, yy);
	r = rr;
}

Circle::~Circle()
{
}

void Circle::Set(int xx, int yy, int rr)
{
	this->Point::Set(xx, yy);
	r = rr;
}

float Circle::Area()
{
	return r*r*PI;
}

float Circle::Circum()
{
	return 2*r*PI;
}

void Circle::Display()
{
	std::cout << "Circle((" << std::ends;
	std::cout << this->getX() << "," << this->getY() << std::ends;
	std::cout<<	")," << r << "), " << std::ends;
	std::cout << this->Area() << ", " << this->Circum() << std::endl;
}

//rectangle.cpp
#include"rectangle.h"
#include<iostream>
Rectangle::Rectangle()
{
	w = 0;
	h = 0;
}

Rectangle::Rectangle(int xx, int yy, int ww, int hh)
{
	this->Point::Point(xx, yy);
	w = ww;
	h = hh;
}

Rectangle::~Rectangle()
{
}

void Rectangle::Set(int xx, int yy, int ww, int hh)
{
	this->Point::Set(xx, yy);
	w = ww;
	h = hh;
}
/*
void Rectangle::Move(int xOff, int yOff)
{
	this->Point::Move(xOff, yOff);
}
*/
float Rectangle::Area()
{
	return w * h * 1.0;
}

float Rectangle::Cricum()
{
	return 2.0 * (w + h);
}

void Rectangle::Display()
{
	std::cout << "Rectangle((" << std::ends;
	std::cout << this->getX() << "," << this->getY() << ")" << std::ends;
	std::cout << "," << w << "," << h << ")," << std::ends;
	std::cout << this->Area() << " " << this->Cricum() << std::endl;
}
//main.cpp
#include"circle.h"
#include"point.h"
#include"rectangle.h"
#include<iostream>

int main() {
	std::cout << "创建矩形R1: 左下角顶点(2,2),宽3,高4" << std::endl;
	Rectangle R1(2, 2, 3, 4);
	std::cout << "输出R1信息" << std::endl;
	R1.Display();
	std::cout << "将R1向右移动3个单位,向上移动4个单位" << std::endl;
	R1.Point::Move(3, 4);
	std::cout << "再次输出其所有的信息" << std::endl;
	R1.Display();
	std::cout << "创建圆C1:	圆心(1,2),半径3" << std::endl;
	Circle C1(1, 2, 3);
	std::cout << "输出C1信息" << std::endl;
	C1.Display();
	std::cout << "将C1向右移动3个单位,向上移动4个单位" << std::endl;
	C1.Point::Move(3, 4);
	std::cout << "再次输出其所有的信息" << std::endl;
	C1.Display();
	std::cout << "创建圆的数组C2[3], 将三个圆分别设置为:" << std::endl;
	Circle C2[3];
	std::cout << "圆心(0, 0), 半径1;" << std::endl;
	C2[0].Set(0, 0, 1);
	std::cout << "圆心(1, 1), 半径2;" << std::endl;
	C2[1].Set(1, 1, 2);
	std::cout << "圆心(5, 5), 半径5;" << std::endl;
	C2[2].Set(5, 5, 5);
	std::cout << "输出这3个圆的信息" << std::endl;
	for (int i = 0; i < 3; i++) {
		C2[i].Display();
	}
	return 0;
}

 

  1. 某校每位学生都要学习英语、语文、数学三门公共课程以及不同的专业课程。会计学专业要学习会计学和经济学两门课程,化学专业要学习有机化学和化学分析两门课程。

编程序管理学生成绩,计算公共课的总分和平均分,以及所有课程的总成绩。

#pragma once
#include<string>
using namespace std;
class PublicCourse {
public:
	PublicCourse(string s="\0", float e = 0, float c = 0, float m = 0);
	void Set(string s, float e = 0, float c = 0, float m = 0);
	float PublicTotal();
	float PublicAve();
	float getEnglish() {
		return English;
	}
	float getChinese() {
		return Chinese;
	}
	float getMath() {
		return Math;
	}
	int getID() {
		return ID;
	}
	string getName() {
		return name;
	}
	static int num;
private:
	float English;
	float Chinese;
	float Math;
	int ID;
	string name;
};
//publiccouse.cpp
#include"publiccourse.h"

PublicCourse::PublicCourse(string s,float e, float c, float m)
{
	English = e;
	Chinese = c;
	Math = m;
	ID = ++num;
	name = s;
}

float PublicCourse::PublicTotal()
{
	return English+Chinese+Math;
}

float PublicCourse::PublicAve()
{
	return this->PublicTotal()/3;
}
void PublicCourse::Set(string s,float e, float c, float m)
{
	English = e;
	Chinese = c;
	Math = m;
	name = s;
}
//accountant.h
#pragma once
#include"publiccourse.h"
class Accountant : public PublicCourse {
public:
	Accountant(string s="\0", float e = 0, float c = 0, float m = 0, float AC = 0, float EC = 0);
	float Total();
	void Set(string s, float e = 0, float c = 0, float m = 0, float AC = 0, float EC = 0);
	void Display();
private:
	float Accountancy;
	float Economic;
};
//accountant.cpp
#include"accountant.h"
#include<iostream>
using namespace std;
Accountant::Accountant(string s, float e, float c, float m, float AC, float EC):PublicCourse(s,e,c,m)
{
	//this->PublicCourse::PublicCourse(s, e, c, m);
	Accountancy = AC;
	Economic = EC;
}
void Accountant::Set(string s, float e, float c, float m, float AC, float EC)
{
	this->PublicCourse::Set(s, e, c, m);
	Accountancy = AC;
	Economic = EC;
}
float Accountant::Total()
{
	return this->PublicTotal()+Accountancy+Economic;
}
void Accountant::Display()
{
	cout << "序号:" << this->getID() << endl;
	cout << "姓名:" << this->getName() << endl;
	cout << "———————成绩————————" << endl;
	cout << "英语:" << getEnglish() << endl;
	cout << "语文:" << getChinese() << endl;
	cout << "数学:" << getMath() << endl;
	cout << "会计学:" << Accountancy << endl;
	cout << "经济学:" << Economic << endl;
	cout << "公共课总分:" << this->PublicTotal() << "   " << "公共课平均成绩:" << this->PublicAve() << endl;
	cout << "所有课程总成绩:" << this->Total() << endl;
}
//Chemistry.h
#pragma once
#include"publiccourse.h"
class Chemistry :public PublicCourse {
public:
	Chemistry();
	Chemistry(string s, float e, float c, float m, float oc, float ca);
	float Total();
	void Set(string s, float e = 0, float c = 0, float m = 0, float AC = 0, float EC = 0);
	void Display();
private:
	float OC;
	float CA;
};
//Chemistry.cpp
#include"Chemistry.h"
#include<iostream>
using namespace std;
Chemistry::Chemistry():PublicCourse()
{
	OC = 0;
	CA = 0;
}

Chemistry::Chemistry(string s, float e, float c, float m, float oc, float ca)
{
	this->PublicCourse::PublicCourse(s, e, c, m);
	OC = oc;
	CA = ca;
}

float Chemistry::Total()
{
	return this->PublicTotal()+OC+CA;
}
void Chemistry::Set(string s, float e, float c, float m, float AC, float EC)
{
	this->PublicCourse::Set(s, e, c, m);
	OC = AC;
	CA = EC;
}
void Chemistry::Display()
{
	cout << "序号:" << this->getID() << endl;
	cout << "姓名:" << this->getName() << endl;
	cout << "———————成绩————————" << endl;
	cout << "英语:" << getEnglish() << endl;
	cout << "语文:" << getChinese() << endl;
	cout << "数学:" << getMath() << endl;
	cout << "有机化学:" << OC << endl;
	cout << "化学分析:" << CA << endl;
	cout << "公共课总分:" << this->PublicTotal() << "   " << "公共课平均成绩:" << this->PublicAve() << endl;
	cout << "所有课程总成绩:" << this->Total() << endl << endl;
}
//main.cpp
#include"publiccourse.h"
#include"accountant.h"
#include"Chemistry.h"
#include<iostream>
using namespace std;
int PublicCourse::num = 0;
int main() {
	int n, m;
	cout << "请输入会计学和化学学生人数:";
	cin >> n >> m;
	Accountant* a = new Accountant[n];
	Chemistry* c = new Chemistry[m];
	float e, ch, ma, ac, ec;
	string s;
	for (int i = 0; i < n; i++) {
		cout << "请输入第"<<i+1<<"位会计学学生的成绩(姓名 英语 语文 数学 会计学 经济学):";
		cin >> s >> e >> ch >> ma >> ac >> ec;
		a[i].Set(s, e, ch, ma, ac, ec);
	}
	for (int i = 0; i < n; i++) {
		a[i].Display();
	}
	for (int i = 0; i < m; i++) {
		cout << "请输入第"<<i+1<<"位化学学生的成绩(姓名 英语 语文 数学 会计学 经济学):";
		cin >> s >> e >> ch >> ma >> ac >> ec;
		c[i].Set(s, e, ch, ma, ac, ec);
	}
	for (int i = 0; i < m; i++) {
		c[i].Display();
	}
	return 0;

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值