demo_copy_constructor_3.cpp
来自「对于一个初涉VC++的人来书」· C++ 代码 · 共 84 行
CPP
84 行
//***************************************************
# 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 + =
减小字号Ctrl + -
显示快捷键?