C++设计 定义一个矩形类Rect 急求啊
定义一个矩形类Rect,数据成员为length、height,要求有构造函数、析构函数,其成员函数area求矩形面积,利用主函数进行测试...
定义一个矩形类Rect,数据成员为length、height,要求有构造函数、析构函数,其成员函数area求矩形面积,利用主函数进行测试
展开
1个回答
展开全部
[Rect.h]:
[code=cpp]
class Rect
{
public:
double height;
double width;
public:
Rect(void);
Rect(double height, double width);
~Rect(void);
void setHeight(double height);
void setWidth(double width);
double getHeight();
double getWidth();
double getArea();
};
[/code]
[Rect.cpp]:
[code=cpp]
#include "Rect.h"
Rect::Rect(void)
{
this->height = 0;
this->width = 0;
}
Rect::Rect(double height,double width){
this->setHeight(height);
this->setWidth(width);
}
Rect::~Rect(void)
{
}
void Rect::setHeight(double height)
{
if(0 < height)
{
this->height = height;
}
}
void Rect::setWidth(double width)
{
if(0 < width)
{
this->width = width;
}
}
double Rect::getHeight()
{
return this->height;
}
double Rect::getWidth()
{
return this->width;
}
double Rect::getArea()
{
return this->height * this->width;
}
[/code]
main.cpp :
[code=cpp]
#include <iostream>
#include "conio.h"
#include "Rect.h"
using namespace std;
int main(int argc, char* argv[])
{
Rect * rect = new Rect(10,20);
cout<<rect->getArea()<<endl;
getch();
return 0;
}
[/code]
[code=cpp]
class Rect
{
public:
double height;
double width;
public:
Rect(void);
Rect(double height, double width);
~Rect(void);
void setHeight(double height);
void setWidth(double width);
double getHeight();
double getWidth();
double getArea();
};
[/code]
[Rect.cpp]:
[code=cpp]
#include "Rect.h"
Rect::Rect(void)
{
this->height = 0;
this->width = 0;
}
Rect::Rect(double height,double width){
this->setHeight(height);
this->setWidth(width);
}
Rect::~Rect(void)
{
}
void Rect::setHeight(double height)
{
if(0 < height)
{
this->height = height;
}
}
void Rect::setWidth(double width)
{
if(0 < width)
{
this->width = width;
}
}
double Rect::getHeight()
{
return this->height;
}
double Rect::getWidth()
{
return this->width;
}
double Rect::getArea()
{
return this->height * this->width;
}
[/code]
main.cpp :
[code=cpp]
#include <iostream>
#include "conio.h"
#include "Rect.h"
using namespace std;
int main(int argc, char* argv[])
{
Rect * rect = new Rect(10,20);
cout<<rect->getArea()<<endl;
getch();
return 0;
}
[/code]
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询