主题:[讨论]编译出现错误的一个简单程序
以下程序是先定义shape类 再派生circle和rectangle类 再有rectangle派生出squar类,可是编译时出现1.cpp
D:\c++\MSDev98\MyProjects\shap\1.cpp(35) : error C2228: left of '.getarea' must have class/struct/union type
D:\c++\MSDev98\MyProjects\shap\1.cpp(38) : error C2228: left of '.getarea' must have class/struct/union type
D:\c++\MSDev98\MyProjects\shap\1.cpp(41) : error C2228: left of '.getarea' must have class/struct/union type 错误。我想请教下哪位高手帮忙看看 因为我在主函数中已经定义了“s1”的类型了 为什么会出现如上错误 万分感谢!
#include<iostream.h>
#define pi 3.14
class shape{
public: shape(){}
~shape(){cout<<"shape distruct"<<endl;}
virtual double getarea(){return -1;}||*用于求面积函数*||
};
class circle:public shape{
public:
circle(double radius){ itsradius=radius;}
~circle(){cout<<"circle distruct"<<endl;}
double getarea(){return pi*itsradius*itsradius;}
private:
double itsradius;
};||*定义圆 并求其面积*||
class rectangle:public shape{
public:
rectangle(double x,double y){width=x;length=y;}
~rectangle(){cout<<"rectangle distruct"<<endl;}
double getarea(){ return width*length;}
private:
double width,length;
};
class squar:public rectangle{
public:
squar(double len);
~squar(){cout<<"squar distruct"<<endl;}
private:
double len;
};
squar::squar(double len):rectangle(len,len){}
void main()
{ shape *s1;
s1=new circle(5);
s1.getarea();
delete s1;
s1=new rectangle(8.3,9.6);
s1.getarea();
delete s1;
s1=new squar(2.5);
s1.getarea();
delete s1;
}
D:\c++\MSDev98\MyProjects\shap\1.cpp(35) : error C2228: left of '.getarea' must have class/struct/union type
D:\c++\MSDev98\MyProjects\shap\1.cpp(38) : error C2228: left of '.getarea' must have class/struct/union type
D:\c++\MSDev98\MyProjects\shap\1.cpp(41) : error C2228: left of '.getarea' must have class/struct/union type 错误。我想请教下哪位高手帮忙看看 因为我在主函数中已经定义了“s1”的类型了 为什么会出现如上错误 万分感谢!
#include<iostream.h>
#define pi 3.14
class shape{
public: shape(){}
~shape(){cout<<"shape distruct"<<endl;}
virtual double getarea(){return -1;}||*用于求面积函数*||
};
class circle:public shape{
public:
circle(double radius){ itsradius=radius;}
~circle(){cout<<"circle distruct"<<endl;}
double getarea(){return pi*itsradius*itsradius;}
private:
double itsradius;
};||*定义圆 并求其面积*||
class rectangle:public shape{
public:
rectangle(double x,double y){width=x;length=y;}
~rectangle(){cout<<"rectangle distruct"<<endl;}
double getarea(){ return width*length;}
private:
double width,length;
};
class squar:public rectangle{
public:
squar(double len);
~squar(){cout<<"squar distruct"<<endl;}
private:
double len;
};
squar::squar(double len):rectangle(len,len){}
void main()
{ shape *s1;
s1=new circle(5);
s1.getarea();
delete s1;
s1=new rectangle(8.3,9.6);
s1.getarea();
delete s1;
s1=new squar(2.5);
s1.getarea();
delete s1;
}