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

初学者关于类的错误,求指导!!

深蓝灬 发布于 2011-12-27 19:59, 607 次点击
题目:编写一个学生数据输入和显示程序,学生数据有编号、姓名、班号和成绩4项。要求将其中的编号、姓名和显示设计成一个类person,并作为学生数据操作类student的基类。试编写基类person和派生类student,并写出主程序运行它们。
#include<iostream.h>
class person
{
protected:
    long int number;
    char name[10];
public:
    person(int number,char name[10])
    {
      this->number=number;
      this->name=name;
    }
    show()
    {
      cout<<"姓名:"<<name<<endle
          <<"班号:"<<number<<endle;
    }
}
class student:public person
{
public:float score;
       int classnumber;
       student(score,classnumber)
       {
         this->score=score;
         this->classnumber=classnumber;
       }
       show()
       {
         cout<<"班号:"<<classnumber<<endle
             <<"成绩:"<<score<<endle;
       }
}
void main()
{
    student A(90,1):person(2010104105,"小于");
    A.person::show();
    A.student::show();
}
哪里有错误,用vc++6.0 编译通不过!!
3 回复
#2
hellovfp2011-12-28 11:22
错误太多了,类不打分号结束,show函数没有返回参数应该定义为void,姓名使用std::string,而不用字符数组。
继承的意义你没有掌握。

#include <iostream>
#include <string>

class person
{
protected:
    long int number;
    std::string name;
public:
    person(int number, std::string name)
    {
        this->number = number;
        this->name = name;
    }
    void show()
    {
        std::cout<<"姓名:"<<name<< std::endl
            <<"班号:"<<number<< std::endl;
    }

}; //here

class student:public person
{
    float score;
    int classnumber;
public:
      
    student(int number, std::string name, float score, int classnumber)
        :person(number, name)
       {
         this->score=score;
         this->classnumber=classnumber;
       }
       void show() //here
       {
           person::show();
           std::cout<<"班号:"<<classnumber<<std::endl
               <<"成绩:"<<score<< std::endl;
       }
}; //here
void main()
{
    student A(2010104105,"小于", 90,1);//here,中文分号错
    A.show();
}
#3
waterstar2011-12-28 16:32
头文件包含不需要.h了,原因是在C++标准中很多旧的头文件都被标准化了,这样就可以使用一部固定的手册而不用担心你在使用哪一个编译器了。为了和旧的头文件有所不同,新的头文件去掉 了.h后缀。因此#include <iostream.h>现在成了#include < iostream>很多其他的头文件也是如此。

并且新的头文件包含在标准名空间(standard namespace)中。在新的头文件中的任何对象,如cout 和 endl 都能在名空间std中找到,也可以被独立的访问。
#4
BianChengNan2011-12-30 12:49
以下是引用深蓝灬在2011-12-27 19:59:39的发言:

题目:编写一个学生数据输入和显示程序,学生数据有编号、姓名、班号和成绩4项。要求将其中的编号、姓名和显示设计成一个类person,并作为学生数据操作类student的基类。试编写基类person和派生类student,并写出主程序运行它们。
#include
class person
{
protected:
    long int number;
    char name[10];
public:
    person(int number,char name[10])
    {
      this->number=number;
      this->name=name;
    }
    show()
    {
      cout<<"姓名:"<
你应该先自己检查一下自己的代码.....
1