\
class Point
{
public:
Point(int x=0,int y=0 );
Point(Point&);
virtual ~Point();
int x,y;
};
// Point.cpp: implementation of the Point class.
//
//////////////////////////////////////////////////////////////////////
#include "Point.h"
#include <stdio.h>
#include <iostream>
using namespace std;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Point::Point(int x,int y)
{
this->x = x;
this->y = y;
cout<<"构造函数被调用"<<endl;
}
Point::Point(Point& p)
{
this->x = p.x;
this->y = p.y;
cout<<"拷贝构造函数被调用"<<endl;
}
Point::~Point()
{
}
class Test
{
public:
Test(int,char*,int,Point);
Test(Test&);
virtual ~Test();
void setPoint(Point);
private:
Point p1;
const int a;
int id;
char* name;
static int count;
};
Test::Test(int id,char* name,int a,Point p):a(a) //
{
//到这里,会调用Point的构造函数,p1会先声明
this->p1 = p;
this->id = id;
this->name = new char[strlen(name)+1]; //Ï
if(this->name!=0)
strcpy(this->name,name);
}
Test::Test(Test& t):a(t.a)
//到这里,会调用Point的构造函数,p1会先声明
p1 = t.p1;
id = t.id;
name = new char[strlen(t.name)+1];
if(name!=0)
strcpy(name,t.name);
cout<<"test"<<endl;
}
Test::~Test()
{
}
int Test::count = 10;
void Test::setPoint(Point p)
{
//在这里拷贝构造函数被调用,因为p1已经被声明过了
this->p1 = p;
}
void main()
{
cout<<"*********************"<<endl;
Point pp(1,1); //构造函数被调用
Point pp2; //构造函数被调用,而且必须有默认构造函数,可以使用无参数初始化,Point(int x=0,int y=0)或者Point()
cout<<"8888888"<<endl;
pp2 = pp; //不再调用
Point pp3 = pp; //拷贝函数被调用
}