第十周任务三之点类及多重派生

源程序:

/*(文件注释头部开始) 
*程序的版权和版本声明部分 
*Copyright (c) 2011,烟台大学计算机学院学生 
*All rights reserved. 
*文件名称:point类及其多重派生类
*作    者:2011级计114-3张宗佳 
*完成日期:2011年4月24号 
*版本号:vc
* 对任务及求解方法的描述部分 
* 输入描术:
* 问题描述:
(1)先建立一个Point(点)类,包含数据成员x,y(坐标点);
(2)以Point为基类,派生出一个Circle(圆)类,增加数据成员 (半径);
(3)再以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,再增加数据成员h(高)。
要求编写程序,设计出各类中基本的成员函数(包括构造函数、析构函数、修改数据成员和获取数据成员的公共接口、用于输出的重载运算符
“<<”函数等),使之能用于处理以上类对象,最后求出圆格柱体的表面积、体积并输出。
(提示:此任务可以分为三个子任务分成若干步骤进行。先声明基类,再声明派生类,逐级进行,分步调试。——这种方法适用于做任何的
项目)
* 程序输出:
* 程序头部的注释结束 
*/  
#include<iostream>

using namespace std;

class Point
{
public:

	Point(double a = 0, double b = 0);

	void setpoint(double,double);

	double getx();
	double gety();
	friend ostream & operator << (ostream &output,Point &p);

	~Point();

protected:

	double x;

	double y;

};
Point::Point(double a, double b)
{
	x = a;
	y = b;
}
void Point::setpoint(double a,double b)
{
	x = a;
	y = b;
}
double Point::getx()
{
	return x;
}
double Point::gety()
{
	return y;
}
Point::~Point(){}
ostream & operator << (ostream &output, Point &p)
{
	output << "圆心是:" << p.x << '\t' << p.y;

	return output;
}
class Circle:public Point
{
public:
	Circle(double a = 0,double b = 0,double c = 0);

	double getr();
	double getarea();

	friend ostream & operator << (ostream &output, Circle &c);

	void setcircle(double);
	~Circle();
protected:
	double r;
};
Circle::Circle(double a,double b,double c):Point(a,b)
{
	r = c;
}
void Circle::setcircle(double c)
{
	r = c;
}


double Circle::getr()
{
	return r;
}
double Circle::getarea()
{
	return 3.14 * r * r;
}
ostream & operator << (ostream &output,Circle &c)
{
	output <<"圆心是:(" << c.x << "," << c.y << ")" << "半径是:" << c.r;
	return output;
}

Circle::~Circle(){}
class Cylinder:public Circle
{
public:
	Cylinder(double a = 0,double b = 0,double c = 0,double d = 0);
	double geth();
	double area();
	double volume();
	void setcylinder(double);
	friend ostream & operator << (ostream &output, Cylinder &c);
	~Cylinder();
private:
	double h;
};
Cylinder::Cylinder(double a,double b,double c,double d):Circle(a,b,c)
{
	h = d;
}
void Cylinder::setcylinder(double d)
{
	h = d;
}
double Cylinder::geth()
{
	return h;
}
double Cylinder::area()
{
	return 2 * 3.14 * r * r + 2 * 3.14 * r * h;
}
double Cylinder::volume()
{
	return h * Circle::getarea();
}
ostream & operator << (ostream &output,Cylinder &c)
{
	output <<"圆心是:(" << c.x << "," << c.y << ")" << "半径是:" << c.r << "高是:" << c.h << "表面积是:" << c.area() << "体积是:" << c.volume();
	return output;
}
Cylinder::~Cylinder(){}
int main()
{
	Cylinder c(3,4,2.5,5);

	cout << "圆心是:(" << c.getx() << "," << c.gety() << ")" << "半径是:" << c.getr() << "高是:" << c.geth() << "表面积是:" << c.area() << "体积是:" << c.volume() << endl;

	c.setpoint(3,5);
	c.setcircle(5);
	c.setcylinder(4);
	cout << c;
	system("pause");
	return 0;
}

实验结果:

圆心是:(3,4)半径是:2.5高是:5表面积是:117.75体积是:98.125
圆心是:(3,5)半径是:5高是:4表面积是:282.6体积是:314请按任意键继续. . .

经验积累:

1.多重派生,可以分成若干部来写,就像老师说的:先声明基类,再声明派生类,逐级进行,分步调试。

2.基类和第一个派生出来的类中的数据成员要定义成prected型,这样,派生出来的类才能访问其数据成员,开始定义成了private了,运行没通过,才发觉是这里出了问题...“爸妈的东西不能碰了,好郁闷”....



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值