已阅读5页,还剩23页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C+ Mini-Course Part 1: Mechanics Part 2: Basics CPSC 221, Texas A class Segment public: Segment(); virtual Segment(); private: Point *m_p0, *m_p1; ; #endif / _SEGMENT_HEADER_ Segment.h #include “Segment.h” #include “Point.h” Segment:Segment() m_p0 = new Point(0, 0); m_p1 = new Point(1, 1); Segment:Segment() delete m_p0; delete m_p1; Segment.cpp #include “Segment.h” #include #include Insert header file at this point. Use library header. Header Guards #ifndef _SEGMENT_HEADER_ #define _SEGMENT_HEADER_ / contents of Segment.h /. #endif To ensure it is safe to include a file more than once. Header Guards #ifndef _SEGMENT_HEADER_ #define _SEGMENT_HEADER_ / contents of segment.H /. #endif To ensure it is safe to include a file more than once. If this variable is not defined Define it. End of guarded area. Circular Includes Whats wrong with this picture? How do we fix it? #include “controller.h” / define gui / . gui.h #include “gui.h” class Controller /. private: Gui* myGui; /. ; controller.h Forward Declarations In header files, only include what you must. If only pointers to a class are used, use forward declarations. /Forward Declaration class Controller; / define gui / . gui.h /Forward declaration class Gui; class Controller /. private: Gui* myGui; /. ; controller.h Compilation Preprocessor Inlines #includes etc. Compiler Translates to machine code Associates calls with functions Linker Associates functions with definitions Object files Executable External Libraries, libc.so, libcs123.so OK, OK. How do I run my Program? make And if all goes well ./myprog C+ Mini-Course Part 2: Basics What is a pointer? int x = 10; int *p; p = p gets the address of x in memory. p x10 What is a pointer? int x = 10; int *p; p = *p = 20; *p is the value at the address p. p x20 What is a pointer? int x = 10; int *p = NULL; p = *p = 20; Declares a pointer to an integer new can be thought of a function with slightly strange syntax new allocates space to hold the object. new calls the objects constructor. new returns a pointer to that object. Deallocating memory using delete / allocate memory Point *p = new Point(5, 5); . / free the memory delete p; For every call to new, there must be exactly one call to delete. Using new with arrays int x = 10; int* nums1 = new int10; / ok int* nums2 = new intx; / ok Initializes an array of 10 integers on the heap. C+ equivalent of the following C code int* nums = (int*)malloc(x * sizeof(int); Using new with multidimensional arrays int x = 3, y = 4; int* nums3 = new intx4;/ ok int* nums4 = new intxy;/ BAD! Initializes a multidimensional array Only the first dimension can be a variable. The rest must be constants. Use single dimension arrays to fake multidimensional ones Using delete on arrays / allocate memory int* nums1 = new int10; int* nums3 = new intx45; . / free the memory delete nums1; delete nums3; Have to use delete. Destructors delete calls the objects destructor. delete frees space occupied by the object. A destructor cleans up after the object. Releases resources such as memory. Destructors an Example class Segment public: Segment(); virtual Segment(); private: Point *m_p0, *m_p1; ; Destructors an Example Segment:Segment() m_p0 = new Point(0, 0); m_p1 = new Point(1, 1); Segment:Segment() if (m_p0) delete m_p0; if (m_p1) delete m_p1; New vs Malloc MallocNew Standard C FunctionOperator (like =, +=, etc.) Used sparingly in C+; used frequently in C Only in C+ Used for allocating chunks of memory of a given size without respect to what will be stored in that memory Used to allocate instances of classes / structs / arrays and will invoke an objects constructor Returns void* and requires explicit casting Returns the proper type Returns NULL when there is not enough memory Throws an exception when there is not enough memory Every malloc() should be matched with a free() Every new/new should be matched with a delete/delete Never mix new/delete with malloc/free Classes vs Structs Default access specifier for classes is private; for structs it is public Except for this difference, structs are functionally the same as classes, but the two are typically used differently: structs should be thought of as lightweight classes that contain mostly data and possibly convenience methods to manipulate that data and are hardly ever used polymorphically struct Point int x; int y; / convenience constructor Point(int a, int b) : x(a), y(b) / returns distance to another point double distance(const Point int dy = m_y pnt.y; return math.sqrt(dx*dx + dy*dy); ; class Segment public: Segment(); virtual Segment(); void setPoints(int x0, int y0, int x1, int y1); protected: Point *m_p0, *m_p1; ; void Segment:setPoints(int x0, int y0, int x1, int y1) m_p0 = new Point(x0, y0); m_p1 = new Point(x1, y1); Syntactic Sugar “-” Point *p = new Point(5, 5); / Access a member function: (*p).move(10, 10); / Or more simply: p-move(10, 10); Stack vs. Heap On the Heap / Dynamic allocation On the Stack / Automatic allocation drawStuff() Point *p = new Point(); p-move(10,10); /. drawStuff() Point p(); p.move(5,5); /. What happens when p goes out of scope? Summary with Header File begin header guard #ifndef _SEGMENT_HEADER_ #define
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 经济常识自测题库及答案全收录
- 健康饮食知识测试题及答案概览
- 2025护理核心制度考试试题及参考答案
- 建筑安全法规与实操指南考试试题及解答
- 2025年肠道传染病防治培训试题及答案
- 建筑技术与应用能力测试试题集
- 化工工程伦理实践题及参考答案版
- 2025年财务专员年底工作总结及2026年度工作计划
- 北师大版五年级英语上册第六单元模拟卷
- 2025年公司车辆使用合同协议
- 私密健康沙龙课件下载
- 儿童粘土手工课件
- 《渗透型液体硬化剂应用技术规程》
- 2025年儿科主治考试《相关专业知识》真题卷(含每题答案)
- 2025生产安全考试题库及答案
- 2025小学道德与法治教师课标考试模拟试卷附参考答案 (三套)
- 绿化工程监理例会会议纪要范文
- 儿童寓言故事-乌鸦喝水
- 《ATM自动取款机》课件
- 2025年度建行房贷合同范本:商业贷款合同模板
- 集装箱式活动板房物流配送方案
评论
0/150
提交评论