构造器和析构器讲解

构造器

构造器是类里的一种特殊的方法。它和通常方法的主要区别:

- 构造器的名字必须和它所在的类的名字一样

- 系统在创建每个类的实例时会第一时间自动调用 这个类的构造器

- 构造器永远不会返回任何值

创建构造器,需要先把它的声明添加到类里:

class Car
{
    Car(void);
};

在结束声明之后开始定义构造器本身:

Car::Car(void) //不用写void Car::Car(void),因为构造器默认不会有任何返回值
{
	color = "WHITE";
	engine = "V8";
	wheel = 4;
	gas_tank = FULL_GAS;
};

例子

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

#define FULL_GAS 85
class Car
{
public:
	std::string color;
	std::string engine;
	float gas_tank;
	unsigned int wheel;

	Car(void);
	void setColor(std::string col);
	void setEngine(std::string eng);
	void setWheel(unsigned int whe);
	void fillTank(int liter);
	int running(void);
	void warning(void);
};

Car::Car(void)
{
	color = "WHITE";
	engine = "V8";
	wheel = 4;
	gas_tank = FULL_GAS;
};

void Car::setColor(std::string col)
{
	color = col;
}

void Car::setEngine(std::string eng)
{
	engine = eng;
}

void Car::setWheel(unsigned int whe)
{
	wheel = whe;
}

void Car::fillTank(int liter)
{
	gas_tank += liter;
}

int Car::running(void)
{
	char i;

	std::cout << "我正在以120的时速往前移动" << std::endl;
	gas_tank--;
	std::cout << "当前还剩" << 100 * gas_tank / FULL_GAS << "%" << "油量" << std::endl;

	if (gas_tank < 10)
	{
		std::cout << "请问是否需要加满油再行驶?(Y/N)" << std::endl;
		std::cin >> i;
		if ('Y' == i || 'y' == i)
		{
			fillTank(FULL_GAS);
		}
		if (0 == gas_tank)
		{
			std::cout << "抛锚中" << std::endl;
			return 1;
		}
	}

	return 0;
}

void Car::warning(void)
{
	std::cout << "WARNING!!" << "还剩" << 100 * gas_tank / FULL_GAS << "%" << "油量" << std::endl;
}

int main()
{
	Car mycar;

	while (!mycar.running())
	{
		;
	}

	return 0;
}

构造对象数组,之前我们已经说过,数组可以是任何一种数据类型,当然也包括对象

如:Car mycar[10]

调用语法依旧是:mycar[x].running;

注:x代表这给定数组元素的下标。


每个类至少有一个构造器,如果你没有在类里定义一个构造器,编译器就会使用如下语法替你定义一个:

ClassName::ClassName(){}

这是一个没有代码内容的空构造器,除此之外,编译器还会替你创建一个副本构造器(CopyConstructor)。


析构器

从前面的内容我们了解到,在创建对象时,系统都会自动调用一个特殊的方法,即构造器。

相应的,在销毁一个对象时,系统也应该会调用另一个特殊方法达到对应效果,这就是析构器。

一般来说,构造器用来完成事先的初始化和准备动作(申请分配内存),析构器用来完成事后所必须的清理工作(清理内存)。

构造器和析构器两者相辅相成,有许多共同之处。首先,析构器有着和构造器/类一样的名字,只不过在前面多了一个波浪符“~”前缀。

class Car
{
	Car(void);
	~Car();
};

其次,析构器也永远不返回任何值。

另外,析构器是不带参数的。所以析构器的声明永远是如下合适:~ClassName();

在上面的例子中析构器是可有可无的,但是在比较复杂的类里,析构器往往至关重要(可能引起内存泄漏)。

例如每个类的构造器申请了一块内存,我们就必须在析构器里释放那块内存。

#include <iostream>
#include <string>
#include <fstream>

class StoreQuote
{
public:
	std::string quote, speaker;
	std::ofstream fileOutput;

	StoreQuote();
	~StoreQuote();

	void inputQuote();
	void inputSpeaker();
	bool write();
};

StoreQuote::StoreQuote()
{
	fileOutput.open("test.txt", std::ios::app);
}

StoreQuote::~StoreQuote()
{
	fileOutput.close();
}

void StoreQuote::inputQuote()
{
	std::getline(std::cin, quote);
}

void StoreQuote::inputSpeaker()
{
	std::getline(std::cin, speaker);
}

bool StoreQuote::write()
{
	if (fileOutput.is_open())
	{
		fileOutput << quote << "|" << speaker << std::endl;
		return true;
	}
	else
	{
		return false;
	}
}

int main()
{
	StoreQuote quote;

	std::cout << "请输入一句名言:" << std::endl;
	quote.inputQuote();

	std::cout << "请输入作者:" << std::endl;
	quote.inputSpeaker();

	if (quote.write())
	{
		std::cout << "成功输入文件";
	}
	else
	{
		std::cout << "写入文件失败";
		return 1;
	}
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值