c++ 封装 (下)
来源:互联网 发布:好玩的钢琴软件 编辑:程序博客网 时间:2024/06/15 05:36
对象数组的内存分布:
对象成员:
#pragma onceclass MyPoint{public:MyPoint(void);MyPoint(double x, double y);~MyPoint(void);double getX();double getY();void setX(double x);void setY(double y);void print();private:double m_dX;double m_dY;};
#include "MyPoint.h"#include <iostream>using namespace std;MyPoint::MyPoint(double x, double y):m_dX(x), m_dY(y){print();cout << "MyPoint(x, y)" << endl;}MyPoint::MyPoint(void){print();cout << "MyPoint()" << endl;}MyPoint::~MyPoint(void){print();cout << "~MyPoint()" << endl;}void MyPoint::print(){cout << "(" << m_dX << "," << m_dY << ")" << endl;}double MyPoint::getX(){return m_dX;}double MyPoint::getY(){return m_dY;}void MyPoint::setX(double x){this->m_dX = x;}void MyPoint::setY(double y){this->m_dY = y;}
#pragma once#include "MyPoint.h"class Line{public:Line(void);Line(double begin_x, double begin_y, double end_x, double end_y);~Line(void);MyPoint getBegin();MyPoint getEnd();void setBegin(MyPoint& begin);void setEnd(MyPoint& end);void setBegin(double x, double y);void setEnd(double x, double y);void print();private:MyPoint m_begin;//<span style="font-family: Arial, Helvetica, sans-serif;">对象成员</span>MyPoint m_end;//对象成员};
#include "Line.h"#include <iostream>using namespace std;Line::Line(double begin_x, double begin_y, double end_x, double end_y):m_begin(begin_x, begin_y), m_end(end_x, end_y){cout << "Line(bx, by, ex, ey)" << endl;}Line::Line(void){cout << "Line()" << endl;}Line::~Line(void){cout << "~Line()" << endl;}MyPoint Line::getBegin(){return m_begin;}MyPoint Line::getEnd(){return m_end;}void Line::setBegin(MyPoint& begin){this->m_begin.setX(begin.getX());this->m_begin.setY(begin.getY());}void Line::setEnd(MyPoint& end){this->m_end.setX(end.getX());this->m_end.setY(end.getY());}void Line::setBegin(double x, double y){this->m_begin.setX(x);this->m_begin.setY(y);}void Line::setEnd(double x, double y){this->m_end.setX(x);this->m_end.setY(y);}void Line::print(){cout << "(" << m_begin.getX() << "," << m_begin.getY() << ")" << endl;cout << "(" << m_end.getX() << "," << m_end.getY() << ")" << endl;}
浅拷贝:
深拷贝:
示例:
#pragma onceclass MyArray{public:MyArray(int count);MyArray(const MyArray& arr);~MyArray(void);void setCount(int count);int getCount();void printAdd();private:int m_iCount;int *m_pArr;};
#include "MyArray.h"#include <iostream>using namespace std;void MyArray::printAdd(){cout << m_pArr << endl;}MyArray::MyArray(const MyArray& arr):m_iCount(arr.m_iCount){m_pArr = new int[m_iCount];for(int i = 0; i < m_iCount; ++i){*(m_pArr+i) = *(arr.m_pArr+i);}}MyArray::MyArray(int count):m_iCount(count){m_pArr = new int[m_iCount];for(int i = 0; i < m_iCount; ++i){*(m_pArr+i) = i;}}MyArray::~MyArray(void){delete []m_pArr;m_pArr = NULL;}void MyArray::setCount(int count){m_iCount = count;}int MyArray::getCount(){return m_iCount;}
#include <iostream>#include "MyArray.h"using namespace std;int main(){MyArray arr1(5);MyArray arr2(arr1);arr1.printAdd();arr2.printAdd();system("pause");return 0;}
这时两个对象成员的地址已经不同了
对象指针:
#include <iostream>#include "MyPoint.h"using namespace std;int main(){MyPoint *p1 = new MyPoint(1.1, 2.2);cout << p1->getX() << " " << (*p1).getX() << endl;cout << p1->getY() << " " << (*p1).getY() << endl;MyPoint *p2 = p1;cout << p1->getX() << " " << (*p1).getX() << endl;cout << p1->getY() << " " << (*p1).getY() << endl;MyPoint p3(1.1, 2.2);MyPoint *p4 = &p3;cout << p1->getX() << " " << (*p1).getX() << endl;cout << p1->getY() << " " << (*p1).getY() << endl;system("pause");return 0;}
this指针:
MyArray MyArray::printAdd(){cout << m_pArr << endl;return *this;}
#include <iostream>#include "MyArray.h"using namespace std;int main(){MyArray arr(4);cout << arr.getCount() << endl;arr.printAdd().setCount(6);//返回*this后可以继续操作//先打印地址,再设置count为6cout << arr.getCount() << endl;system("pause");return 0;}
MyArray& MyArray::printAdd(){cout << m_pArr << endl;return *this;}
#include <iostream>#include "MyArray.h"using namespace std;int main(){MyArray arr(4);cout << arr.getCount() << endl;arr.printAdd().setCount(6);//返回*this后可以继续操作//先打印地址,再设置count为6cout << arr.getCount() << endl;system("pause");return 0;}
比较两段代码可以看出,前面是返回了另一个对象,后面的是修改的当前对象。
还有返回指针:
MyArray* MyArray::printAdd(){cout << m_pArr << endl;return this;}
#include <iostream>#include "MyArray.h"using namespace std;int main(){MyArray arr(4);cout << arr.getCount() << endl;arr.printAdd()->setCount(6);//返回*this后可以继续操作//先打印地址,再设置count为6cout << arr.getCount() << endl;system("pause");return 0;}
注意它们的返回类型和调用方式
0 0
- c++ 封装 (下)
- C++Windows下SQLite简单封装
- Java:封装案例(下)
- Objective-C基础(面向对象--下--三大特性--封装)
- Qt c/C++ 混合编程,windows下的网络编程--udp封装(一)
- C#--封装
- 【C#】封装
- C语言下的封装、继承与多态
- objective-c 中队libxml2 小封装了下。
- Lua学习之Mac下封装C模块
- Linux下c实现域名转IP的方法封装
- 模块的封装(一):C语言类的封装
- [C\C++]何谓封装
- 结构体封装(C语言)
- 小朋友学C++(7):封装
- H.264的RTP封装(下)
- 2、fragment的使用封装(下)
- 2、fragment的使用封装(下)
- mcrypt.h not found. Please reinstall libmcrypt”的解决方法
- 使用libsvm实现文本分类(问题总结)
- Spring mvc 下载
- bzoj3894: 文理分科
- swift函数学习
- c++ 封装 (下)
- 关于ADC的采样率和UART的波特率
- 深入剖析之通过IAR MAP文件查看目标文件内存分配
- hadoop2配置文件详细解释
- jquery easyui datagrid纯js导出到excel
- 【JEECG示例文档】使用Kettle从mysql向oracle中抽取数据
- 运行servlet程序无法找到路径的问题(Servlet HTTP Status 404)
- 小K对于java中static的理解
- App Store2016年最新审核规则