[Error] expected primary-expression before ';' token ,错误类似这个,求原因及解答 5
#include<iostream>usingnamespacestd;classFirgure{protected:floatx_size;floaty_size;pu...
#include<iostream>
using namespace std;
class Firgure
{protected:
float x_size;
float y_size;
public:
Firgure(float a,float b)
{x_size=a;y_size=b;}
virtual float area()const=0;
};
class Triangle:public Firgure
{public:
Triangle(float a,float b): Firgure(a,b){}
virtual float area() const
{return 1/2*x_size*y_size;}
};
class Rectangle:public Firgure
{
public:
Rectangle(float a,float b): Firgure(a,b){}
virtual float area() const
{return x_size*y_size;}
};
class Circle:public Firgure
{public:
Circle(float a,float b): Firgure(a,b){}
virtual float area() const
{return 3.14*x_size*x_size;}
};
int main()
{Firgure *p;
Triangle a1(3,6);Rectangle b1(5,8);Circle c1(2,0);
p=&Triangle;
p->area();
cout<<"三角形的面积为"<<p->area()<<endl;
p=&Rectangle;
p->area();
cout<<"矩形的面积为"<<p->area()<<endl;
p=&Triangle;
p->Circle();
cout<<"圆的面积为"<<p->area()<<endl;
return 0;
} 展开
using namespace std;
class Firgure
{protected:
float x_size;
float y_size;
public:
Firgure(float a,float b)
{x_size=a;y_size=b;}
virtual float area()const=0;
};
class Triangle:public Firgure
{public:
Triangle(float a,float b): Firgure(a,b){}
virtual float area() const
{return 1/2*x_size*y_size;}
};
class Rectangle:public Firgure
{
public:
Rectangle(float a,float b): Firgure(a,b){}
virtual float area() const
{return x_size*y_size;}
};
class Circle:public Firgure
{public:
Circle(float a,float b): Firgure(a,b){}
virtual float area() const
{return 3.14*x_size*x_size;}
};
int main()
{Firgure *p;
Triangle a1(3,6);Rectangle b1(5,8);Circle c1(2,0);
p=&Triangle;
p->area();
cout<<"三角形的面积为"<<p->area()<<endl;
p=&Rectangle;
p->area();
cout<<"矩形的面积为"<<p->area()<<endl;
p=&Triangle;
p->Circle();
cout<<"圆的面积为"<<p->area()<<endl;
return 0;
} 展开
1个回答
展开全部
你的程序的主要问题是没有分清类和对象的区别,对于指针p是指向对象的,而不是指向类的
你的C++程序我帮你改完了,你看看吧(改动的地方见注释)
#include<iostream>
using namespace std;
class Firgure
{protected:
float x_size;
float y_size;
public:
Firgure(float a,float b)
{x_size=a;y_size=b;}
virtual float area()const=0;
};
class Triangle:public Firgure
{public:
Triangle(float a,float b): Firgure(a,b){}
virtual float area() const
{return 1.0/2*x_size*y_size;} //这里把1/2改成1.0/2
};
class Rectangle:public Firgure
{
public:
Rectangle(float a,float b): Firgure(a,b){}
virtual float area() const
{return x_size*y_size;}
};
class Circle:public Firgure
{public:
Circle(float a,float b): Firgure(a,b){}
virtual float area() const
{return 3.14*x_size*x_size;}
};
int main()
{Firgure *p;
Triangle a1(3,6);Rectangle b1(5,8);Circle c1(2,0);
p=&a1; //这里把p=&Triangle;改成p=&a1;
//p->area();//这里去掉这句
cout<<"三角形的面积为"<<p->area()<<endl;
p=&b1; //这里把p=&Rectangle;改成p=&b1;
//p->area();//这里去掉这句
cout<<"矩形的面积为"<<p->area()<<endl;
p=&c1; //这里把p=&Triangle;改成p=&c1;
//p->Circle();//这里去掉这句
cout<<"圆的面积为"<<p->area()<<endl;
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询