大家帮我看看这个程序有什么问题!谢谢了.

shuirh 2009-02-26 09:39:49
#include <iostream.h>
static const 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);
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; //这句报错

fun(point);

Rectangle rec(4, 5); //这句也报错

point = &rec;
fun(point);

Circle cir(10);
point = ○
fun(point);
}

大家帮我看看这3个地方如何修改啊?小弟我不胜感激了啊!
...全文
109 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
shuirh 2009-02-26
  • 打赏
  • 举报
回复
/*8-9 定义一个Shape抽象类,在此基础上派生出Rectangle和Circle,
*二者都有GetArea()函数计算对象的面积,GetPerim()函数计算对象的周长。
*/

#include <iostream.h>
static const pai = 3.1415926;

class Shape
{
public:
//基类的纯虚函数可以只声明不定义吗? //纯虚函数本来就只声明不定义
Shape()
{
cout << "Shape constructing" << endl;
}
virtual float GetArea() = 0;
virtual float GetPerim() = 0;
};

class Rectangle : public Shape
{
float lenth;
float width;
public:
Rectangle(float len, float wid)
{
lenth = len;
width = 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:";
return -1;
}

float Shape :: GetPerim()
{
cout << "the perim of Shape is:";
return -1;
}*/

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)
{
cout << point->GetArea() << endl;
cout << point->GetPerim() << endl;
}

main()
{
//Shape *point = new Shape; //Shape是一个抽象类,抽象类不能定义对象,不能分配内存空间
Shape *point_ = NULL;
Rectangle rec(4, 5);
Circle cir(10);

//fun(point_);

point_ = &rec;
fun(point_);

point_ = ○
fun(point_);

//delete point;
}

终于改对了.
我真的很差啊,我都不好意思发帖了.
但是大家很热情啊,对我的帮助非常大,谢谢你们了.
lq651659889 2009-02-26
  • 打赏
  • 举报
回复
你的是不是少了using namespace std;啊!要不你也要这样输出撒std::cout.
waizqfor 2009-02-26
  • 打赏
  • 举报
回复
错误不少啊 LZ再好好对对书

#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);
}

yangch_nhcmo 2009-02-26
  • 打赏
  • 举报
回复
改了一下,LZ再看看

#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);
}

lightnut 2009-02-26
  • 打赏
  • 举报
回复
1.
class Rectangle : public Shape
{
.....
Rectanlge(float len, float wid); //这个构造函数写错了: xxxxxglx不是xxxxxlgx
.....
}
2.
class Shape
{
public:
virtual float GetArea() = 0;
.....
}
这里=0表示Shape是个抽象基类, 不能创建其对象, 所以
Shape *point = new Shape
会报错!
lw1a2 2009-02-26
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;

static const double pai = 3.1415926;

class Shape
{
public:
virtual double GetArea() = 0;
virtual double GetPerim() = 0;
};

class Rectangle : public Shape
{
double lenth;
double width;
public:
Rectangle(double len, double wid);
double GetArea();
double GetPerim();
};

class Circle : public Shape
{
double radius;
public:
Circle(double r = 0);
double GetArea();
double GetPerim();
};

//double Shape :: GetArea()
//{
// cout << "the area of Shape is:";
//
//}
//
//double Shape :: GetPerim()
//{
// cout << "the perim of Shape is:";
//}

//这个构造函数有什么问题?
Rectangle :: Rectangle(double len, double wid)
{
lenth = len;
width = width;
}

double Rectangle :: GetArea()
{
cout << "the area of Rectangle is:";
return lenth * width;
}

double Rectangle :: GetPerim()
{
cout << "the perim of Rectangle is:";
return 2 * (lenth + width);
}

Circle :: Circle(double r)
{
radius = r;
}

double Circle :: GetArea()
{
cout << "the area of Circle is:";
return pai * radius * radius;
}

double 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; //不能创建纯虚类

//fun(point);

Rectangle rec(4, 5); //这句也报错

Shape *point = &rec;
fun(point);

Circle cir(10);
point = ○
fun(point);
}

错太多了,都不想改了,照书抄麻烦多对两遍

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧