一个类派生的问题

piaopiaohu123 2009-05-15 10:59:32
#include <stdafx.h>
#include <iostream.h>
#include <string.h>
//using namespace std;

class Student
{
public:
Student(int n,string name)
{
num=n;
nam=name;
}

void display()
{
cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
}
protected:
int num;
string name;
};

class Student1:public Student
{
Student1(int n,sring nam,int a):Student(n, name)
{
age=a;
}
void show()
{
display();
cout<<"age:"<<age<<endl;
}
private:
int age;

};

class Student2:public Student1
{
Student2(int n,string nam,int a,int s): Student1(n, nam, a)
{
score=s;
}
void show_all()
{
show();
cout<<"score:"<<score<<endl;
}
private:
int score;
};

int main()
{
Student2 stud(10010,"Li",17,89);
stud.show_all();

return 0;
}


大家帮我看看这个程序要怎么改,改了一晚上了,思路都乱了。。。谢谢了各位,小弟分不多。
...全文
117 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
piaopiaohu123 2009-05-15
  • 打赏
  • 举报
回复
恩,试过了,加了#include <stdafx.h>
会出来这个E:\artic\msg\ChatRoom\static\static.cpp(71) : error C2629: unexpected 'class Student ('
E:\artic\msg\ChatRoom\static\static.cpp(71) : error C2334: unexpected token(s) preceding ':'; skipping apparent function body
E:\artic\msg\ChatRoom\static\static.cpp(81) : error C2146: syntax error : missing ';' before identifier 'name'
E:\artic\msg\ChatRoom\static\static.cpp(81) : error C2501: 'string' : missing storage-class or type specifiers
E:\artic\msg\ChatRoom\static\static.cpp(81) : error C2501: 'name' : missing storage-class or type specifiers
E:\artic\msg\ChatRoom\static\static.cpp(87) : error C2629: unexpected 'class Student1 ('
E:\artic\msg\ChatRoom\static\static.cpp(87) : error C2334: unexpected token(s) preceding ':'; skipping apparent function body
E:\artic\msg\ChatRoom\static\static.cpp(102) : error C2629: unexpected 'class Student2 ('
E:\artic\msg\ChatRoom\static\static.cpp(102) : error C2334: unexpected token(s) preceding ':'; skipping apparent function body
E:\artic\msg\ChatRoom\static\static.cpp(115) : error C2661: 'Student2::Student2' : no overloaded function takes 4 parameters
lingyin55 2009-05-15
  • 打赏
  • 举报
回复
或者不用包含#include <stdafx.h> 然后选择setting,在c/c++栏,选择PreCompiled headers,然后设置第一选项,选择不使用预编译头
lingyin55 2009-05-15
  • 打赏
  • 举报
回复
#include <stdafx.h>

[Quote=引用 8 楼 piaopiaohu123 的回复:]
可是,小弟按照几位的程序,在VC6下面编译还是有点问题 fatal error C1010: unexpected end of file while looking for precompiled header directive
[/Quote]
goodname 2009-05-15
  • 打赏
  • 举报
回复
程序我是调试过的,能够运行,虽然未必是楼主要表达的意思,但是肯定不会出现这种错误。

你或者re-build一下,要么还是查查是否抄掉了什么东西。
piaopiaohu123 2009-05-15
  • 打赏
  • 举报
回复
可是,小弟按照几位的程序,在VC6下面编译还是有点问题 fatal error C1010: unexpected end of file while looking for precompiled header directive
aaaa3105563 2009-05-15
  • 打赏
  • 举报
回复
帮顶··
crst_zh 2009-05-15
  • 打赏
  • 举报
回复
楼主最大的问题是没有用virtual
goodname 2009-05-15
  • 打赏
  • 举报
回复
好像晚了一点点
chenxu_ustc 2009-05-15
  • 打赏
  • 举报
回复
如果为了锻炼下使用继承 ,类可以向你这么组织
如果真正的设计类的话 ,看你的类里面 ,并没有因为多了的数据成员score 和age 使类产生了不同的行为
一般这种情况下 ,讲score 和age 也作为他的数据成员就好了,没必要继承
goodname 2009-05-15
  • 打赏
  • 举报
回复
class Student
{
public:
Student(int n,string name)
{
num=n;
this->name=name;
}

void display()
{
cout <<"num:" <<num <<endl;
cout <<"name:" <<name <<endl;
}
protected:
int num;
string name;
};

class Student1:public Student
{
public:
Student1(int n,string name,int a):Student(n, name)
{
age=a;
}
void show()
{
display();
cout <<"age:" <<age <<endl;
}
private:
int age;

};

class Student2:public Student1
{
public:
Student2(int n,string nam,int a,int s): Student1(n, nam, a)
{
score=s;
}
void show_all()
{
show();
cout <<"score:" <<score <<endl;
}
private:
int score;
};

int main()
{
Student2 stud(10010,"Li",17,89);
stud.show_all();

return 0;
}


自己对比下吧
mengde007 2009-05-15
  • 打赏
  • 举报
回复

#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;
cout <<"name:"<<name<<endl;
}
protected:
int num;
string name;
};

class Student1:public Student
{
public:
Student1(int n,string nam,int a):Student(n, nam) { age=a; }

void show()
{
display();
cout <<"age:" <<age <<endl;
}
private:
int age;

};

class Student2:public Student1
{
public:
Student2(int n,string nam,int a,int s): Student1(n, nam, a),score(s){}

void show_all()
{
Student1::show();
cout <<"score:" <<score <<endl;
}
private:
int score;
};

int main()
{
Student2 stud(10010,"Li",17,89);
stud.show_all();

return 0;
}

nickyjay0501 2009-05-15
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
using namespace std;

class Student
{
public:
Student(int n,string name)
{
num=n;
this->name=name;
}

void display()
{
cout <<"num:" <<num <<endl;
cout <<"name:" <<name <<endl;
}
protected:
int num;
string name;
};

class Student1:public Student
{
public:

Student1(int n,string name,int a):Student(n, name)
{
age=a;
}
void show()
{
display();
cout <<"age:" <<age <<endl;
}
private:
int age;

};

class Student2:public Student1
{
public:
Student2(int n,string nam,int a,int s): Student1(n, nam, a)
{
score=s;
}
void show_all()
{
show();
cout <<"score:" <<score <<endl;
}
private:
int score;
};

int main()
{
Student2 stud(10010,"Li",17,89);
stud.show_all();

return 0;
}

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧