求高手帮忙、、、、
#include<stdio.h>#include<iostream.h>
class base{
public:
virtual double getArea()=0;
};
class square:public base{
public:
square(double L){
Long=L;
}
double getArea(){
return Long*Long;
}
private:
double Long;
};
class circle{
public:
circle(double r){
R=r;
}
double getArea(){
return 3.14*R*R;
}
private:
double R;
};
int main(){
double s1,s2;
base *pd;
square Sq(2.0);
circle Cir(3.0);
pd=&Sq;
s1=pd->getArea();
pd=&Cir;
s2=pd->getArea();
cout<<"s1="<<s1<<endl;
cout<<"s2="<<s2<<endl;
return 0;
}
Compiling...
a.cpp
G:\BCheng\1\a.cpp(36) : error C2440: '=' : cannot convert from 'class circle *' to 'class base *'(问题就在这里,不知道怎么改)
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.
a.exe - 1 error(s), 0 warning(s)