计算机程序设计c++ 11-5:继承类设计

点,圆,圆柱体继承设计

点类

class Point
{
	private:
		int x, y;  //点的x和y坐标
	
	public:
		Point(int x=0, int y=0);  // 构造函数
		void SetPoint(int, int);  // 设置坐标
		int GetX(){return x;}     // 取私有变量x 
		int GetY(){return y;}     // 取私有变量y
		void Print();  			  // 输出点的坐标
}

类外定义函数

Point::Point(int a, int b)
{
	SetPoint(a, b);
}

Point::SetPoint(int a, int b)
{
	x = a;
	y = b;
}

Point::Print()
{
	cout << '[' << x << ", " << y << ']';
}

从点类派生出圆类

class Circle::public Point
{
	double radius;
	
	public:
		Circle(int x=0, int y=0, double r=0.0);
		void SetRadius( double ); //设置半径
		double GetRadius(); //取半径
		double Area(); //计算面积
		void Print(); //输出圆心坐标和半径
};

类外定义函数

Circle::Circle(int a, int b, double r): Point(a,b)
{
	SetRadius(r);
}

void Circle::SetRadius( double r ){ radius = ( r >= 0 ? r : 0 );}

double Circle::GetRadius(){ return radius; }

double Circle::Area() { return 3.14159 * radius * radius;}

void Circle::Print()
{
	cout << "Center = ";
	Point::Print();
	cout << "; Radius = " << radius << endl;
}

从圆类派生出圆柱体类

class Cylinder: public Circle
{
	double high;
	
	public:
		Cylinder(int x, int y, double r, double h);
		void Set_data(int x, int y, double r, double h); //修改数据
		double Area(); //计算表面积
		double Volume(); //计算体积
		void Print(); //输出圆心坐标和半径
};

类外定义函数

Cylinder::Cylinder(int x, int y, double r, double h):Circle(x, y, r)
{
	high = h;
}

void Cylinder::Set_data(int x, int y, double r, double h)
{
	SetPoint(x, y);
	SetRadius(r);
	high= h;
}

double Cylinder::Volume()
{
	return Circle::Area() * high;
}

double Cylinder::Area()
{
	double r = GetRadius();
	return 2*3.14159 * r * r + 2 * 3.1415 * r * high;
}

主函数测试

#include <iostream>
using namespace std;

int main()
{
	cout << "测试点类" << endl;
	Point p(30, 50);
	p.Print();
	
	cout << endl << endl << "测试圆类" << endl;
	Circle c(120, 80, 10.0);
	cout << "圆心:";
	c.Point::Print();
	cout << "\n圆面积: " << c.Area() << endl;

	cout<<endl<<"测试圆柱体类"<<endl;
	Cylinder cy(240, 160, 10.0, 10.0);
	cout << "圆柱体中心点: ";
	cy.Point::Print();
	cout << "\n圆柱体圆面积:" << cy.Circle::Area() << endl;
	cout << "圆柱体表面积:" << cy.Area() <<endl;
	cout << "圆柱体体积:" << cy.Volume() <<endl;
	return 0;
}

在这里插入图片描述

点->长方形->长方体

#include<iostream>
using namespace std;
class point {
		int x, y;

	public:
		point() {
			x = 0;
			y = 0;
		};
		
		void setdata(int x, int y) {
			this->x = x;
			(*this).y = y;
		};
		
		point(int x, int y) {
			setdata(x, y);
		};
		
		void zb() {
			cout << '(' << x << ',' << y << ')' << endl;
		};
		
		~point();
};

class square: public point {
		int length, width;
		point center;
	
	public:
		square():point(0, 0)
		{
			width = 0;
			length = 0;
		}
		
		void setdata(int l, int w) {
					length = l;
					width = w;
				};
		
		square(int x, int y, int l, int w):	point(x, y) 
		{
			setdata(l, w);
		};
	
		~square();
};

class cube:protected square {
		int hight;
	
	public:
		void setdata(int h)
		{
			hight=h;
		}
		
		cube():square(0, 0, 0, 0)
		{
			hight = 0;
		}
		
		cube(int x,int y,int l,int w,int h):square(x,y,l,w)
		{
			setdata(h);
		}
		
		~cube();
};
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

uncle_ll

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

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

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

打赏作者

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

抵扣说明:

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

余额充值