您的位置:首页 > 编程语言 > C语言/C++

c++ 封装 (下)

2016-01-21 15:41 357 查看
对象数组的内存分布:



对象成员:

#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;
}




注意它们的返回类型和调用方式
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: