C++第五课:类的使用(三)[个人见解]

继承既然已经知道了是什么意思了,下面我们来学封装!

什么是封装?我们来举个例子:

1、你不能随意挥霍父母的血汗钱,血汗钱是作为他们的私有财产,而父母因为某些原因会主动给你,你才能用的舒心,否则父母不会给你用,你也用不到。

能看出什么?对于私有财产,需要一个正当的理由来包装你要用到财产的举动,你才能获取。

在这里呢,我们就可以把它作为封装。

用形象点的话语解释:把客观事物封装成抽象的类,并且类可以把自己的数据和方法只让可信的类或者对象操作,对不可信的进行信息隐藏。

下面用代码看看封装的实现到底是个什么玩意?

首先创建两个类:Parent和Child。

Parent.h:

#ifndef __PARENT_H__
#define __PARENT_H__

class  Parent
{
public:
	 Parent();
	~ Parent();
public:			//公有的成员函数以实现封装
	void setCar(char *srccar);
	char *getCar()const;

	void setMoney(float money);
	float getMoney()const;
private:		//私有的成员变量
	char		*p_car;				//家用车
	float		p_money;			//钱
};
#endif

Parent.cpp:

#define _CRT_SECURE_NO_WARNINGS        //用到不是全新标准的函数,一定要加上这句,看报错异常的提示
#include "Parent.h"
#include <cstring>
#include <iostream>
using namespace std;
Parent::Parent()
{
}

Parent::~Parent()
{
}

//封装p_car
void Parent::setCar(char *srccar)
{
	//分配内存空间
	this->p_car =	(char *)malloc(sizeof(char));
	memset(this->p_car,0,sizeof(char));
	//判断p_car是否分配成功
	if (this->p_car== NULL)
	{
		cout << "this->p_car分配内存空间失败" << endl;
		system("pause");
		return;
	}
	strcpy(this->p_car,srccar);				    //字符串数据只能用拷贝的函数赋值,不能直接用等号
}
char *Parent::getCar()const
{
	return this->p_car;
}

//封装p_money
void Parent::setMoney(float money)
{
	this->p_money = money;
}
float Parent::getMoney()const
{
	return this->p_money;
}

Child.h:

#ifndef __CHILD_H__
#define __CHILD_H__

#include "Parent.h"

class Child
	:public Parent
{
public:
	Child();
	~Child();
	void getParentCar(Parent &parents);
	void getParentMoney(Parent &parents);
private:
	
};
#endif

Child.cpp:

#include "Child.h"
#include <iostream>
using namespace std;

Child::Child()
{
}

Child::~Child()
{
}

//获取父母的车子
void Child::getParentCar(Parent &parents)
{
	cout << "你能使用父母的" << parents.getCar() << endl;
}

//获取父母的钱
void Child::getParentMoney(Parent &parents)
{
	cout << "你能使用父母的" << parents.getMoney() << endl;
}

主体源文件:

#include <iostream>
#include "Parent.h"
#include "Child.h"
using namespace std;

int main()
{
	Parent parent;
	parent.setCar("威龙");			//设置汽车名字
	parent.setMoney(100.000000f);		//设置金额

	Child child;
	child.getParentCar(parent);
	child.getParentMoney(parent);

	system("pause");
	return 0;
}

运行下看看,写的很对,但很麻烦,如果需要加多个信息,是不是一个个设置呢?这样既耗时也麻烦,不够高逼格。

下面我们来优化下Parent.h文件的代码:

#ifndef __PARENT_H__
#define __PARENT_H__

class  Parent
{
public:
	Parent();                       //无参构造
	 Parent(char *car,float money);            //带参构造
	~ Parent();
public:			//公有的成员函数以实现封装

      char *getCar()const;

      float getMoney()const;

private:		//私有的成员变量
	char		*p_car;				//家用车
	float		p_money;			//钱
};
#endif
        

Parent.cpp:

#define _CRT_SECURE_NO_WARNINGS
#include "Parent.h"
#include <cstring>
#include <iostream>
using namespace std;
Parent::Parent()
{
}
Parent::Parent(char *car, float money)
{
		//分配内存空间
	this->p_car = (char *)malloc(sizeof(char));
	memset(this->p_car, 0, sizeof(char));
	//判断p_car是否分配成功
	if (this->p_car == NULL)
	{
		cout << "this->p_car分配内存空间失败" << endl;
		system("pause");
		return;
	}
	strcpy(this->p_car, car);
	this->p_money = money;
}

Parent::~Parent()
{
}

char *Parent::getCar()const
{
  return this->p_car;
}
float Parent::getMoney()const
{
  return this->p_money;
}

主体源文件:

#include <iostream>
#include "Parent.h"
#include "Child.h"
using namespace std;

int main()
{
	Parent parent("威龙",100.000000f);
	
	Child child;
	child.getParentCar(parent);
	child.getParentMoney(parent);

	system("pause");
	return 0;
}

Parent的四个封装函数与改动后带参构造是等同效果,不信可以运行看看。

提示:你学的不是代码怎么写,而是方式方法,而小编主要说的只是套路方法,不是什么深层次的东西,望对你有用。

无参构造能干嘛?带参构造能干嘛?封装能封装什么?还有别忘了前面学的cstring头文件里的函数。

posted @ 2019-03-26 21:18  邪气凛然  阅读(207)  评论(0编辑  收藏  举报