⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demo_copy_constructor_3.cpp

📁 对于一个初涉VC++的人来书
💻 CPP
字号:

//***************************************************

# include <iostream.h>
# include <string.h>

class Student
{
public:
	Student(char* pName="no name") //构造函数
	{
		cout<<"Constructing new student "<<pName<<endl;

		strcpy(name,pName); 
	}

	Student(Student& s) //拷贝构造函数
	{
		cout<<"Constructing copy of "<<s.name<<endl;

		strcpy(name,"copy of ");
		strcat(name,s.name);
	}

	~Student() //析构函数
	{
		cout<<"Destructing "<<name<<endl;
	}

protected:
	char name[40];
};

class Tutor
{
public:
	Tutor(Student& s):student(s) //拷贝构造函数
	{
		cout<<"Constructing tutor"<<endl;
	}

protected:
	Student student;
};

void fn(Tutor tutor)
//void fn(Tutor& tutor)
{
	cout<<"    In function fn()"<<endl;
}

void main()
{
	Student jenny("Jenny");
	Tutor tutor(jenny);

	cout<<"    Calling fn()"<<endl;

	fn(tutor);

	cout<<"    Returned from fn()"<<endl;
}

/***1
Constructing new student Jenny
Constructing copy of Jenny
Constructing tutor
    Calling fn()
Constructing copy of copy of Jenny
    In function fn()
Destructing copy of copy of Jenny
    Returned from fn()
Destructing copy of Jenny
Destructing Jenny
/***2
Constructing new student Jenny
Constructing copy of Jenny
Constructing tutor
    Calling fn()
    In function fn()
    Returned from fn()
Destructing copy of Jenny
Destructing Jenny
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -