65,210
社区成员
发帖
与我相关
我的任务
分享
#include <iostream.h>
static const double pai = 3.1415926;
class Shape
{
public:
virtual float GetArea() = 0;
virtual float GetPerim() = 0;
};
class Rectangle : public Shape
{
float lenth;
float width;
public:
Rectangle(float len, float wid);
float GetArea();
float GetPerim();
};
class Circle : public Shape
{
float radius;
public:
Circle(float r = 0);
float GetArea();
float GetPerim();
};
float Shape :: GetArea()
{
cout << "the area of Shape is:";
}
float Shape :: GetPerim()
{
cout << "the perim of Shape is:";
}
//这个构造函数有什么问题?
Rectangle :: Rectangle(float len, float wid)
{
lenth = len;
width = width;
}
float Rectangle :: GetArea()
{
cout << "the area of Rectangle is:";
return lenth * width;
}
float Rectangle :: GetPerim()
{
cout << "the perim of Rectangle is:";
return 2 * (lenth + width);
}
Circle :: Circle(float r)
{
radius = r;
}
float Circle :: GetArea()
{
cout << "the area of Circle is:";
return pai * radius * radius;
}
float Circle :: GetPerim()
{
cout << "the perim of Circle is:";
return 2 * pai * radius;
}
void fun(Shape *point)
{
point->GetArea; //调用的形式错了
point->GetPerim;
}
main()
{
//Shape *point = new Shape; //这句报错
Shape *point;
fun(point);
Rectangle rec(4, 5); //这句也报错
point = &rec;
fun(point);
Circle cir(10);
point = ○
fun(point);
}
#include <iostream.h>
using namespace std;
static const int pai = 3.1415926; //要注明类型
class Shape
{
public:
virtual float GetArea() = 0;
virtual float GetPerim() = 0;
};
class Rectangle : public Shape
{
float lenth;
float width;
public:
//Rectanlge(float len, float wid); //这里打错了,导致下面调用函数时出错;
Rectangle(float len, float wid);
float GetArea();
float GetPerim();
};
class Circle : public Shape
{
float radius;
public:
Circle(float r = 0);
float GetArea();
float GetPerim();
};
float Shape :: GetArea()
{
cout << "the area of Shape is:";
}
float Shape :: GetPerim()
{
cout << "the perim of Shape is:";
}
//这个构造函数有什么问题? //因为上面打错字母,找不到函数出错
Rectangle :: Rectangle(float len, float wid)
{
lenth = len;
width = width;
}
float Rectangle :: GetArea()
{
cout << "the area of Rectangle is:";
return lenth * width;
}
float Rectangle :: GetPerim()
{
cout << "the perim of Rectangle is:";
return 2 * (lenth + width);
}
Circle :: Circle(float r)
{
radius = r;
}
float Circle :: GetArea()
{
cout << "the area of Circle is:";
return pai * radius * radius;
}
float Circle :: GetPerim()
{
cout << "the perim of Circle is:";
return 2 * pai * radius;
}
void fun(Shape *point)
{
point->GetArea(); //point->GetArea ==> point->GetArea()
point->GetPerim(); //point->GetPerim ==>point->GetPerim()
}
int main()
{
// Shape* point = new Shape; //Shape 是个抽象类,不能分配空间
Shape *point;
point = new Circle;
fun(point);
Rectangle rec(4, 5);
point = &rec;
fun(point);
Circle cir(10);
point = ○
fun(point);
}