用C++设计一个矩形类

要求:用到构造函数 析构函数
用到指针调用等调用方法
主函数里只能出现简单的定义和调用

参考一下CRect的定义
class CRect : public tagRECT
{
public:

// Constructors

// uninitialized rectangle
CRect();
// from left, top, right, and bottom
CRect(int l, int t, int r, int b);
// copy constructor
CRect(const RECT& srcRect);
// from a pointer to another rect
CRect(LPCRECT lpSrcRect);
// from a point and size
CRect(POINT point, SIZE size);
// from two points
CRect(POINT topLeft, POINT bottomRight);

// Attributes (in addition to RECT members)

// retrieves the width
int Width() const;
// returns the height
int Height() const;
// returns the size
CSize Size() const;
// reference to the top-left point
CPoint& TopLeft();
// reference to the bottom-right point
CPoint& BottomRight();
// const reference to the top-left point
const CPoint& TopLeft() const;
// const reference to the bottom-right point
const CPoint& BottomRight() const;
// the geometric center point of the rectangle
CPoint CenterPoint() const;
// swap the left and right
void SwapLeftRight();
static void SwapLeftRight(LPRECT lpRect);

// convert between CRect and LPRECT/LPCRECT (no need for &)
operator LPRECT();
operator LPCRECT() const;

// returns TRUE if rectangle has no area
BOOL IsRectEmpty() const;
// returns TRUE if rectangle is at (0,0) and has no area
BOOL IsRectNull() const;
// returns TRUE if point is within rectangle
BOOL PtInRect(POINT point) const;

// Operations

// set rectangle from left, top, right, and bottom
void SetRect(int x1, int y1, int x2, int y2);
void SetRect(POINT topLeft, POINT bottomRight);
// empty the rectangle
void SetRectEmpty();
// copy from another rectangle
void CopyRect(LPCRECT lpSrcRect);
// TRUE if exactly the same as another rectangle
BOOL EqualRect(LPCRECT lpRect) const;

// inflate rectangle's width and height without
// moving its top or left
void InflateRect(int x, int y);
void InflateRect(SIZE size);
void InflateRect(LPCRECT lpRect);
void InflateRect(int l, int t, int r, int b);
// deflate the rectangle's width and height without
// moving its top or left
void DeflateRect(int x, int y);
void DeflateRect(SIZE size);
void DeflateRect(LPCRECT lpRect);
void DeflateRect(int l, int t, int r, int b);

// translate the rectangle by moving its top and left
void OffsetRect(int x, int y);
void OffsetRect(SIZE size);
void OffsetRect(POINT point);
void NormalizeRect();

// set this rectangle to intersection of two others
BOOL IntersectRect(LPCRECT lpRect1, LPCRECT lpRect2);

// set this rectangle to bounding union of two others
BOOL UnionRect(LPCRECT lpRect1, LPCRECT lpRect2);

// set this rectangle to minimum of two others
BOOL SubtractRect(LPCRECT lpRectSrc1, LPCRECT lpRectSrc2);

// Additional Operations
void operator=(const RECT& srcRect);
BOOL operator==(const RECT& rect) const;
BOOL operator!=(const RECT& rect) const;
void operator+=(POINT point);
void operator+=(SIZE size);
void operator+=(LPCRECT lpRect);
void operator-=(POINT point);
void operator-=(SIZE size);
void operator-=(LPCRECT lpRect);
void operator&=(const RECT& rect);
void operator|=(const RECT& rect);

// Operators returning CRect values
CRect operator+(POINT point) const;
CRect operator-(POINT point) const;
CRect operator+(LPCRECT lpRect) const;
CRect operator+(SIZE size) const;
CRect operator-(SIZE size) const;
CRect operator-(LPCRECT lpRect) const;
CRect operator&(const RECT& rect2) const;
CRect operator|(const RECT& rect2) const;
CRect MulDiv(int nMultiplier, int nDivisor) const;
};
温馨提示:内容为网友见解,仅供参考
第1个回答  2008-11-07
好题目,可扩展性很强,如果是老师给的题目那可真是个好老师.
不过析构函数不一定会用到

刚才随便写了点

.h

class CMYRC
{
public:
CMYRC( int left, int right, int top, int bottom, int size ); // 运动范围

void UserInit( int x, int y, int size ); // 初始化矩形
void SetMoveOrStop(); // 运动或静止
void MoveTo( int x, int y ); // 指定移动目的点
void GetCurPos( int &x, int &y ); // 取得矩形当前的坐标

private:
// 创建者设定
int m_left;
int m_right;
int m_top;
int m_bottom;
int m_size;

// 矩形中心坐标
int m_x;
int m_y;

// 运动
int m_x_to;
int m_y_to;

DWORD m_tick_lastmove;
DWORD m_speed_move;

// 其他属性
DWORD m_color;
DWORD m_tick_color; // 还可以加入颜色渐变
};

.cpp

CMYRC::CMYRC( int left, int right, int top, int bottom, int size )
{
m_left = left;
m_right = right;
m_top = top;
m_bottom = bottom;
m_size = size;

UserInit( rand(), rand(), size ); // 两个rand应该有个生成函数来保证在left right top bottom内
m_tick_lastmove = GetTickCount();
}

void CMYRC::UserInit( int x, int y, int size )
{
m_x = x;
m_y = y;
m_size = m_size;
}

void CMYRC::SetMoveOrStop()
{
if ( m_tick_lastmove == 0 )
{
m_tick_lastmove = GetTickCount();
// 需要检查是否有移动目的点 m_x_to m_y_to
}
else
{
DWORD tick = GetTickCount() - m_tick_lastmove;

int xlen = m_x_to - m_x;
int ylen = m_y_to - m_y;
int linelen = (int)sqrt( (float)(xlen*xlen + ylen*ylen) ); // 上次开始移动时距目的点总距离

int linepass = tick * m_speed_move; // 间隔时间内移动的距离

if ( linepass >= linelen )
{
m_x = m_x_to;
m_y = m_y_to;
}
else
{
// 计算xy的偏移得到当前时刻xy的值
}
m_tick_lastmove = 0; // 停止计时将 m_tick_lastmove 用做标志
}
}

void CMYRC::MoveTo(int x, int y)
{
if ( m_tick_lastmove != 0 )
{
SetMoveOrStop();
}

m_x = x;
m_y = y;

if ( m_tick_lastmove == 0 )
{
SetMoveOrStop();
}
}

void CMYRC::GetCurPos( int &x, int &y )
{
if ( m_tick_lastmove != 0 )
{
SetMoveOrStop();
}

x = m_x;
y = m_y;

if ( m_tick_lastmove == 0 )
{
SetMoveOrStop();
}
}

有点乱,突然想自己写个游戏玩了...
第2个回答  2008-11-07
//main.cpp
#include <iostream>
using namespace std;
#include "Rectangle.h"
int main(void);

int main(void)
{
double li;
double wi;

cout<<"Input the width :"<<endl;
cin>>wi;
cout<<"Input the length :"<<endl;
cin>>li;
Rectangle MC(li,wi);
MC.print();

return 0;
}

//Rectangle.cpp
#include <iostream>
using namespace std;
#include "Rectangle.h"

Rectangle::Rectangle(double l,double w)
{
length=l;
width=w;
area=length*width;
}

void Rectangle::print()
{
cout<<"The area of the rectangle is "<<area<<endl;
}

//Rectangle.h
class Rectangle
{
double length;
double width;
double area;
public:
Rectangle(double l,double w);
void print();
};

这个用不到析构函数本回答被网友采纳

C++定义并实现一个矩形类
cout<<"该矩形的面积为:"<<((x2-x1)*(y2-y1))<<endl;} private:double x1,x2,y1,y2;};

C++问题:设计一个名为Rectangle的矩形类,其属性为矩形的左下角和右...
{ cout<<"长方形的左下角坐标是:"<<left<<","<<bottom<<endl;} void Getp2(int right,int top)

C++设计一个矩形类,派生出长方形正方形,计算周长和面积
zhengfangxing b(13.0);\/\/构造正方形 cout<<"长方形面积:"<<a.getS()<<"长方形周长"<<a.getC()<<endl;cout<<"正方形面积:"<<b.getS()<<"正方形周长"<<b.getC()<<endl;return ;}

采用c++定义并实现一个矩形(Rectangle)类
rect.SetWidth(100);rect.SetColor("红色");cout << "矩形:

C++程序设计 矩形Rectangle类,通过Rectangle类计算周长和面积。_百度...
public:Rectangle():m_width(1),m_length(1){ } Rectangle(int length,int width){ if (test(length,width)) { m_length = length;m_width = width;} else { m_length = 1;m_width = 1;} } ~Rectangle(){} void Setwidth(int a){ if (test(m_length,a)) { m_width = a;...

C++设计 定义一个矩形类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...

用C++语言“定义并实现一个矩形类,有长、宽两个属性,用成员函数计算矩形...
class rectangle\/\/定义矩形类 { public:rectangle(float longth,float weight)\/\/构造函数 { itslongth=longth;itsweight=weight;} float getArea(){return itslongth*itsweight;};\/\/计算矩形面积 private:float itslongth,itsweight;\/\/定义长宽属性 };int main(){ rectangle rec1(5,10);\/\/定义...

用c++编写一个矩形类
include <iostream>using namespace std;class Rectangle { private: double length; double height; double area; double circumference; public: Rectangle() {} Rectangle(double l, double h) { length = l; height=h; } Rectangle(const Rectangle &r) { leng...

定义一个描述矩形的类Rectangle,包括的数据成员有宽(width)和长(length...
C++编程如下 include <iostream.h> include <std.h> include <math.h> int main(){ int a1=5,a2=6;int b1=4,b2=5;int s1,s2;s1=a1*a2;s2=b1*b2;printf("矩形1的面积为:%d;",s1);printf("矩形2的面积为:%d;",s2);if(s1>s2){ printf("s1>s2");} else { if(s1<s2){ ...

c++如何设计一个矩形类(rect)?
{ public:rect(int width, int length) : width(width),length(length){} int Area(){ return length * width;} int Perimeter(){ return 2 * (length + width);} friend int add(rect &r1, rect &r2);private:int length;int width;};int add(rect &r1,rect &r2){ return r1....

相似回答