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

📄 6_9.cpp

📁 清华大学C++补充习题代码
💻 CPP
字号:
//6_9.cpp
#include<iostream>
#include<cstring>
using namespace std;
class Student
{
public:
	Student();
	~Student();
	char* GetName() { return Name; }
	void SetName(char* nam) {strcpy(Name,nam);}
private:
	char* Name;
};

Student::Student()
{
	cout<<"Constructor is called!"<<endl;
	Name=new char[20];
	strcpy(Name,"zzz");
}
Student::~Student()
{
	delete[] Name;
	cout<<"Destructor is called!"<<endl;
}

int main()
{
	Student a;
	Student b(a);
	cout<<"a's Name:"<<a.GetName()<<endl;
	cout<<"b's Name:"<<b.GetName()<<endl;
	a.SetName("sss");
	cout<<"a's Name:"<<a.GetName()<<endl;
	cout<<"b's Name:"<<b.GetName()<<endl;
	return 0;
}
/*
程序运行结果为:
Constructor is called!
a's Name:zzz
b's Name:zzz
a's Name:sss
b's Name:sss
Destructor is called! 
出错
*/

⌨️ 快捷键说明

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