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;
}
2024-9-5 C++课后作业
最新推荐文章于 2024-10-31 18:26:46 发布