您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
第15讲-不是每一滴牛奶都叫特仑苏(构造器和析构器讲解)
发布时间:2021-05-15 18:02:06编辑:雪饮阅读()
类的构造函数
类的构造函数是类的一种特殊的成员函数,它会在每次创建类的新对象时执行。
构造函数的名称与类的名称是完全相同的,并且不会返回任何类型,也不会返回 void。构造函数可用于为某些成员变量设置初始值。
下面的实例有助于更好地理解构造函数的概念:
#include <iostream>
#include <string>
#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 steEngine(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::steEngine(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)
{
std::cout<<"我正在以的时速往前移动....越过那高山越过那河...\n";
gas_tank--;
std::cout<<"当前还剩"<<100*gas_tank/FULL_GAS<<"%"<<"油量!\n";
return gas_tank;
}
void Car::warning(void)
{
std::cout<<"WARNING!!"<<"还剩"<<100*gas_tank/FULL_GAS<<"%"<<"油量!";
}
int main()
{
char i;
Car mycar,car1;
while(mycar.running())
{
if(mycar.running()<10){
mycar.warning();
if(mycar.running()<=0){
std::cout<<"油尽灯枯\n";
break;
}
std::cout<<"请问是否需要加满油再行驶?(Y/N)\n";
std::cin>>i;
if('Y'==i||'y'==i){
mycar.fillTank(FULL_GAS);
}
}
}
return 0;
}
编译并运行结果如:
我正在以120的时速往前移动....越过那高山越过那河...
当前还剩24.7059%油量!
我正在以120的时速往前移动....越过那高山越过那河...
当前还剩23.5294%油量!
我正在以120的时速往前移动....越过那高山越过那河...
当前还剩22.3529%油量!
我正在以120的时速往前移动....越过那高山越过那河...
当前还剩21.1765%油量!
我正在以120的时速往前移动....越过那高山越过那河...
当前还剩20%油量!
我正在以120的时速往前移动....越过那高山越过那河...
当前还剩18.8235%油量!
我正在以120的时速往前移动....越过那高山越过那河...
当前还剩17.6471%油量!
我正在以120的时速往前移动....越过那高山越过那河...
当前还剩16.4706%油量!
我正在以120的时速往前移动....越过那高山越过那河...
当前还剩15.2941%油量!
我正在以120的时速往前移动....越过那高山越过那河...
当前还剩14.1176%油量!
我正在以120的时速往前移动....越过那高山越过那河...
当前还剩12.9412%油量!
我正在以120的时速往前移动....越过那高山越过那河...
当前还剩11.7647%油量!
我正在以120的时速往前移动....越过那高山越过那河...
当前还剩10.5882%油量!
WARNING!!还剩10.5882%油量!我正在以120的时速往前移动....越过那高山越过那河...
当前还剩9.41177%油量!
请问是否需要加满油再行驶?(Y/N)
类的析构函数
类的析构函数是类的一种特殊的成员函数,它会在每次删除所创建的对象时执行。
析构函数的名称与类的名称是完全相同的,只是在前面加了个波浪号(~)作为前缀,它不会返回任何值,也不能带有任何参数。析构函数有助于在跳出程序(比如关闭文件、释放内存等)前释放资源。
下面的实例有助于更好地理解析构函数的概念:
#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<<"\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<<"成功写入文件^_^\n";
}
else{
std::cout<<"写入文件失败T_T\n";
return 1;
}
return 0;
}
编译并运行效果如:

关键字词:构造,析构
上一篇:第14讲-(下)_闭门造车
下一篇:第16讲-this指针和类的继承