cpp
#include<iostream>
using namespace std;
class tuxing{
protected:
double r;
public:
tuxing(int r1)
{
r=r1;
}
tuxing()
{
}
virtual double biaomianji()
{
return 0;
}
virtual double tiji()
{
return 0;
}
};
class zhengfangti:public tuxing{
public:
zhengfangti(double r1):tuxing(r1)
{
}
virtual double biaomianji()
{
return r*r*6;
}
virtual double tiji()
{
return r*r*r;
}
};
class yuan:public tuxing{
public:
yuan(double r1):tuxing(r1)
{
}
virtual double biaomianji()
{
return r*r*3.14/4;
}
virtual double tiji()
{
return r*r*r*3.14*3/4;
}
};
class yuanzhu:public tuxing
{
protected:
double h;
public:
yuanzhu(double r1,double h1):tuxing(r1)
{
h=h1;
}
virtual double biaomianji()
{
return 2*r*3.14*h;
}
virtual double tiji()
{
return r*r*3.14*h;
}
};
void TopPrint(tuxing&r )
{
cout<<r.biaomianji()<<endl;
cout<<r.tiji()<<endl;
}
int main()
{
tuxing p1(4),*p;
zhengfangti p2(4);
yuan p3(4);
yuanzhu p4(4,3);
p=&p2;
cout<<p->tiji()<<endl;
cout<<p->biaomianji()<<endl;
p=&p3;
cout<<p->tiji()<<endl;
cout<<p->biaomianji()<<endl;
p=&p4;
cout<<p->tiji()<<endl;
cout<<p->biaomianji()<<endl;
TopPrint(p2);
TopPrint(p3);
TopPrint(p4);
return 0;
}