新微赢技术网
标题:
yx怎么没了?
[打印本页]
作者:
龍2132
时间:
2009-11-4 02:31
标题:
yx怎么没了?
#include<iostream>
#include<string>
using namespace std;
class Student
{public:
Student(int n,string nam):num(n),name(nam){}//基类构造函数;
void display()
{
cout<<"num:"<<num<<endl<<"name:"<<name<<endl;
}
protected:
int num;
string name;
};
class Student1:public Student//public inheritance;
{public:
Student1(int n,string nam,int a):Student(n,name),age(a){}//派生类构造函数;
void display1()
{
display();
cout<<"age:"<<age<<endl;
}
private:
int age;
};
class Student2:public Student1//public inheritance;
{public:
Student2(int n,string nam,int a,char s):Student1(n,nam,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;
}
作者:
航母风云
时间:
2009-11-4 02:31
在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;
}
作者:
妈妈猪的一天
时间:
2009-11-4 02:31
如果我一定要用string呢?是不是这种情况一定不能避免?
作者:
平淡♀芳
时间:
2009-11-4 02:32
Student1(int n,string nam,int a):Student(n,name),age(a){}//派生类构造函数;
Student(n,name)中的name应该是nam
作者:
帅精灵
时间:
2009-11-4 02:32
呀,我太粗心了~~~
谢谢~~
欢迎光临 新微赢技术网 (http://bbs.weiying.cn/)
Powered by Discuz! X3.2