最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++關(guān)于構(gòu)造函數(shù)可向父類或者本類傳參的講解

 更新時(shí)間:2018年12月19日 11:57:19   作者:Engineer-Bruce_Yang  
今天小編就為大家分享一篇關(guān)于C++關(guān)于構(gòu)造函數(shù)可向父類或者本類傳參的講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧

 前面我們學(xué)習(xí)了C++使用初始化列表的方式來初始化字段的方法:

http://m.fzitv.net/article/153032.htm

這一節(jié)的原理和前面的差不多。

在C++的構(gòu)造函數(shù)中,子類繼承父類,那么,在創(chuàng)建一個(gè)子類成員時(shí),可以同時(shí)向父類或者子類的構(gòu)造函數(shù)進(jìn)行傳參,實(shí)現(xiàn)方法如下:

寫一個(gè)例子:mul_argc.c

#include <iostream>
#include <cstring>
using namespace std ; 
//英雄聯(lián)盟類 
class Hero
{
 private :
 int blood ; 
 string name ;
 public :
 Hero(int blood = 100, string name = "man wang")
 {
 this->blood = blood ; 
 this->name = name ;
 }
 ~Hero()
 {
 if(this->blood < 0){
  cout << "Hero blood error!\n" << endl ;
  return ;
 }
 cout << "Hero name is:" << this->name << endl ; 
 cout << "Hero blood is :" << this->blood << endl ; 
 } 
}; 
//德瑪類,繼承于英雄類 
class Dema : public Hero
{
 private :
 int blood ; 
 string name ;
 public :
 Dema(int blood , string name);
 ~Dema();
};
//在實(shí)現(xiàn)子類的構(gòu)造函數(shù)時(shí),可以同時(shí)向父類的構(gòu)造函數(shù)傳參
//例如這個(gè)例子的 : Hero(58,JS),相當(dāng)于向父類的構(gòu)造函數(shù)傳參
//當(dāng)然,子類也可以給本類的的成員傳參
Dema::Dema(int blood , string name) : Hero(89 , "JS") , blood(58) , name("dema")
{
 this->name = name ;
 this->blood = blood ; 
}
Dema::~Dema()
{
 if(this->name != "dema"){
 cout << "This Hero are no dema!" << endl;
 return ;
 }
 if(this->blood < 0){
 cout << "Dema blood error!\n" << endl ;
 return ;
 }
 cout << "Dmea name is:" << this->name << endl ; 
 cout << "Dema blood is :" << this->blood << endl ; 
}
int main(int argc , char **argv)
{
 Dema player1(79,"dema");
 return 0 ;
}

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

相關(guān)文章

最新評(píng)論

曲靖市| 崇明县| 河曲县| 长治县| 郯城县| 新巴尔虎右旗| 文山县| 广灵县| 宜良县| 文山县| 沙坪坝区| 青神县| 中超| 灵川县| 中宁县| 桐柏县| 云和县| 灵台县| 鹿邑县| 新龙县| 军事| 丹江口市| 泰顺县| 渭源县| 江川县| 清苑县| 临沂市| 尉氏县| 日照市| 遂平县| 大邑县| 泌阳县| 镇远县| 肥东县| 宁夏| 安宁市| 汉川市| 云浮市| 曲麻莱县| 睢宁县| 雅安市|