这篇文章主要介绍了C++中构造函数向父类或者本类传参的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
在C++的构造函数中,子类继承父类,那么,在创建一个子类成员时,可以同时向父类或者子类的构造函数进行传参,实现方法如下:
写一个例子:mul_argc.c
#include <iostream>
#include <cstring>
using namespace std ;
//英雄联盟类
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();
};
//在实现子类的构造函数时,可以同时向父类的构造函数传参
//例如这个例子的 : Hero(58,JS),相当于向父类的构造函数传参
//当然,子类也可以给本类的的成员传参
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 ;
}
感谢你能够认真阅读完这篇文章,希望小编分享的“C++中构造函数向父类或者本类传参的示例分析”这篇文章对大家有帮助,同时也希望大家多多支持天达云,关注天达云行业资讯频道,更多相关知识等着你来学习!