C++11继承的构造函数举例分析
1. C++11继承的构造函数
C++11继承的构造函数是指在C++11中,子类可以继承父类的构造函数,从而实现更简洁的构造函数定义。
2. 举例分析
下面通过一个例子来分析C++11继承的构造函数。
#include <iostream>
using namespace std;
class Base
{
public:
Base(int a, int b)
{
m_a = a;
m_b = b;
cout << "Base" << endl;
}
protected:
int m_a;
int m_b;
};
class Derived :public Base
{
public:
using Base::Base;
//Derived(int a, int b)
//{
// m_a = a;
// m_b = b;
// cout << "Derived" << endl;
//}
void print()
{
cout << m_a << "," << m_b << endl;
}
};
int main()
{
Derived d(1, 2);
d.print();
return 0;
}
从上面的代码可以看出,Base是一个父类,Derived是一个子类,Derived继承了Base,为了使用父类的构造函数,可以在子类中使用using Base::Base语句,这样就可以使用父类的构造函数,而不需要在子类中重新定义构造函数了。
3. 总结
从上面的分析可以看出,C++11继承的构造函数可以让子类继承父类的构造函数,使得构造函数的定义更加简洁,减少了程序的重复编码,提高了程序的可读性。
猜您想看
-
如何使用iKuai软路由进行迅雷下载配置
iKua...
2023年04月17日 -
python基于selenium+cv2+numpy怎样实现登录某大型电商系统
一、介绍使用P...
2023年05月22日 -
如何在Steam上找到和关注游戏开发者的动态和最新消息?
如何在Stea...
2023年05月13日 -
怎么理解dubbo
1.什么是Du...
2023年05月26日 -
Minikube中怎么搭建Knative
1. 安装Mi...
2023年05月26日 -
Spring Security要怎么学
一、Sprin...
2023年07月21日