2024-9-5 C++课后作业

include <iostream>
#include <cmath>

using namespace std;

class graphical{

	public:
		graphical(){}

		~graphical(){}
		
		virtual float calculate_area() = 0;

		virtual float calculate_perm() = 0;

	protected:

		float perm;
		float area;

};

class Rectangle: public graphical{

	public:
		Rectangle(){}

		Rectangle(float l, float w): length(l), wide(w){}

		~Rectangle(){}

		float calculate_area() override{
					
			area = length * wide;

			return area;

		}

		float calculate_perm() override{
			
			perm = ( length + wide ) * 2;
			
			return perm;
		}

	private:
		float length;
		float wide;

};

class Circle: public graphical{

	public:
		Circle(){}

		Circle(float r): radius(r){}

		~Circle(){}
		
		float calculate_area() override{
					
			area = M_PI * radius * radius;

			return area;

		}

		float calculate_perm() override{
			
			perm = 2 * M_PI * radius;
			
			return perm;
		}


	private:
		float radius;

};

class Triangle: public graphical{

	public:
		Triangle(){}

		Triangle(float a, float b, float c):L1(a), L2(b),L3(c){}

		~Triangle(){}
		
		float calculate_area() override{
					
			float p = (L1 + L2 + L3) / 2;
			area = sqrt(p * (p-L1)* (p-L2)* (p-L3));

			return area;

		}

		float calculate_perm() override{
			
			perm = L1 + L2 + L3;
			
			return perm;
		}

	private:
		float L1;
		float L2;
		float L3;

};

void calculation(graphical& rf){
	
	cout<<"area="<<rf.calculate_area()<<endl;
	cout<<"perm="<<rf.calculate_perm()<<endl;
	
}



int main(int argc, const char *argv[])
{
	
	Rectangle R(3,4);
	calculation(R);

	Circle C(6);
	calculation(C);

	Triangle T(3,4,5);
	calculation(T);

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值