?? 4_4.cpp
字號:
//4_4.cpp
#include <iostream>
#include <cmath>
using namespace std;
class Point //Point類聲明
{
public:
Point(int xx=0, int yy=0) {X=xx;Y=yy;}
Point(Point &p);
int GetX() {return X;}
int GetY() {return Y;}
private:
int X,Y;
};
Point::Point(Point &p) //拷貝構造函數的實現
{
X=p.X;
Y=p.Y;
cout<<"Point拷貝構造函數被調用"<<endl;
}
//類的組合
class Line //Line類的聲明
{
public: //外部接口
Line (Point xp1, Point xp2);
Line (Line &);
double GetLen(){return len;}
private: //私有數據成員
Point p1,p2; //Point類的對象p1,p2
double len;
};
//組合類的構造函數
Line:: Line (Point xp1, Point xp2)
:p1(xp1),p2(xp2)
{
cout<<"Line構造函數被調用"<<endl;
double x=double(p1.GetX()-p2.GetX());
double y=double(p1.GetY()-p2.GetY());
len=sqrt(x*x+y*y);
}
//組合類的拷貝構造函數
Line:: Line (Line &Seg): p1(Seg.p1), p2(Seg.p2)
{
cout<<"Line拷貝構造函數被調用"<<endl;
len=Seg.len;
}
//主函數
int main()
{
Point myp1(1,1),myp2(4,5); //建立Point類的對象
Line line(myp1,myp2); //建立Line類的對象
Line line2(line); //利用拷貝構造函數建立一個新對象
cout<<"The length of the line is:";
cout<<line.GetLen()<<endl;
cout<<"The length of the line2 is:";
cout<<line2.GetLen()<<endl;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -