#include <iostream>
#include<stdlib.h>
using namespace std; class A
{
public:
int _a;
A()
{
_a = ;
}
void print()
{
printf("%d\n", _a);
}
}; class B: public A
{
public:
int _a;
B()
{
_a = ;
}
}; int main()
{
B b;
b.print();
printf("%d\n", b._a);
return ;
}
运行的结果为:
子类会覆盖父类的变量
#include <iostream>
#include<stdlib.h>
using namespace std; class base
{
private:
int m_i;
int m_j;
public:
base(int i):m_j(i),m_i(m_j){}
base():m_j(),m_i(m_j){}
int get_i(){return m_i;}
int get_j(){return m_j;}
}; int main()
{
base obj();
cout<<obj.get_i()<<endl
<<obj.get_j()<<endl; return ;
}
运行结果为:
构造函数的初始化变量顺序是按照成员变量的声明顺序来执行的。