一个简单的例子:
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
Student(int n=01,string nam="vivi",char s='f'){num=n;name=nam;sex=s;} //基类构造函数;
void display()
{
cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
}
protected:
int num;
string name;
char sex;
};
class Student1: public Student
{
public:
Student1(int a=24,string add="xxxxxxxxxxxx"){age=a;addr=add;} //派生类构造函数;
void display_1()
{ cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"age:"<<age<<endl;
cout<<"address:"<<addr<<endl;
}
private:
int age;
string addr;
};
int main()
{
Student1 stud;
stud.display_1();
return 0;
}
请问:
在用派生类Student1建立对象时,使用的是默认参数的构造函数,根据派生类构造函数的定义,应该只对age和addr赋值,并没有调用基类的构造函数,为什么输入出时,从基类继承下来的那些数据成员也赋上默认值了呢?
新手,请赐教!