注意点:
class base{protected://抽象类成员变量要用protected,而用private子类明显不可以调用
double x;
public: base(double a){x=a;}
virtual void s()=0;//加个const就不对了
virtual void v()=0;};
View Code
#include<iostream>
using namespace std;
class base
{
protected:
double x;
public:
base(double a){x=a;}
virtual void s()=0;//加个const就不对了
virtual void v()=0;
};
class fang:public base
{
public:
fang(double a):base(a){};
void s(){cout<<"the surface of fang is:"<<x*x*6<<endl;}
void v(){cout<<"the v of fang is:"<<x*x*base::x<<endl;}
};
class ball:public base
{
public:
ball(double a):base(a){}
void s(){cout<<"the surface of ball is:"<<x*x*3.14*4<<endl;}
void v(){cout<<"the v of ball is:"<<x*x*x*3.14*x/3*4<<endl;}
};
void main()
{
fang f(3.0);
f.s();
f.v();
ball b(3.0);
b.s();
b.v();
///////也可以用虚函数的指针
base *p;
p=&f;
p->s();
p->v();
p=&b;
p->s();
p->v();
}