创建一个矩形类 大家帮帮忙(新手)
题目:,
定义一个矩形类,有长len和宽wid两个属性,有成员函数计算居心面积area,并要求在main()中演示这个类 ,
class Rectangle
{
float len;//长
float wid;//宽
float area;//面积
public:
Rectangle(float x , y) //构造函数,
float area();求面积
void print();显示
};
//成员函数定义
Rectangle::Rectangle(float len1, wid1)
{
len=len1;
wid=wid1;
}
float Rectang::area(float len1, wid1)
{
float area1;
area1=len1*wid1;
return area1;
}
void Rectang::print()
{
cout<<"len"<<len<<"wid"<<wid<<"area"<<area;
}
//主程序部分
void main()
{
float len1, wid1, area1;
cout<<"Enter the lengh:\n";
cin>>len1;
cout<<"Enter the width:\n";
cin>>wid1;
Rectang Rectang1(len1, wid1);
Rectang1.area(len1, wid1);
Rectang1.print();
return 0;
}
我自己做的,感觉总是好像缺少什么,大家给改改,并提个建议
问题点数:10、回复次数:4Top
1 楼ATGO(ATGO)回复于 2005-09-17 15:51:27 得分 0
为什么这么多错误的?或者说是我看不懂.Top
2 楼jsjjms(专心研究嵌入式)回复于 2005-09-17 16:08:56 得分 0
错误:
1。Rectangle(float x , y) //构造函数, -〉参数???
下面也有类似的错误。
2。float area();求面积
void print();显示
没有参数,下面定义的时候怎么又有参数??
。。。。。。。。。。。。。。。。
楼主应该先看看书。Top
3 楼benlei999(醉明月)回复于 2005-09-17 16:12:51 得分 0
float Rectang::area(float len1, wid1)
{
float area1;
area1=len1*wid1;
return area1;
}-------------------------------------
你没有对成员变量area写值啊!打印的却是成员变量
改为:
float Rectang::area()//既然用了构造函数初始化,为什么不用成员变量呢?
{
area=len*wid;
return area;
}Top
4 楼jsjjms(专心研究嵌入式)回复于 2005-09-17 16:14:27 得分 10
下面已经改好。
楼主应该不是学计算机的吧?
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<iostream.h>
class Rectangle
{
public:
Rectangle(float x ,float y); //构造函数,
float sarea(float len1, float wid1);//求面积
void print();//显示
float len;//长
float wid;//宽
float area;//面积
};
//成员函数定义
Rectangle::Rectangle(float len1, float wid1)
{
len=len1;
wid=wid1;
}
float Rectangle::sarea(float len1, float wid1)
{
float area1;
area1=len1*wid1;
return area1;
}
void Rectangle::print()
{
cout<<"len"<<len<<"wid"<<wid<<"area"<<area;
}
//主程序部分
void main()
{
float len1, wid1, area1;
cout<<"Enter the lengh:\n";
cin>>len1;
cout<<"Enter the width:\n";
cin>>wid1;
Rectangle Rectang1(len1, wid1);
Rectang1.sarea(len1, wid1);
Rectang1.print();
}
Top




