Created
October 4, 2015 14:44
-
-
Save hanxiaomax/7d1ce066bd967aabed64 to your computer and use it in GitHub Desktop.
派生类调用基类构造函数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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