Skip to content

Instantly share code, notes, and snippets.

@hanxiaomax
Created October 4, 2015 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hanxiaomax/7d1ce066bd967aabed64 to your computer and use it in GitHub Desktop.
Save hanxiaomax/7d1ce066bd967aabed64 to your computer and use it in GitHub Desktop.
派生类调用基类构造函数
#include <iostream>
using namespace std;
class base
{
public:
base(){};
base(int i):base_(i){};
public:
int base_;
};
class inher:public base
{
public:
inher(){};
inher(int a,int b):base(8),a(a),b(b){};//如果没有base(8),则base_执行默认初始化,产生未定义行为
public:
int a;
int b;
public:
void display(){cout<<a<<" "<<b<<" | "<<base_<<endl;};
};
int main(int argc, char const *argv[])
{
inher i(1,2);
i.display();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment