//两个类;
#include <iostream>
class CPoint
{
public:
CPoint(void){xLoc = 0; yLoc = 0;}
CPoint(double xLoc_Init, double yLoc_Init)
{
xLoc = xLoc_Init;
yLoc = yLoc_Init;
};
~CPoint(){};
double GetX(void) const{return xLoc;}
double GetY(void) const{return yLoc;}
void SetX(double xVal){xLoc = xVal;}
void SetY(double yVal){yLoc = yVal;}
private:
double xLoc, yLoc;
};
class CRectangle
{
public:
CRectangle(const CPoint &, const CPoint &);
~CRectangle(){};
void SetLPoint(const CPoint &);
void SetRPoint(const CPoint &);
double GetPerimeter(void);
double GetArea(void);
private:
CPoint BotLeftCorner, TopRightCorner;
};
int main(void)
{
using namespace std;
CPoint blc(2, 5);
CPoint trc(6, 8);
CRectangle * a_rectangle = new CRectangle(blc, trc);
double perimeter = 0, area = 0;
perimeter = a_rectangle -> GetPerimeter();
area = a_rectangle -> GetArea();
cout << "the first rectangle's perimeter is: " << perimeter << endl;
cout << "the first rectangle's area is: " << area << endl;
blc.SetX(4);
blc.SetY(6);
trc.SetX(7);
trc.SetY(9);
a_rectangle -> SetLPoint(blc);
a_rectangle -> SetRPoint(trc);
perimeter = a_rectangle -> GetPerimeter();
area = a_rectangle -> GetArea();
cout << "the second rectangle's perimeter is: " << perimeter << endl;
cout << "the second rectangle's area is: " << area << endl;
delete a_rectangle;
return 0;
}
CRectangle::CRectangle(const CPoint &blcSet, const CPoint &trcSet)
{
BotLeftCorner.SetX(blcSet.GetX());
BotLeftCorner.SetY(blcSet.GetY());
TopRightCorner.SetX(trcSet.GetX());
TopRightCorner.SetY(trcSet.GetY());
}
void CRectangle::SetLPoint(const CPoint &blcSet)
{
BotLeftCorner.SetX(blcSet.GetX());
BotLeftCorner.SetY(blcSet.GetY());
}
void CRectangle::SetRPoint(const CPoint &trcSet)
{
TopRightCorner.SetX(trcSet.GetX());
TopRightCorner.SetY(trcSet.GetY());
}
double CRectangle::GetPerimeter(void)
{
return 2 * ((TopRightCorner.GetX() - BotLeftCorner.GetX()) + (TopRightCorner.GetY() - BotLeftCorner.GetY()));
}
double CRectangle::GetArea(void)
{
return (TopRightCorner.GetX() - BotLeftCorner.GetX()) * (TopRightCorner.GetY() - BotLeftCorner.GetY());
}
使用声明为public类型的函数访问声明为private类型的数据成员.
对象的常引用作为函数参数, 要求其使用的方法必须限定为const类型.
/*声明形式如右*/ double GetX(void) const;
晚上吃什么比较好呢?