实验书本上关于继承的代码(摘抄)

公有继承:

#include<iostream>
using namespace std;
class student
{
public:
    void get_value()
    {
        cin >> num >> name >> sex;
    }
    void display()
    {
        cout << "num:" << num << endl;
        cout << "name:" << name << endl;
        cout << "sex:" << sex << endl;
    }
private:
    int num;
    string name;
    char sex;
};

class Student1:public student
{
public:
    void display_1()
    {
        //display();  //改为此行后正确,“只能通过基类的公用成员函数来引用基类的私有数据成员”,课本上如是说。
        cout << "num:" << num << endl;  //报错在这一行 派生类企图引用基类的私有变量!
        cout << "age:" << age <<endl;
        cout << "address:" << addr << endl;
    }
private:
    int age;
    string addr;
};

int main()
{
    student srm;
    srm.get_value();
    srm.display();
    Student1 zxy;
    zxy.get_value();
    zxy.display();
}
程序报错

正确代码如下,用基类的公用成员函数去引用基类的私有数据成员!

#include<iostream>
using namespace std;
class student
{
public:
    void get_value()
    {
        cin >> num >> name >> sex;
    }
    void display()
    {
        cout << "num:" << num << endl;
        cout << "name:" << name << endl;
        cout << "sex:" << sex << endl;
    }
private:
    int num;
    string name;
    char sex;
};

class Student1:public student
{
public:
    void get_value2()
    {
        get_value();
        cin >> age >> addr;
    }

    void display_1()
    {
        display();
        //cout << "num:" << num << endl;
        cout << "age:" << age <<endl;
        cout << "address:" << addr << endl;
    }
private:
    int age;
    string addr;
};

int main()
{
    student srm;
    srm.get_value();
    srm.display();
    Student1 zxy;
    zxy.get_value2();
    zxy.display_1();
}



私有继承

#include<iostream>
using namespace std;
class student
{
public:
    student(int i=25,string s="srm",char c='b'):num(i),name(s),sex(c){}
    void get_value()
    {
        cin >> num >> name >> sex;
    }
    void display()
    {
        cout << "num:" << num << endl;
        cout << "name:" << name << endl;
        cout << "sex:" << sex << endl;
    }
private:
    int num;
    string name;
    char sex;
};

class Student1:private student
{
public:
    void get_value2()
    {
        get_value();
        cin >> age >> addr;
    }

    void display_1()
    {
        display();
        //cout << "num:" << num << endl;
        cout << "age:" << age <<endl;
        cout << "address:" << addr << endl;
    }
private:
    int age;
    string addr;
};

int main()
{
    student srm;
    //srm.get_value();
    srm.display();
    Student1 zxy;
    zxy.get_value2();
    //zxy.get_value();  //用这个函数就开始出错了,因为get_value()是基类的公用,继承后变成了派生类的私有
    zxy.display_1();
}


简单的将继承改为 私有继承,仍然能编译通过,


加入构造函数

#include<iostream>
using namespace std;
class student
{
public:
    student(int i=25,string s="srm",char c='b'):num(i),name(s),sex(c){}
    void get_value()
    {
        cin >> num >> name >> sex;
    }
    void display()
    {
        cout << "num:" << num << endl;
        cout << "name:" << name << endl;
        cout << "sex:" << sex << endl;
    }
private:
    int num;
    string name;
    char sex;
};

class Student1:private student
{
public:
    Student1(int i=25,string s="rcz"):student(),age(i),addr(s){}
    void get_value2()
    {
        get_value();
        cin >> age >> addr;
    }

    void display_1()
    {
        display();
        //cout << "num:" << num << endl;
        cout << "age:" << age <<endl;
        cout << "address:" << addr << endl;
    }
private:
    int age;
    string addr;
};

int main()
{
    student srm;
    //srm.get_value();
    srm.display();
    cout << endl;
    Student1 zxy;
    //zxy.get_value();
    zxy.display_1();
}

保护成员与保护继承

保护成员对类的用户来说是私有成员,类外不可访问,但是保护成员可以被类的派生类的成员函数引用!

就像父辈的保险箱,只能被子女(其派生类)打开。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值