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; }