c++ 封装 (下)

对象数组的内存分布:




对象成员:


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



注意它们的返回类型和调用方式



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值