您的位置:首页 > 编程语言 > C语言/C++

C++第十三周任务-项目一(理解基类中成员的访问限定符和派生类的继承方式)

2013-05-24 14:31 906 查看
/* 
* 程序的版权和版本声明部分 
* Copyright (c)2013, 烟台大学计算机学院学生 
* All rightsreserved. 
* 文件名称:score.cpp 
* 作    者:   王锴英 
* 完成日期: 2013 年 05 月 24 日 
* 版本号: v1.0 
* 输入描述:
* 问题描述:派生与继承
* 输出: 
*/  

//派生类B对基类A的继承
#include <iostream>
using namespace std;
class StudentA
{
    public:
    StudentA(int n,string nam,char s);
    void showA();
    ~StudentA(){}
    protected:
    int num;
    string name;
    char sex;
};
class StudentB:public StudentA
{
    public:
    StudentB(int n,string nam,char s,int a,string ad);
    void showB();
    ~StudentB(){}//析构函数
    private:
    int age;
    string addr;
};
StudentA::StudentA( int n,string nam,char s)
{
    num=n;
    name=nam;
    sex=s;
}
void StudentA::showA()
{
    cout<<"num:"<<num<<endl;
    cout<<"name:"<<name<<endl;
    cout<<"sex:"<<sex<<endl<<endl;
}
StudentB::StudentB(int n,string nam,char s,int a,string ad):StudentA(n,nam,s)
{
    age=a;
    addr=ad;
}
void StudentB::showB()
{
    cout<<"num:"<<num<<endl;
    cout<<"name:"<<name<<endl;
    cout<<"sex:"<<sex<<endl;
    cout<<"age:"<<age<<endl;
    cout<<"address:"<<addr<<endl;
}
int main()
{
    StudentB stud1(10010,"Wang-li",'f',19,"115 Beijing Road,Shanghai");
    StudentB stud2(10011,"Zhang-fun",'m',21,"213 Shanghai Roadm,Beijing ");
    StudentA stud3(20010,"He-xin",'m');
    stud1.showB();
    stud2.showA();
    stud3.showA();
    return 0;
}


运行结果:



尝试改变:

1.修改studentA类,将protected改为public后,结果不变。

2.修改studentA类,将protected改为private后,出现错误:

error: 'int StudentA::num' is private|。
3.修改B类的继承方式,将public改为protected后,出现错误:

error: 'StudentB::StudentB(int, std::string, char, int, std::string)' is protected
4.修改B类的继承方式,将public改为private后,出现错误:
error: 'StudentB::StudentB(int, std::string, char, int, std::string)' is private


5.将main函数的“stud3.showA()”改为“[b]stud3.showB()”后出现错误:[/b]

error: 'class StudentA' has no member named 'showB'

6.[b]将main函数的“stud1.showB()”改为“stud1.showA()后,结果为:[/b]



7.[b]将main函数的“stud2.showA()”改为“stud2.showB()”后,结果为:[/b]

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐