[cpp] #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<ctime>
#include<fstream>
#include<climits>
using namespace std;
class Point
{
public:
Point(double x, double y){};
Point(Point &p);
private:
double x, y;
};
Point::Point(Point &p):x(p.x), y(p.y)
{
cout<<"calling copy gouzao of point"<<endl;
}
class Line
{
public:
Line(Point p1, Point p2);
Line(Line &l);
private:
Point p1, p2;
};
//Point有形參,所以只能用初始化列表初始化
//需要用初始化列表初始化:1.內嵌對象有參數表 2.常成員,需要綁定一個對象
Line::Line(Point mp1, Point mp2):p1(mp1), p2(mp2)
{
cout<<"calling gouzao of line"<<endl;
}
Line::Line(Line &l):p1(l.p1), p2(l.p2)
{
cout<<"calling copy gouzao of line"<<endl;
}
int main()
{
Point myp1(3, 5), myp2(3, 6);
Line myl(myp1, myp2);
Line myl2(myl);
return 0;
}
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<ctime>
#include<fstream>
#include<climits>
using namespace std;
class Point
{
public:
Point(double x, double y){};
Point(Point &p);
private:
double x, y;
};
Point::Point(Point &p):x(p.x), y(p.y)
{
cout<<"calling copy gouzao of point"<<endl;
}
class Line
{
public:
Line(Point p1, Point p2);
Line(Line &l);
private:
Point p1, p2;
};
//Point有形參,所以只能用初始化列表初始化
//需要用初始化列表初始化:1.內嵌對象有參數表 2.常成員,需要綁定一個對象
Line::Line(Point mp1, Point mp2):p1(mp1), p2(mp2)
{
cout<<"calling gouzao of line"<<endl;
}
Line::Line(Line &l):p1(l.p1), p2(l.p2)
{
cout<<"calling copy gouzao of line"<<endl;
}
int main()
{
Point myp1(3, 5), myp2(3, 6);
Line myl(myp1, myp2);
Line myl2(myl);
return 0;
}
運行結果如下:
摘自 飄過的小牛