?1.基类指针数组??
#include <bits/stdc++.h>
#define PI 3.1415926
using namespace std;
class Shape{
public:
Shape(){}
virtual float girth() =0;//基类求周长函数
virtual float area() =0;//基类求面积函数
virtual void display() =0;//基类输出虚函数
~Shape(){}
};
class Circle:public Shape{
protected:
float radius;//半径
public:
Circle(){}
void setRadius(float r);
virtual float girth();
virtual float area();
virtual void display();
~Circle(){}
};
void Circle::setRadius(float r)
{
radius = r;
}
float Circle::girth(){
return 2*PI*radius;
}
float Circle::area(){
return PI*radius*radius;
}
void Circle::display(){
cout<<"圆的周长为:"<<Circle::girth()<<endl;
cout<<"圆的面积为:"<<Circle::area()<<endl;
}
class Square:public Shape{
protected:
float len;
public:
Square(){}
void setLen(float l);
virtual float girth();
virtual float area();
virtual void display();
~Square(){}
};
void Square::setLen(float l)
{
len = l;
}
float Square::girth(){
return 4*len;
}
float Square::area(){
return len*len;
}
void Square::display(){
cout<<"正方形的周长为:"<<Square::girth()<<endl;
cout<<"正方形的面积为:"<<Square::area()<<endl;
}
class Rectangle:public Shape{
protected:
float lon;
float wide;
public:
Rectangle(){}
void setLW(float l,float w);
virtual float girth();
virtual float area();
virtual void display();
~Rectangle(){}
};
void Rectangle::setLW(float l,float w)
{
lon = l;
wide = w;
}
float Rectangle::girth(){
return 2*(lon+wide);
}
float Rectangle::area(){
return lon*wide;
}
void Rectangle::display(){
cout<<"长方形的周长为:"<<Rectangle::girth()<<endl;
cout<<"长方形的面积为:"<<Rectangle::area()<<endl;
}
class Trapezoid:public Shape{
protected:
float shang;
float xia;
float gao;
float zuo;//左腰
float you;//右腰
public:
Trapezoid(){}
void setData(float s,float x,float g,float z,float y);
virtual float girth();
virtual float area();
virtual void display();
~Trapezoid(){}
};
void Trapezoid::setData(float s,float x,float g,float z,float y)
{
shang = s;
xia = x;
gao = g;
zuo = z;
you = y;
}
float Trapezoid::girth(){
return (shang+xia+zuo+you);
}
float Trapezoid::area(){
return (shang+xia)*gao/2;
}
void Trapezoid::display(){
cout<<"梯形的周长为:"<<Trapezoid::girth()<<endl;
cout<<"梯形的面积为:"<<Trapezoid::area()<<endl;
}
class Triangle:public Shape{
protected:
float di;
float zuo;//左边
float you;//右边
float gao;
public:
Triangle(){}
void set(float d,float z,float y,float g);
virtual float girth();
virtual float area();
virtual void display();
~Triangle(){}
};
void Triangle::set(float d,float z,float y,float g)
{
di = d;
zuo = z;
you = y;
gao = g;
}
float Triangle::girth(){
return di+zuo+you;
}
float Triangle::area(){
return di*gao/2;
}
void Triangle::display(){
cout<<"三角形的周长为:"<<Triangle::girth()<<endl;
cout<<"三角形的面积为:"<<Triangle::area()<<endl;
}
int main()
{
float r,s,lon,wid;
float shang,xia,zuoyao,youyao,gao;//梯形
float di,h,zuo,you;//三角形
cout<<"请输入圆的半径:";
cin>>r;
cout<<"请输入正方形的边长:";
cin>>s;
cout<<"请输入长方形的长、宽:";
cin>>lon>>wid;
cout<<"请输入梯形的上底、下底、高、左腰长、右腰长:";
cin>>shang>>xia>>gao>>zuoyao>>youyao;
cout<<"请输入三角形的底、左边长度、右边长度、高:";
cin>>di>>zuo>>you>>h;
Circle circle;
Square square;
Rectangle rectangle;
Trapezoid trapezoid;
Triangle triangle;
circle.setRadius(r);
square.setLen(s);
rectangle.setLW(lon,wid);
trapezoid.setData(shang,xia,gao,zuoyao,youyao);
triangle.set(di,zuo,you,h);
Shape *p[6]={&circle,&square,&rectangle,&trapezoid,&triangle};//定义基类指针数组,使之每个成员指向一个派生类对象
int i,girthsum=0,areasum=0;
for(i=0;i<5;i++){
girthsum +=p[i]->girth();
areasum +=p[i]->area();
p[i]->display();
}
cout<<"****************************"<<endl;
cout<<"所有图形周长之和:"<<girthsum<<endl;
cout<<"所有图形面积之和:"<<areasum<<endl;
return 0;
}
|