注册 登录
编程论坛 C++教室

[求助]求助错在哪?(有关继承)

xweiweix 发布于 2007-01-22 19:39, 722 次点击
/*编写一个学生和教师数据输入和显示程序,学生数据有编号、姓名、班号和成绩,教师数据有编号、姓名、
职称和部门。要求将编号、姓名输入和显示设计成一个类person,并作为学生数据操作类student和教师数
据操作类teacher的基类。*/
#include<iostream.h>
#include<string.h>
class Person
{
public:
int id;
char name[10];
void show(Student s)
{
s.set();
cout << id << endl;
cout << name << endl;
s.print();
}

};
class Student:public Person
{
public:
int classid;
double score;
void set()
{
cout << "请输入编号:" << endl;
cin >> classid;
cout << "请输入成绩:" << endl;
cin >> score;
}
void print()
{
cout << 编号:" << classid << endl;
cout << "成绩:" << score << endl;
}

};
class Teacher:public Person
{
public:
char zhichen[10];
char bumen[10];

};
void main()
{
Person a;
strcpy(a.name,"СÃ÷");
a.id=2;
Student s1;
a.show(s1);
}

//程序还没写完整 可是一直报错 达人帮忙指点下
6 回复
#2
tyc6112007-01-22 20:45
1. 你应该把错误贴出来,不然别人怎么给你分析
2. 你的程序太不规范了,比如头文件应该用<iostream>而不是<iostream.h>(.h形式已经被摒弃),main函数返回int型
3. 设计类时,一般应该把数据成员设计为private,不然,你怎么体现封闭性?
4. 传递类类型时,能用引用就用引用,能用const引用就用const引用
#3
dragonfly2007-01-23 08:34
在person类里的show函数的参数是什么类型?student类在这里还没定义,肯定出错
#4
xweiweix2007-01-23 09:25
show函数的形参类型是对象类型啊 就是传进去一个Student类的对象
还是一头雾水 达人们能不能说的具体点啊
#5
tyc6112007-01-23 10:40
以下是引用xweiweix在2007-1-23 9:25:00的发言:
show函数的形参类型是对象类型啊 就是传进去一个Student类的对象
还是一头雾水 达人们能不能说的具体点啊

在类Person前,放上前置声明:class Student;

#6
dragonfly2007-01-23 10:41
编译是从前到后的!
在你Person类声明前有没有Student类的声明呢?
编译器能认Student吗?
能不出错吗?
还没明白吗?
#7
song42007-01-23 20:38
#include<iostream.h>
#include<string.h>
class student;
class Person
{
public:
int id;
char name[10];
void show(Student s)
{
s.set();
cout << id << endl;
cout << name << endl;
s.print();
}

};
class Student:public Person
{
public:
int classid;
double score;
void set()
{
cout << "请输入编号:" << endl;
cin >> classid;
cout << "请输入成绩:" << endl;
cin >> score;
}
void print()
{
cout << 编号:" << classid << endl;
cout << "成绩:" << score << endl;
}

};
class Teacher:public Person
{
public:
char zhichen[10];
char bumen[10];

};
void main()
{
Person a;
strcpy(a.name,"СÃ÷");
a.id=2;
Student s1;
a.show(s1);
}
你这么做是干什么呢
让基类调用子类函数来完成功能???
看看虚函数吧
1