C++ 实验六

//两个类;
#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;
晚上吃什么比较好呢?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值