小甲鱼-C++快速入门笔记(16)之定义构造器和析构器

面向对象的编程技术开发程序的步骤:

(1)定义一个有属性和方法的类(模板)

(2)为该类创建一个变量(实现)

1、构造器

构造器和通常的方法的主要区别:

(1)构造器的名字必须和它所在的类的名字一样;

(2)系统在创建某个类的实例时会第一时间自动调用这个类的构造器;

(3)构造器永远不会返回任何值

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

using namespace std;

class Car
{
public:
	string color;
	string engine;
	float gas_tank;
	unsigned int wheel;

	//函数声明
	Car(void);   //构造函数
	void setColor(string col);
	void setEngine(string eng);
	void setWheel(unsigned int whe);
	void fill_tank(float liter);
	int running(void);
	void warning(void);
};

Car::Car(void)  //定义构造函数
{
	color = "WHITE";
	engine = "V8";
	wheel = 4;
	gas_tank = FULL_GAS;

}

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

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

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

void Car::fill_tank(float liter)
{
	gas_tank += liter;
}

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

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

	if(gas_tank < 10)
	{
		warning();
		cout << "请问是否需要加满油再行驶?[Y/N]" << endl;
		cin >> i;
		
		if('Y' == i || 'y' == i)
		{
			fill_tank(FULL_GAS);
		}
	}

	if(0 == gas_tank)
	{
		cout << "抛锚中.....";
		return 0;
	}

	return 1;
}

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

int main()
{
	Car mycar;

	while (mycar.running())
	{
		;	
	}

	return 0;
}

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

CalssName::ClassName(){}

 

2、构造对象数组

Car mycar[10];

//调用语法
mycar[x].running;

3、定义析构器

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

析构器和构造器/类有着一样的名字,只是前边多了一个波浪符"~"前缀

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

注意:

(1)析构器永远不返回任何值

(2)析构器不带参数,所以析构器的声明永远是如下格式: ~ClassName();

(3)在比较复杂的类里,析构器往往至关重要(可能引起内存泄露)

练习:

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

using namespace std;

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

	StoreQuote();  
	~StoreQuote(); 

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

StoreQuote::StoreQuote()
{
	fileOutput.open("test.txt", ios::app);  //打开文件,以追加的形式写入
}

StoreQuote::~StoreQuote()
{
	fileOutput.close();   //关闭文件
}

void StoreQuote::inputQuote()
{
	getline(cin, quote);   //读取名言
	cout << "quote: " << quote << endl;
}

void StoreQuote::inputSpeaker()
{
	cin.ignore(1);
	getline(cin, speaker);  //读取作者
}

bool StoreQuote::write()
{
	if(fileOutput.is_open())
	{
		fileOutput << quote << "|" << speaker << endl;   //写入文件
		return true;
	}
	else
	{
		return false;
	}
}

int main()
{
	StoreQuote quote;

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

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

	if(quote.write())
	{
		cout << "成功写入文件^_^" << endl;
	}
	else
	{
		cout << "写入文件失败T_T" << endl;
		return 1;
	}
	return 0;
}

问题: 按照视频中的代码跑出现了一个bug,就是无法写入作者的名字,后来发现是把前一次的回车写进去了,也就是写入了空格.

解决方法: 在inputSpeaker()函数中加入cin.ignore(1), 表示忽略回车,写入正常

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值