C++快速入门---构造器和析构器(9)

C++快速入门---构造器和析构器(9)

 

使用面向对象的编程技术开发程序最基本步骤

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

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

 

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

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

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

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

 

构造器的作用是:初始化。

 

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

class Car {

       Car(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;
	unsigned int 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 = "While";
	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的时速往前移动。。。越过那高山越过那河。。。\n";
	gas_tank--;
	std::cout << "当前还剩" << 100 * gas_tank / FULL_GAS << "%" << "油量!\n";

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

	return 0;
}

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

int main()
{
	Car mycar;//定一个类的对象、实例 
	
	while (!mycar.running())
	{
		;
	}
	
	return 0;
}

 

构造对象数组:数组可以是任何一种数据类型,当然也包括对象。

如:Car mycar[10];

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

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

 

注意:每个类至少有一个构造器,如果你没有在类里定义一个构造器,编译器就会使用如下语法替你定义一个:ClassName::ClassName(){},这是一个没有代码内容的空构造器,除此之外,编译器还会替你创建一个副本构造器(CopyConstructor)。

 

 

 

析构器

构造器:用来完成事先的初始化和准备工作(申请分配内存)

析构器:用来完成事后所必须的清理工作(清理内存)。当一个对象在消亡的时候,由编译器自动调用。

class Car

{

   Car(void);   //构造器

   ~Car();   //析构器

}

代码如下:

定义存名言和作者

#include <iostream>
#include <string.h>
#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);//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())
	{
		//把刚才两个quote和speaker写入文件里面 
		fileOutput << quote << "|" << speaker << "\n";
		return true;
	}
	else
	{
		return false;
	}
}

int main()
{
	StoreQuote quote;
	
	std::cout << "请输入一句名言:\n";
	quote.inputQuote();
	
	std::cout << "请输入作者:\n";
	quote.inputSpeaker();
	
	if (quote.write())
	{
		std::cout << "成功写入文件^_^";
	}
	else
	{
		std::cout << "写入文件失败T_T";
		return 1;
	}
	
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值