|
在c++中用string定义字符串容易出问题,改了几处换成char型就没得问题了
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
void setName(char *strname)
{
strcpy(name,strname);
}
Student(int n,char *strname):num(n)
{
setName(strname);
}//基类构造函数;
void display()
{
cout<<"num:"<<num<<endl<<"name:"<<name<<endl;
}
protected:
int num;
char name[20];
};
class Student1:public Student//public inheritance;
{public:
Student1(int n,char *strname,int a):Student(n,strname),age(a)
{
}//派生类构造函数;
void display1()
{
display();
cout<<"age:"<<age<<endl;
}
private:
int age;
};
class Student2:public Student1//public inheritance;
{public:
Student2(int n,char *strname,int a,char s):Student1(n,strname,a),sex(s)
{
}//间接派生类构造函数;
void display2()
{
display1();
cout<<"sex:"<<sex<<endl;
}
private:
char sex;
};
int main()
{
Student2 stud(120,"yx",22,'m');// 为什么结果yx未出现?
stud.display2();
system("pause");
return 0;
} |
|