多重派生类

声明 Point(点)类,由 Point类派生出 Circle(圆)类,再由 Circle类派生出Cylinder(圆柱体)类。将类的定义部分分别作为3个头文件,对它们的成员函数的声明部分分别作为3个源文件(cpp文件),在主函数中用#include命令把它们包含进来,形成一个完整的程序并运行。
1.代码部分:

(1)头文件“point.h”

#pragma once
#include<iostream>
using namespace std;
class point
{
public:
	point(float x = 0, float y = 0);
	void setPoint(float, float);
	float getX()const { return x; }
	float getY()const { return y; }
	friend ostream& operator<<(ostream&, const point&);
protected:
	float x, y;
};

(2)头文件“circle.h”

#pragma once
#include<iostream>
#include"point.h"
using namespace std;
class circle :public point
{
public:
	circle(float x = 0, float y = 0, float r = 0);
	void setRadius(float);
	float getRadius()const;
	float area()const;
	friend ostream& operator<<(ostream&, const circle&);
protected:
	float radius;
};

(3)头文件“cylinder.h”

#pragma once
#include<iostream>
#include"circle.h"
using namespace std;
class cylinder :public circle
{
public:
	cylinder(float x = 0, float y = 0, float r = 0, float h = 0);
	void setHeight(float);
	float getHeight()const;
	float area()const;
	float volume()const;
	friend ostream& operator<<(ostream&, const cylinder&);
protected:
	float height;
};

(4)源文件point.cpp

#include<iostream>
using namespace std;
#include"point.h"
point::point(float a, float b)
{
	x = a;
	y = b;
}
void point::setPoint(float a, float b)
{
	x = a; y = b;
}
ostream& operator<<(ostream& output, const point& p)
{
	output << "[" << p.x << "," << p.y << "]" << endl;
	return output;
}

(5)源文件circle.cpp

#include<iostream>
#include"circle.h"
using namespace std;
circle::circle(float a,float b,float r):point(a,b),radius(r){}
void circle::setRadius(float r)
{
	radius = r;
}
float circle::getRadius()const { return radius; }
float circle::area()const { return 3.14159 * radius * radius; }
ostream& operator<<(ostream& output, const circle& c)
{
	output << "Center=[" << c.x << "," << c.y << "],r=" << c.radius << ",area=" << c.area() << endl;
	return output;
}

(6)源文件cylinder.cpp

#include<iostream>
#include"cylinder.h"
using namespace std;
cylinder::cylinder(float a,float b,float r,float h):circle(a,b,r),height(h){}
void cylinder::setHeight(float h) { height = h; }
float cylinder::getHeight()const { return height; }
float cylinder::area()const
{
	return 2 * circle::area() + 2 * 3.14159 * radius * height;
}
float cylinder::volume()const
{
	return circle::area() * height;
}
ostream& operator<<(ostream& output, const cylinder& cy)
{
	output << "Center=[" << cy.x << "," << cy.y << "],r=" << cy.radius << ",h=" << cy.height
		<< "\narea=" << cy.area() << ",volume="
		<< cy.volume() << endl;
	return output;
}

(7)主函数部分

#include<iostream>
#include"point.h"
#include"circle.h"
#include"cylinder.h"
using namespace std;
int main()
{

	cylinder cy1(3.5, 6.4, 5.2, 10);
	cout << "original cylinder :\nx=" << cy1.getX() << "y=" << cy1.getY() << ",r=" << cy1.getRadius() << ",h="
		<< cy1.getHeight() << "\narea=" << cy1.area() << ",volume=" << cy1.volume() << endl;
	cy1.setHeight(15);
	cy1.setRadius(7.5);
	cy1.setPoint(5.5, 6.4);
	cout << "\nnew cylinder:\n" << cy1;
	point& pRef = cy1;
	cout << "\npRef as a point:" << pRef;
	circle& cRef = cy1;
	cout << "\ncRef as a circle:" << cRef;
	return 0;
}

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值