开发者社区> 问答> 正文

在函数PrintInfo()中需要调用基类的成员函数PrintName().

对于这个题目:
定义一个基类Person,数据成员包含字符指针类型变量Name用于保存姓名,
函数成员包括默认构造函数、带形参构造函数用于初始化数据成员、输出
姓名的成员函数PrintName()。从Person类派生出Student类,增加长整型
数据成员Number用于保存学号,派生类的函数成员包括带形参构造函数用
于初始化数据成员,输出学生信息的成员函数PrintInfo()。
要求:在函数PrintInfo()中需要调用基类的成员函数PrintName().
给出下列回答:

#include
#include
using namespace std;
class Person
{
private:
char *name;
public:
Person(char *n1):name(n1){strcpy(name,n1);}
void PrintName() {cout<<"The name is:" << name;}
};
class Student:public Person
{
private:
long int Number;
public:
Student(char *n2,long int n3):Person(n2) {Number=n3;}
void PrintInfo() {cout<<"The number is:" << Number;}
};
int main()
{
char ch[]="张三";
Person a(ch);
a.PrintName();
cout<<endl;
Student b(ch,142180217);
b.PrintInfo();
cout<<endl;
return 0;
}

错误在哪??该怎么改啊??

展开
收起
a123456678 2016-03-06 11:44:27 3838 0
1 条回答
写回答
取消 提交回答
  • #include <string>
    #include <iostream>
    using namespace std;
    class Person
    {
    private:
        char *name;
    public:
        Person(char *n1) :name(n1){ strcpy(name, n1); }
        void PrintName() { cout << "The name is:" << name; }
    };
    class Student :public Person
    {
    private:
        long int Number;
    public:
        Student(char *n2, long int n3) :Person(n2) { Number = n3; }
            void PrintInfo() { cout << "The number is:" << Number << endl; this->PrintName(); }
    };
    int main()
    {
        char ch[] = "张三";
        Person a(ch);
        a.PrintName();
        cout << endl;
        Student b(ch, 142180217);
        b.PrintInfo();
        cout << endl;
        return 0;
    }
    2019-07-17 18:54:21
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
建立联系方法之一 立即下载
函数对象 立即下载
低代码开发师(初级)实战教程 立即下载