描述
圆形、三角形和长方形都可以看做是多边形。定义一个shape(包含两个整型数据成员,代表图形的中心)。在此基础上,派生出圆形类,三角形类,长方形类。要求:
每个类都需要定义构造函数
定义一组set 、get函数,完成对数据成员的操作
重载>>,<<< span="">运算符函数,完成输入、输出操作
在圆形类,三角形类,长方形类中定义perimeter函数,计算图形的周长
输入
圆的半径
三角形的三条边的长度
长方形的长和宽
输出
圆的周长
三角形的周长
长方形的周长
说明:结果保留两位小数
样例输入
1
1 1 1.41
1 1
样例输出
6.28
3.41
4.00
这个题emmmmm有点无聊啊,这题目要求像是为了练习而强行搬上去的,很多函数和数据成员没有任何意义,反正我没觉得继承的这个图形中心并没有什么用,但是题目怎么要求咱们照做就好了
#include <iostream>
#include <iomanip>
using namespace std;
class shape{
int x,y;
};
class circular:public shape{
int r;
double C;
public:
circular(int R):r(R){}
void set(int m)
{
r=m;
}
void get()
{
cout<<fixed<<setprecision(2)<<C<<endl;
}
double perimeter()
{
double pi=3.1415;
C=pi*r*2;
return C;
}
friend istream & operator >> (istream &input, circular &p){
cin>>p.r;
return input;
}
friend ostream & operator << (ostream &output, circular &p){
cout<<fixed<<setprecision(2)<<p.C<<endl;
return output;
}
};
class triangle:public shape{
double a,b,c,C;
public:
triangle(double A,double B,double Cc):a(A),b(B),c(Cc){}
void set(double m,double n,double p)
{
a=m;
b=n;
c=p;
}
void get()
{
cout<<fixed<<setprecision(2)<<C<<endl;
}
double perimeter()
{
C=a+b+c;
return C;
}
friend istream & operator >> (istream &input, triangle &p){
cin>>p.a>>p.b>>p.c;
return input;
}
friend ostream & operator << (ostream &output, triangle &p){
cout<<fixed<<setprecision(2)<<p.C<<endl;
return output;
}
};
class rectangle:public shape{
double a,b,C;
public:
rectangle(double A,double B):a(A),b(B){}
void set(double m,double n)
{
a=m;
b=n;
}
void get()
{
cout<<fixed<<setprecision(2)<<C<<endl;
}
double perimeter()
{
C=2*a+b*2;
return C;
}
friend istream & operator >> (istream &input, rectangle &p){
cin>>p.a>>p.b;
return input;
}
friend ostream & operator << (ostream &output, rectangle &p){
cout<<fixed<<setprecision(2)<<p.C<<endl;
return output;
}
};
int main()
{
double r,a,b,c,l,w;
cin>>r>>a>>b>>c>>l>>w;
circular A(r);
triangle B(a,b,c);
rectangle C(l,w);
A.perimeter();
B.perimeter();
C.perimeter();
A.get();
B.get();
C.get();
return 0;
}