c++ 封装 (下)
2016-01-21 15:41
357 查看
对象数组的内存分布:
对象成员:
浅拷贝:
深拷贝:
示例:
这时两个对象成员的地址已经不同了
对象指针:
this指针:
比较两段代码可以看出,前面是返回了另一个对象,后面的是修改的当前对象。
还有返回指针:
注意它们的返回类型和调用方式
对象成员:
#pragma once class 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 once class 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为6 cout << 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为6 cout << 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为6 cout << arr.getCount() << endl; system("pause"); return 0; }
注意它们的返回类型和调用方式
相关文章推荐
- 判断单链表是否有环(C++)
- 剖析C++编程中friend关键字所修饰的友元函数和友元类
- C++中声明类的class与声明结构体的struct关键字详解
- 自学c语言第二个/除
- atoi重写
- 【C++】优先队列priority_queue
- c++中const使用详解
- 详解C++中的函数调用和下标以及成员访问运算符的重载
- C语言 百炼成钢4
- 学习C++(2)之类的静态成员及静态成员函数
- 简单了解C++语言中的二元运算符和赋值运算符
- <笔试><面试>C/C++单链表(最综合)最全工程从建立到相关函数实现
- C++/Python
- C语言static
- <笔试><面试>C/C++单链表相关(4)判断两链表是否相交,求交点(链表不带环/可能带环)
- Java与C++的编程差异
- Ubuntu下用glade和GTK+开发C语言界面程序(三)——学习make的使用方法
- CPP Primer 5th 笔记(一)
- OpenCV人脸识别C++源码分析
- 翠竹林 Opencv+C++之人脸识别