本文共 1523 字,大约阅读时间需要 5 分钟。
说明:
之后的笔记都是这个模式,就不再赘述。在demo.cpp中有代码说明的注释。
1. demo.cpp:-----------------------------------#include#include #include "Line.h"using namespace std;/******************************//* 构造函数和析构函数 熟悉class类创建和销毁的过程,明白其原理*//******************************/int main(void){ Line *p = new Line(1,2,3,4); p->info(); delete p; p = NULL; system("pause"); return 0;}
2. Coordinate.h:-----------------------------------class Coordinate{public: Coordinate(int x, int y); ~Coordinate(); void setX(int x); int getX() const; //在Line中定义的const Coordinate m_coorA; void setY(int y); int getY() const;private: int m_iX; int m_iY;};
3. Coordinate.cpp:-----------------------------------#include#include "Coordinate.h"using namespace std;Coordinate::Coordinate(int x, int y){ m_iX = x; m_iY = y; cout<<"Coordinate() "< <<","< <
4. Line.h:-----------------------------------#include "Coordinate.h"class Line{public: Line(int x1, int y1, int x2, int y2); ~Line(); void setA(int x, int y); void setB(int x, int y); void info();public: const Coordinate m_coorA; //常对象 数据成员 Coordinate m_coorB; // 普通对象 数据成员};
5. Line.cpp:-----------------------------------#include "Line.h"#includeusing namespace std;Line::Line(int x1, int y1, int x2, int y2):m_coorA(x1,y1),m_coorB(x2,y2){ cout<<"Line()"<
F5后: