- 0
- 0
- 约8.47千字
- 约 25页
- 2021-11-30 发布于安徽
- 举报
Deep Copy -- Customized Copy Constructor Explicitly define copy constructor Allocate new memory for pointers to point * Person::Person(Person person) { id = person.id; Date *p = person.getBirthDate(); birthDate = new Date(*p); } Run CustomCopyConstructor Person1 .h Person1.cpp §6.5 Destructors Invoked to destroy an object The opposite of constructor Like “new” vs. “delete” Named as “~ClassName” * Run TestCircle6.cpp Circle6.h Circle6.cpp ~Circle() { numberOfObjects--; } Notes No return value, no parameters So, no overloading A default destructor is available if No destructor is explicitly defined A destructor is useful if some resource, e.g. memory is allocated dynamically by an object * Person::Person(int id, int year, int month, int day) { this - id = id; birthDate = new Date(year, month, day); } Person::~Person() { delete birthDate; } Summary Constructors to initialize object Initialize object and allocate memory Special characteristics Overloading Copy constructor Shallow copy vs. deep copy Const object for non-modify Destructor to destroy objects * Review Questions Q6.1 Q6.9 * Chapter 6 Constructors and Destructors 构造函数与析构函数 §6.1 Basic Constructors §6.2 Dynamic Initialization of Objects §6.3 Dynamic Constructors §6.4 Copy Constructors §6.5 Destructors * §6.1 Simple Constructors A special member function to construct objects Plays the role of initializing objects Exactly the same name as the class Executed automatically by the system, NOT YOU! class integer{ private: int m,n; public: integer( ); }; integer:: integer( ){ m=0; n=0; } int main(){ integer i; } Constructor is Special Should be declared in pubic section No return type, not even “void” Make ‘implicit calls’ to the operators new and delete when memory allocation is required. Can’t refer their addresses An object with a constructor (or destructor) can’t be used as a member of a union * * Default Constructor A special constructor No paramet
您可能关注的文档
最近下载
- 常微分方程(第四版)课件 王高雄 高等教育出版社 第一章 绪论.pptx VIP
- 第四单元-第二章-第一节 食物中的营养物质课件-2024-2025学年七年级生物下学期人教版202.pptx
- 新技术、新工艺、新材料及新理念在市政工程中的应用.ppt VIP
- 《四川麻将竞赛规则》(TTFMJ 01-2024).pdf VIP
- 大学语文 课件全套 吕澜希 第1--7章 先秦文学---外国文学.pptx
- 盈建科教学知识培训课件.pptx VIP
- 常微分方程(第四版)课件 王高雄 高等教育出版社 第二章 一阶微分方程的初等解法(一).pptx VIP
- 现代产品设计与开发第7章 CMF设计的材料与工艺.pptx VIP
- 【教案】八年级 第16课 模块功能先划分.docx VIP
- 离散数学(微课版)(第2版).pptx
原创力文档

文档评论(0)