C上机实验六 下载本文

C++上机实验六

1、 设计描述平面坐标上的点CPoint类,该类满足下述要求: ?具有x,y坐标信息;

?具有带默认形参值的构造函数,参数分别用于初始化x和y坐标信息;

?具有获取x、y信息的GetX和GetY函数,具有设置x、y信息的SetX和SetY函数; 2、 设计一个矩形类CRectangle,该类满足下述要求:

?具有矩形的左下角和右上角两个点的坐标信息,这两个点的数据类型是CPoint;

?具有带参数的构造函数CRectangle(const CPoint &, const CPoint &),参数分别用于设置左下角和右上角两个点的坐标信息;

?具有设置左下角和设置右上角的两个点坐标的功能SetLPoint(const CPoint &)和SetRPoint(const CPoint &);

?具有获得周长(GetPerimeter)和获得面积(GetArea)的功能。 3、 在main函数中,完成以下工作:

?动态创建一个CRectangle类的对象a_rectagnle,其初始的左下角和右上角坐标分别为(2,5)、(6,8);调用GetPerimeter和GetArea获得矩形周长和面积,并将周长和面积显示在屏幕上; ?调用SetLPoint设置a_rectagnle的左下角为(4,6),调用SetRPoint设置a_rectagnle的右上角为(7,9);调用GetPerimeter和GetArea获得矩形周长和面积,并将周长和面积显示在屏幕上; ?销毁该动态创建的对象。 #include<iostream>

using namespace std;

class CPoint { public: CPoint() { x=0; y=0; }

void GetX() {

cout<<"x = "<<x<<endl; }

void GetY() {

cout<<"y = "<<y<<endl; }

void SetX() {

cout<<"Input x: "<<endl;

cin>>x; }

void SetY() {

cout<<"Input y: "<<endl;

cin>>y; } private: int x; int y; };

int main() { CPoint p; p.SetX(); p.SetY(); p.GetX(); p.GetY(); return 0; } (2)

#include<iostream>

using namespace std;

class CPoint { public: CPoint() { x=0; y=0; } int GetX()

{ return x; } int GetY() { return y; }

void SetX() {

cin>>x; }

void SetY() {

cin>>y; } private: int x,y; };

class CRectangle { public:

CRectangle(const CPoint &a, const CPoint &b) { L=a; R=b; }

void SetRPoint() {

cout<<"Input the RPoint"<<endl; R.SetX(); R.SetY(); }

void SetLPoint() {

cout<<"Input the LPoint"<<endl; L.SetX(); L.SetY(); }

int GetPerimeter() { int s;

s= 2*(R.GetX()-L.GetX()+R.GetY()-L.GetY());

cout<<"The Perimeter is "<<s<<endl;

return 0; }

int GetArea() { int t;

t= (R.GetX()-L.GetX())*(R.GetY()-L.GetY());

cout<<"The Area is "<<t<<endl; return 0; } private:

CPoint R,L; }; (3)

#include<iostream>

using namespace std;

class CPoint { public: CPoint() { x=0;

y=0; } int GetX() { return x; } int GetY() { return y; }

void SetX() {

cin>>x; }

void SetY() {

cin>>y; } private: int x,y;

};

class CRectangle { public:

CRectangle(const CPoint &a, const CPoint &b) { L=a; R=b; }

void SetRPoint() {

cout<<"Input the RPoint"<<endl; R.SetX(); R.SetY(); }

void SetLPoint() {

cout<<"Input the LPoint"<<endl; L.SetX(); L.SetY(); }

int GetPerimeter() { int s;

s= 2*(R.GetX()-L.GetX()+R.GetY()-L.GetY());

cout<<"The Perimeter is "<<s<<endl;

return 0; }

int GetArea() { int t;

t= (R.GetX()-L.GetX())*(R.GetY()-L.GetY());

cout<<"The Area is "<<t<<endl; return 0; } private:

CPoint R,L; }; int main() { CPoint L; CPoint R;

cout<<"Initialize the LPoint:"<<endl;

L.SetX(); L.SetY();

cout<<"Initialize the RPoint:"<<endl; R.SetX(); R.SetY();

CRectangle a_rectagnle(L,R);

a_rectagnle.GetPerimeter();

a_rectagnle.GetArea();

a_rectagnle.SetLPoint();

a_rectagnle.SetRPoint();

a_rectagnle.GetPerimeter();

a_rectagnle.GetArea(); return 0; }