重载 =(深拷贝)
#includeusing namespace std; class CNum { public: int *pNu; public: CNum() { pNu=new int(100); } ~CNum() { if(pNu!=NULL) { delete pNu; pNu=NULL; } } public: CNum &operator =(CNum &num) { if(this->pNu) {delete this->pNu; this->pNu=NULL; } this->pNu=new int; *this->pNu=*num.pNu; return *this; } }; int main() { CNum pnu1; cout<<*(pnu1.pNu)< 单例模式
#includeusing namespace std; class CObject { private: static CObject *pob; private: CObject(){}; CObject(CObject &ob); public: static CObject* getobject(); }; CObject * CObject::pob=NULL; CObject* CObject::getobject() { if(pob==NULL) { pob=new CObject; } return pob; } int main() { CObject* p1=CObject::getobject(); CObject* p2=CObject::getobject(); CObject* p3=CObject::getobject(); system("pause"); return 0; } 模板模式
#includeusing namespace std; class person { public: virtual void type()=0; void eat() { cout<<"来一碗饭"< 文件
头文件
#includeusing namespace std; #pragma once //只编译一次 //#ifndef _STUDENT_H //#define _STUDENT_H class student { public: string name; int age; public: student(); public: void show(); }; //#endif 源文件
#include#include //系统头文件<> #include"student.h"//自定义头文件 "" #include"student.h" using namespace std; student::student() { name="tom"; age=12; } void student::show() { cout<



