博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++中class类 的 构造函数、析构函数
阅读量:4049 次
发布时间:2019-05-25

本文共 1523 字,大约阅读时间需要 5 分钟。

说明

  • demo.cpp:main.cpp所在之处
  • Line.h:线段类的.h文件
  • Line.cpp:线段类的.cpp文件
  • Coordinate.h:坐标类的.h文件
  • Coordinate.cpp:坐标类的.cpp文件

之后的笔记都是这个模式,就不再赘述。在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"#include 
using namespace std;Line::Line(int x1, int y1, int x2, int y2):m_coorA(x1,y1),m_coorB(x2,y2){ cout<<"Line()"<

F5后:

这里写图片描述

你可能感兴趣的文章
Idea下安装Lombok插件
查看>>
zookeeper
查看>>
Idea导入的工程看不到src等代码
查看>>
技术栈
查看>>
Jenkins中shell-script执行报错sh: line 2: npm: command not found
查看>>
8.X版本的node打包时,gulp命令报错 require.extensions.hasownproperty
查看>>
Jenkins 启动命令
查看>>
Maven项目版本继承 – 我必须指定父版本?
查看>>
通过C++反射实现C++与任意脚本(lua、js等)的交互(二)
查看>>
利用清华镜像站解决pip超时问题
查看>>
微信小程序开发全线记录
查看>>
CCF 分蛋糕
查看>>
解决python2.7中UnicodeEncodeError
查看>>
小谈python 输出
查看>>
Django objects.all()、objects.get()与objects.filter()之间的区别介绍
查看>>
python:如何将excel文件转化成CSV格式
查看>>
机器学习实战之决策树(一)
查看>>
机器学习实战之决策树二
查看>>
[LeetCode By Python]7 Reverse Integer
查看>>
[LeetCode By Python]121. Best Time to Buy and Sell Stock
查看>>