一杯清酒邀明月
天下本无事,庸人扰之而烦耳。

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

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

2、系统在创建某个类的对象时会第一时间自动调用这个类的构造器  

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

实例1:构造器的运用

 1 #include <iostream>
 2 
 3 #define FULL_GAS 85 
 4 
 5 class Car//让我们来造辆车,定义类Car ,C++允许在类里面声明常量,但不允许对它进行赋值 
 6 {
 7 public:
 8     std::string color;
 9     std::string engine;
10     unsigned int gas_tank;//油缸 
11     unsigned int wheel;
12     
13     Car(void);//类Car的构造函数,系统会自动调用
14     void setColor(std::string col);
15     void setEngine(std::string eng);
16     void setWheel(unsigned int whe);
17     void filltank(int liter);//加油
18     int running(void);//
19     void warning(void); 
20 };
21 Car::Car(void)
22 {
23     color = "White";
24     engine = "V8";
25     wheel = 4;
26     gas_tank = FULL_GAS;
27 } 
28 void Car::setColor(std::string col) 
29 {
30     color = col;
31 }
32 void Car::setEngine(std::string eng)
33 {
34     engine = eng;
35 }
36 void Car::setWheel(unsigned int whe)
37 {
38     wheel = whe;
39 } 
40 void Car::filltank(int liter)//函数(又称方法)的定义 
41 {
42     gas_tank += liter;
43 }
44 int Car::running(void)
45 {
46     char i;
47      
48     std::cout << "我正在以120的时速往前移动。。。\n";
49     gas_tank--;
50     std::cout << "当前还剩 " << 100*gas_tank/FULL_GAS << "%" << "油量!\n";
51     
52     if(gas_tank < 10)
53         {
54             warning();
55             std::cout << "请问是否需要加满油再行驶?(Y/N)\n";
56             std::cin >> i;
57             if( 'Y'== i || 'y' == i)
58             {
59                 filltank(FULL_GAS); 
60             }
61             
62             if(gas_tank == 0)
63             {
64                 std::cout << "抛瞄中。。。。。";
65                 return 0;
66             }
67         }
68     
69     return gas_tank; 
70 } 
71 void Car::warning(void)
72 {
73     std::cout << "WARNING!!" << "\n还剩 " <<  100*gas_tank/FULL_GAS << "%" << "油量!\n";
74 } 
75 
76 int main()
77 {
78     Car mycar;
79     
80     mycar.gas_tank = FULL_GAS;
81     
82     while(mycar.running())//有油则继续跑,没有油则结束
83     {
84         ;
85     }   
86     return 0;
87 }

在销毁一个对象时,系统会调用另一个特殊方法,即析构器。

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

析构器不返回任何值,也不带参数

实例2:构造器与析构器的共同应用

 1 #include <iostream>
 2 #include <string>
 3 #include <fstream>
 4 
 5 class StoreQuote//定义类StoreQuote 
 6 {
 7 public:
 8     std::string quote,speaker;
 9     std::ofstream fileOutput;//定义文件对象fileOutput 
10     
11     StoreQuote();//构造器 
12     ~StoreQuote();//析构器
13     
14     void inputQuote();//名言输入 
15     void inputSpeaker();//作者输入 
16     bool write();//写文件 
17 };
18 
19 StoreQuote::StoreQuote()
20 {
21     fileOutput.open("test.txt", std::ios::app);//以附加(app)的形式打开文件 
22 }
23 
24 StoreQuote::~StoreQuote()//关闭文件,释放内存 
25 {
26     fileOutput.close();
27 }
28 
29 void StoreQuote::inputQuote()
30 {
31     std::getline(std::cin, quote);
32 }
33 void StoreQuote::inputSpeaker()
34 {
35     std::getline(std::cin, speaker);
36 }
37 
38 bool StoreQuote::write()
39 {
40     if(fileOutput.is_open())//文件是否打开成功 
41     {
42         fileOutput << quote << "|" << speaker << "\n";//写入数据 
43         return true;
44     }
45     else
46     {
47         return false;
48     }
49 }
50 
51 int main()
52 {
53     StoreQuote quote;//使用StoreQuote类定义一个对象quote 
54     
55     std::cout << "请输入一句名言:\n";
56     quote.inputQuote();
57     
58     std::cout << "请输入作者:\n";
59     quote.inputSpeaker();
60     
61     if(quote.write())//文件是否写入成功 
62     {
63         std::cout << "成功写入文件"; 
64     }
65     else
66     {
67         std::cout << "写入文件失败"; 
68         return 1;
69     }
70     return 0;
71 }
posted on 2023-08-18 14:17  一杯清酒邀明月  阅读(90)  评论(0编辑  收藏  举报