ch18_7.cpp
资源名称:c.rar [点击查看]
上传用户:puke2000
上传日期:2022-07-25
资源大小:912k
文件大小:1k
源码类别:
C#编程
开发平台:
Visual C++
- //**********************
- //** ch18_7.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();
- }