斗转星移——面向对象(三)

析构函数

作用:
对象销毁前,做清理工作。

具体的清理工作,一般和构造函数对应
比如:如果在构造函数中,使用new分配了内存,就需在析构函数中用delete释放。

如果构造函数中没有申请资源(主要是内存资源),
那么很少使用析构函数。

函数名:
~类型
没有返回值,没有参数,最多只能有一个析构函数

访问权限:
一般都使用public

使用方法:
不能主动调用。
对象销毁时,自动调用。
如果不定义,编译器会自动生成一个析构函数(函数体为空,什么也不做)

#include <iostream>
#include <Windows.h>
#include <string>
#include <string.h>

using namespace std;

// 定义一个“人类”
class Human {
public:
	Human();
	Human(int age, int salary);
	~Human(); //析构函数

private:
	string name;
	int age;
	int salary;
	char *addr;
};

Human::Human() {
	name = "无名氏";
	age = 18;
	salary = 30000;

	addr = new char[64];
	strcpy_s(addr, 64, "China");
	cout << "调用默认构造函数-" << this << endl;
}

Human::~Human() {
	cout << "调用析构函数-" << this  << endl;  //用于打印测试信息
	delete addr;
}

void test() {
	Human h1;

	{
		Human h2;
	}
	cout << "test()结束" << endl;
}

int main(void) {
	test();

	system("pause");
	return 0;
}

注意:
如同我们之前所讲,欠债就得还钱,new了内存,就要记得delete掉。

永不迷失的this指针

Human::Human(int age, int salary) {
	cout << "调用自定义的构造函数" << endl;
	this->age = age;      //this是一个特殊的指针,指向这个对象本身
	this->salary = salary;
	/*
	在这个构造函数中我们使用this指针访问数据成员age和salary
	说明this就是对应的对象的地址,且this只能指向这个对象,不能改变
	*/
}
class Human {
public:
	Human();
	Human(int age, int salary);
	......

	void thisTestError(Human *other) {
		this = other;  // 将报错!this堪称“永不迷失的真爱”
	}
    ......
};

“人人共享”的静态数据成员

假设现在项目需要获取总的人数,如何实现?
只能使用一个全局变量,然后在构造函数中对这个全局变量进行修改(加1)
缺点:使用全局变量不方便,破坏程序的封装性。

解决方案:
使用类的静态成员。

Human.h

class Human {
public:
	......
int getCount();
private:
	string name;
	int age;
	......

	// 类的静态成员
	static int count;
};

Human.cpp

#include "Human.h"	

// 初始化非const的类的静态成员,不能在类内初始化
int Human::count = 0;
......

Human::Human() {
	name = "无名氏";
	age = 18
	count++;
}

// 类的普通成员函数,可以直接访问静态成员(可读可写)
int Human::getCount() {
	return count;
}

main.cpp

#include "Human.h"
#include <iostream>

using namespace std;

int main(void) {
	Human h1;
	cout << h1.getCount() << endl;	//1

	Human h2;
	cout << h1.getCount() << endl;	//2

	system("pause");
	return 0;
}

注意:
对于非const的类静态成员,只能在类的实现文件中初始化。
const类静态成员,可以在类内设置初始值,也可以在类的实现文件中设置初始值。(但是不要同时在这两个地方初始化,只能初始化1次)

“不能拥有的方法”:静态成员函数

我们继续上面的思考:
当需要获取总的人数时,还必须通过一个对象来访问,比如h1.getCount().
如果当前没有可用的对象时,就非常尴尬,不能访问getCount()!

如果为了访问总的人数,而特意去创建一个对象,就很不方便,
而且得到的总人数还不真实(包含了一个没有实际用处的人)

解决方案:
把getCount()方法定义为类的静态方法!

类的静态方法:
1.可以直接通过类来访问【更常用】,也可以通过对象(实例)来访问。
2.在类的静态方法中,不能访问普通数据成员和普通成员函数(对象的数据成员和成员函数)

Human.h

#pragma once
......
class Human {
public:
    ......
	static int getCount();
    ......
};

Human.cpp

#include "Human.h"

//静态方法的实现,不能加static
int Human::getCount() { 
	//  静态方法中,不能访问实例成员(普通的数据成员)
	// cout << age;

	// 静态方法中,不能访问this指针
	// 因为this指针是属于实例对象的
	// cout << this;

	//静态方法中,只能访问静态数据成员
	return count;
}

main.cpp

#include "Human.h"
#include <iostream>

using namespace std;

void test() {
	cout << "总人数: ";
	// ??? 没有可用的对象来访问getCount()

	// 直接通过类名来访问静态方法!
	// 用法:类名::静态方法
	cout << Human::getCount(); 
}

int main(void) {
	Human h1, h2;

	test();	//2	

	system("pause");
	return 0;
}

说明:
1)静态数据成员

对象的成员函数(没有static的成员函数)内部,可以直接访问“静态数据成员”
类的静态成员函数(有static的成员函数)内部,可以直接访问“静态数据成员”
即:所有的成员函数,都可以访问静态数据成员。

2)静态成员函数

对象可以直接访问静态成员函数
类可以直接访问静态成员函数(Human::getHumanCount())
在类的静态成员函数(类的静态方法)内部,不能直接访问this指针和对象的数据成员(非static数据成员)!
在类的静态成员函数(类的静态方法)内部,只能访问类的数据成员(static数据成员)

我还是从前那个少年const成员

需求分析:
怎样表示人的“血型”?
血型可以修改吗?

解决方案:
把血型定义为const数据类型(常量数据成员)

const数据成员的初始化方式:
1.使用类内值(C++11支持)
2.使用构造函数的初始化列表
(如果同时使用这两种方式,以初始化列表中的值为最终初始化结果)
注意: 不能在构造函数或其他成员函数内,对const成员赋值!

Human.h

#pragma once
......
class Human {
public:
    ......
private:
    ......
	const string bloodType;
};

Human.cpp

// 使用初始化列表,对const数据成员初始化
Human::Human():bloodType("未知") {
     ......

	//在成员函数内,不能对const数据成员赋值
	//bloodType = "未知血型";
	count++;
}

void Human::description() const {
	cout << "age:" << age
		<< " name:" << name
		<< " salary:" << salary
		<< " addr:" << addr 
		<< " bloodType:" << bloodType << endl; 
		//其他成员函数可以“读”const变量
}

Main.cpp

int main(void) {
	Human h1;

	h1.description();

	system("pause");
	return 0;
}

const成员函数

需求分析:
const的Human对象,不能调用普通的成员函数。

分析:
C++认为,const(常量)对象,如果允许去调用普通的成员函数,而这个成员函数内部可能会修改这个对象的数据成员!而这将导致const对象不再是const对象!
【类比】:专一男就是const对象,撩妹方法,就是普通的成员函数,如果允许专一男调去撩妹,那么专一男,也就不专一了!

解决方案:
如果一个成员函数内部,不会修改任何数据成员,就把它定义为const成员函数。

//Human.h
class Human {
public:
    ......
	void description() const;  //注意,const的位置
    ......
};

//Human.cpp
void Human::description ()const {
	cout << "age:" << age
		<< " name:" << name
		<< " salary:" << salary
		<< " addr:" << addr 
		<< " bloodType:" << bloodType << endl;
}

//main.cpp
int main(void) {
	const Human h1;
	h1.description();
	
	system("pause");
	return 0;
}

注意:
const成员函数内,不能修改任何数据成员!

C++的成员函数设置建议:
如果一个对象的成员函数,不会修改任何数据成员,那么就强烈建议:
把这个成员函数,定义为const成员函数!

建模的常用手段:组合与聚合

说明:组合和聚合,不是C++的语法要求,是应用中的常用手段。

组合
需求:
构建一个计算机类,一台计算机,由CPU芯片,硬盘,内存等组成。
CPU芯片也使用类来表示。

CPU.h

#pragma once

#include <string>

class CPU
{
public:
	CPU(const char *brand = "intel", const char *version="i5");
	~CPU();
private:
	std::string brand; //品牌
	std::string version; //型号
};

CPU.cpp

#include "CPU.h"
#include <iostream>


CPU::CPU(const char *brand, const char *version)
{
	this->brand = brand;
	this->version = version;
	std::cout << __FUNCTION__ << std::endl;
}


CPU::~CPU()
{
	std::cout << __FUNCTION__ << std::endl;
}

Computer.h

#pragma once
#include "CPU.h"

class Computer
{
public:
	Computer(const char *cpuBrand, const char *cpuVersion, 
		int hardDisk, int memory);
	~Computer();
private:
	CPU cpu;      // Computer和CPU是“组合”关系
	int hardDisk; //硬盘, 单位:G
	int memory;   //内存, 单位:G
};

Computer.cpp

#include "Computer.h"
#include <iostream>


Computer::Computer(const char *cpuBrand, const char *cpuVersion,
	int hardDisk, int memory):cpu(cpuBrand, cpuVersion)
{	//只能使用初始化列表对CPU cpu进行初始化
	this->hardDisk = hardDisk;
	this->memory = memory;

	std::cout << __FUNCTION__ << std::endl;
}


Computer::~Computer()
{
	std::cout << __FUNCTION__ << std::endl;
}

Main.cpp

#include <iostream>
#include <Windows.h>
#include <string>
#include <string.h>
#include "Computer.h"

using namespace std;

void test() {
	Computer a("intel", "i9", 1000, 8);
}

int main(void) {
	test();

	system("pause");
	return 0;
}

小结:
被拥有的对象(芯片)的生命周期与其拥有者(计算机)的生命周期是一致的。
计算机被创建时,芯片也随之创建。
计算机被销毁时,芯片也随之销毁。
拥有者需要对被拥有者负责,是一种比较强的关系,是整体与部分的关系。

具体组合方式:
1)被组合的对象直接使用成员对象。(常用)
2)使用指针表示被组合的对象,在构造函数中,创建被组合的对象;在析构函数中,释放被组合的对象。


聚合
需求:
给计算机配一台音响。

Computer.h

#pragma once
#include "CPU.h"
#include "VoiceBox.h"

class Computer
{
public:
	Computer(const char *cpuBrand, const char *cpuVersion, 
		int hardDisk, int memory);
	~Computer();

	void addVoiceBox(VoiceBox *box);
private:
	CPU cpu;  // Computer和CPU是“组合”关系
	int hardDisk; //硬盘, 单位:G
	int memory;   //内存, 单位:G

	VoiceBox *box; //音箱和计算机是聚合关系
};

Computer.cpp

#include "Computer.h"
#include <iostream>

Computer::Computer(const char *cpuBrand, const char *cpuVersion,
	int hardDisk, int memory):cpu(cpuBrand, cpuVersion)
{
	this->hardDisk = hardDisk;
	this->memory = memory;

	std::cout << __FUNCTION__ << std::endl;
}

void Computer::addVoiceBox(VoiceBox *box) {
	this->box = box;
}


Computer::~Computer()
{
	std::cout << __FUNCTION__ << std::endl;
}

Main.cpp

#include <iostream>
#include <Windows.h>
#include <string>
#include <string.h>
#include "Computer.h"
#include "VoiceBox.h"

using namespace std;

void test(VoiceBox *box) {
	Computer a("intel", "i9", 1000, 8);
	a.addVoiceBox(box);
}

int main(void) {
	VoiceBox box;

	test(&box);

	system("pause");
	return 0;
}

小结:
聚合不是组成关系,被包含的对象,也可能被其他对象包含。
拥有者,不需要对被拥有的对象的生命周期负责。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值