急求!!!一道C++题目

设计一个程序;声明一个Rectangle类, 有数据成员length(长),width(宽),成员函数GetArea(),计算矩形的面积,在函数内输入矩形的长和宽,构造一个Rectanglede 对象,并初始化该对象的长,宽值,通过该对象的成员函数GetArea()求出相应的面积。

#include<iostream.h>
class Rectangle
{
public:
Rectangle(int x,int y)
{
length=x;
width=y;
}
~Rectangle(){}
int GetArea()
{
return length*width;
}
private:
int length,width;
};
void main()
{
int l,w,a;

cout<<"please input the length and the width:"<<endl;
cin>>l>>w;
Rectangle shape(l,w);
cout<<"the area of the shap is :"<<endl;
a=shape.GetArea();
cout<<a<<endl;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-03-07
哎..我还没学到C++啊...
第2个回答  2008-03-07
#include "stdafx.h"
#include <iostream>

using namespace std;
class Rectangle {
protected : double m_length;
double m_width;
public:
Rectangle(){};
Rectangle(double len,double wid){
this->m_length = len;
this->m_width = wid;
}

double getArea() {
return m_length*m_width;
}
};

void main() {

Rectangle rect(30.5,20.7);
cout<<"面积:"<<rect.getArea()<<endl;
}
按照你的要求最简单的,想复杂点可以自己再加,比如输入长宽之类的。
第3个回答  2008-03-07
#includ<iostream.h>
class Rectangle{
float length,width
Public:
Rectangle(){return 0;}
Rectangle(x,y){x=length;y=width;}
GetArea()
}
Class::GetArea()
{renturn x*y;}
void mian()
{
Rectangle Rectanglede;
cin>>X>>y;
Rectanglede.GetArea(x,y);
cout<<"Area="<<GetArea()<<endl;
}

不好意思我学习的很有限 不知道对不对,假如不对请原谅我
相似回答
大家正在搜