所有代码如下所示,都是按照你的要求来的,记得给分啊,费了挺长时间的:
#include
#include
using namespace std;
class CRectangle
{
private:
double x,y,a,b;
public:
CRectangle() //无参数的构造函数
{
x=y=a=b=0;
}
CRectangle(double x1,double y1,double a1,double b1)//有参数的构造函数
{
cout<<"该长方形已经被创建"< x=x1; y=y1; a=a1; b=b1; } ~CRectangle() { cout<<"该长方形已被析构"< } void changePoint(double x2,double y2);//修改坐标函数 void changelength(double a2,double b2); //改长方形的边长 double length();//计算周长 double area(); //计算面积 friend double distance(CRectangle &CRe); //求重心距离原地的函数 friend ostream & operator<<(ostream &out,const CRectangle & CRe); //重载<< }; void CRectangle::changePoint(double x2,double y2) { x=x2; y=y2; } void CRectangle::changelength(double a2,double b2) { a=a2; b=b2; } double CRectangle::length() { return a+a+b+b; } double CRectangle::area() { return a*b; } double distance(CRectangle &CRe) { double x=CRe.x; double y=CRe.y; double a=CRe.a; double b=CRe.b; return sqrt((x+a/2)*(x+a/2)+(y+b/2)*(y+b/2)); } ostream & operator<<(ostream &out,const CRectangle &CRe) { out<<"该长方形左下角坐标是:("< out<<"该长方形长是"< ///////注意这里,如果使用CRe.length()和CRe.area()会出错,我也没搞懂为什么错误///// out<<"该长方形周长是: "< out<<"该长方形面积是: "< return out; } int main() { CRectangle c(1,1,4,3); cout< cout<<"距离原点距离是: "< c.changePoint(2,2); //左下角改为(2,2) c.changelength(8,6); //长改为8,宽改为6 cout< cout<<"距离原点距离是: "< return 0; } 运行截图如下: 记得给分,给分,给分,给分,给分,给分,分,分,分,分,分,分…………
10分写这么复杂代码?
现在程序员还真廉价了。
///////////////////////////////////////////////////////////////
// Rectangle.h
#pragma once
class CRectangle
{
public:
CRectangle(double x,double y,double a,double b);
~CRectangle();
double Getx() const{return _x;}
double Gety() const{return _y;}
double Geta() const{return _a;}
double Getb() const{return _b;}
void Setx(double x){_x=x;}
void Sety(double y){_y=y;}
void Seta(double a){_a=a;}
void Setb(double b){_b=b;}
double length() const{return (_a+_b)*2;}
double area() const{return _a*_b;}
friend double distance(const CRectangle &rc);
private:
double _x,_y,_a,_b;
};
//////////////////////////////////////////////////
// CRectangle.cpp
#include "stdafx.h"
#include "Rectangle.h"
CRectangle::CRectangle(double x,double y,double a,double b)
: _x(x)
, _y(y)
, _a(a)
, _b(b)
{
std::cout << "Rectangle created." << std::endl;
}
CRectangle::~CRectangle()
{
std::cout << "Rectangle destroyed." << std::endl;
}
///////////////////////////////////////////////////////
// main.cpp
#include "stdafx.h"
#include "Rectangle.h"
double distance(const CRectangle &rc)
{
double x1,y1;
x1=rc._x+a/2.0;
y1=rc._y+b/2.0;
return sqrt(x1*x1+y1*y1);
}
ostream& operator << (ostream& out, CRectangle &rc)
{
std::cout << "x:" << rc.Getx() << "\ny:" << rc.Gety()
<< "\na:" << rc.Geta() << "\nb:" << rc.Getb();
}
//主函数略
/////////////////////////////////////////////
// stdafx.h
#pragma once
#include
#include
///////////////////////////////////////////////////
// stdafx.cpp
#include "stdafx.h"
//程序完成