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

📄 e8.cpp

📁 一、教学目的: 能理解C++中运算符重载的需要性
💻 CPP
字号:
#include<string.h>
#include<iostream.h>          

class Name
{
public:
	Name()//默认构造函数
	{
		pName=0;
	}
	Name(char*pn)//构造函数
	{
		copyName(pn);
	}
	Name(Name& s)//拷贝构造函数
	{
		copyName(s.pName);
	}
	~Name()
	{
		deleteName();
	}
	Name&operator =(Name& s)//赋值运算符
	{
		deleteName();
		copyName(s.pName);
		return *this;
	}
	void display()
	{
		cout<<pName<<endl;
	}
protected:
	void copyName(char*pN);
	void deleteName();
	char*pName;
};
void Name::copyName(char*pN)
{
	pName=new char[strlen(pN)+1];
	if(pName)
	{
		strcpy(pName,pN);
	}
}
void Name::deleteName()
{
	if(pName)
	{
		delete pName;
		pName=0;
	}
}
void main()
{
	Name s("claudette");
	Name t("temporary");
	t.display();
	t=s;
	t.display();
}

⌨️ 快捷键说明

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