复制构造函数实例
[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;
}
运行结果如下:
摘自 飘过的小牛
补充:软件开发 , C++ ,