#include<iostream>//不正确代码。。
using namespace std;
const double PI=3.1415926;
class shape
{
public:
virtual double getArea()
{
return -1;
}
virtual double getPerimeter()
{
return -1;
}
};
class circle:public shape
{
double radius;
public:
circle(double r=0)
{
radius=r;
}
double getArea()
{
return PI*radius*radius;
}
double getPerimeter()
{
return 2*PI*radius;
}
};
class rectangle:public shape
{
double length;
double width;
public:
rectangle(double le=0,double wi=0)
{
length=le;
width=wi;
}
double getArea()
{
return length*width;
}
double getPerimeter()
{
return 2*width+2*length;
}
};
class square:public rectangle
{
double sqlength;
public:
square(double sq)
{
sqlength=sq;
}
double getArea()
{
return sqlength*sqlength;
}
double getPerimeter()
{
return 4*sqlength;
}
};
void fun(shape* p,double *a,double *per)
{
*a=p->getArea();
*per=p->getPerimeter();
}
int main()
{
double area,perimeter;
shape*pp;
circle r(5);
pp=&r;
fun(pp,&area,&perimeter);
cout<<area<<endl;cout<<perimeter<<endl;
rectangle b(5,6);
pp=&b;
fun(pp,&area,&perimeter);
cout<<area<<endl;cout<<perimeter<<endl;
square c(20);
pp=&c;
fun(pp,&area,&perimeter);
cout<<area<<endl;cout<<perimeter<<endl;
}
#include <iostream>//转自http://blog.csdn.net/sxhelijian/article/details/7566500
using namespace std;
//定义抽象基类Shape
class Shape
{
public:
virtual double area() const =0; //纯虚函数
};
//定义Circle类
class Circle:public Shape
{
public:
Circle(double r):radius(r){} //结构函数
virtual double area() const {return 3.14159*radius*radius;}; //定义虚函数
protected:
double radius; //半径
};
//定义Rectangle类
class Rectangle:public Shape
{
public:
Rectangle(double w,double h):width(w),height(h){} //结构函数
virtual double area() const {return width*height;} //定义虚函数
protected:
double width,height; //宽与高
};
class Triangle:public Shape
{
public:
Triangle(double w,double h):width(w),height(h){} //结构函数
virtual double area() const {return 0.5*width*height;} //定义虚函数
protected:
double width,height; //宽与高
};
int main()
{
Circle c1(12.6),c2(4.9); //建立Circle类对象c1,c2,参数为圆半径
Rectangle r1(4.5,8.4),r2(5.0,2.5); //建立Rectangle类对象r1,r2,参数为矩形长、宽
Triangle t1(4.5,8.4),t2(3.4,2.8); //建立Triangle类对象t1,t2,参数为三角形底边长与高
Shape *pt[6]={&c1,&c2,&r1,&r2,&t1,&t2};
//定义基类指针数组pt,使它每一个元素指向一个派生类对象
double areas=0.0; //areas为总面积
for(int i=0;i<6;i++)
{
areas=areas+pt[i]->area();
}
cout<<"totol of all areas="<<areas<<endl; //输出总面积
system("pause");
return 0;
}
#include<iostream>//比较正确的写法?
using namespace std;
const double PI=3.1415926;
class sharp
{
public:
virtual double getArea()=0;
virtual double getPerimeter()=0;
};
class circle:public sharp
{
double radius;
public:
circle(double r=0)
{
radius=r;
}
double getArea()
{
return PI*radius*radius;
}
double getPerimeter()
{
return 2*PI*radius;
}
};
class rectangle:public sharp
{
double length;
double width;
public:
rectangle(double le=0,double wi=0)
{
length=le;
width=wi;
}
double getArea()
{
return length*width;
}
double getPerimeter()
{
return 2*width+2*length;
}
};
class square:public rectangle
{
//double sqlength;
public:
square(double sq):rectangle(sq,sq)
{
//sqlength=sq;
}
// double getArea()
// {
// return sqlength*sqlength;
// }
// double getPerimeter()
// {
// return 4*sqlength;
// }
};
void fun(sharp* p,double *a,double *per)
{
*a=p->getArea();
*per=p->getPerimeter();
}
int main()
{
double area,perimeter;
sharp *pp;
circle r(5);
pp=&r;
fun(pp,&area,&perimeter);
cout<<area<<endl;cout<<perimeter<<endl;
rectangle b(5,6);
pp=&b;
fun(pp,&area,&perimeter);
cout<<area<<endl;cout<<perimeter<<endl;
square c(20);
pp=&c;
fun(pp,&area,&perimeter);
cout<<area<<endl;cout<<perimeter<<endl;
//计算圆、矩形、正方形总面积
double totalarea=0;
sharp* pt[3]={&r,&b,&c};
for(int i=0;i<3;i++)
{
totalarea=totalarea+pt[i]->getArea();
}
cout<<totalarea<<endl;
}