如何继承方法,然后向其中添加一些内容?

时间:2018-12-03 10:49:18

标签: c++ class inheritance methods

class uni_person {
    public:
    string f_name;   // 1
    string s_name;   // 2
    int age;         // 3
    string institute;// 5
    string uni;      // 6 
    string p_number; // 7

    void addSmth() {
        int x;
        cout << "What do you want to add about the student ?\n1: first name\n2: second name\n3: age\n4: course\n5: institute\n6: university\n7: phone number\n8: group\n";
        cin >> x;
        switch (x) {
            case 1: cin >> f_name; break;
            case 2: cin >> s_name; break;
            case 3: cin >> age; break;
            case 5: cin >> institute; break;
            case 6: cin >> uni; break;
            case 7: cin >> p_number; break;
            default: cout << "Error" << endl; break;
        }
    }
};

class Student: public uni_person {
    public:
    int course;
    int group;

    void addSmth() {
        int x;
        cout << "What do you want to add about the student ?\n1: first name\n2: second name\n3: age\n4: course\n5: institute\n6: university\n7: phone number\n8: group\n";
        cin >> x;
        switch (x) {
            case 4: cin >> course; break;
            case 8: cin >> group; break;
        }
    }
};

所以我有一个问题,我目前正在C ++中学习OOP,而Ive遇到了这个问题...如何使类继承其他类方法并向其中添加新内容而不重写它?有特殊功能吗? 我改写了addSmth(),并想向addSmth()添加更多行为。

2 个答案:

答案 0 :(得分:0)

首先,您应该将用户交互与实际逻辑分开。如果可以的话,可以从派生函数中显式调用Base函数:

您还应该标记基本功能virtual和派生功能override

virtual void uni_person::promptAddSmth()
{
    cout << "What do you want to add about the student ?\n1: first name\n2: second name\n3: age\n4: course\n5: institute\n6: university\n7: phone number\n8: group\n";
    int x;
    cin >> x;
    addSmth(x);
}

virtual void uni_person::addSmth(int x)
{
    switch (x) {
    case 1: cin >> f_name; break;
    case 2: cin >> s_name; break;
    case 3: cin >> age; break;
    case 5: cin >> institute; break;
    case 6: cin >> uni; break;
    case 7: cin >> p_number; break;
    default: cout << "Error" << endl; break;
    }
}

void Student::promptAddSmth() override
{
    cout << "What do you want to add about the student ?\n1: first name\n2: second name\n3: age\n4: course\n5: institute\n6: university\n7: phone number\n8: group\n";
    int x;
    cin >> x;
    addSmth(x);
}

void Student::addSmth(int x) override
{
    switch(x) {
    case 4: cin >> course; break;
    case 8: cin >> group; break;
    default: uni_person::addSmth(x); break;
    }
}

答案 1 :(得分:0)

没有办法向C ++中无法期望的方法添加更多行为。但是您可以从开始就设计添加方法:

class uni_person
{
public:
    string f_name;   // 1
    string s_name;   // 2
    int age;         // 3
    string institute;// 5
    string uni;      // 6
    string p_number; // 7

    void addSmth()
    {
        int x;
        cout << "What do you want to add about the student ?\n1: first name\n2: second name\n3: age\n4: course\n5: institute\n6: university\n7: phone number\n8: group\n";
        cin >> x;
        switch (x) {
            case 1: cin >> f_name; break;
            case 2: cin >> s_name; break;
            case 3: cin >> age; break;
            case 5: cin >> institute; break;
            case 6: cin >> uni; break;
            case 7: cin >> p_number; break;
            default: cout << "Error" << endl; break;
        }

        // Call additional code. Does nothing by default.
        appendix();
    }

protected:

    virtual void appendix() {}
};

class Student: public uni_person
{
public:
    int course;
    int group;

private:
    void appendix() override
    {
        // Code from your Student::addSmth().
        // Will be called at the end of uni_person::addSmth().

        int x;
        cout << "What do you want to add about the student ?\n1: first name\n2: second name\n3: age\n4: course\n5: institute\n6: university\n7: phone number\n8: group\n";
        cin >> x;
        switch (x) {
            case 4:case 4: cin >> course; break;
            case 8: cin >> group; break;
        }
    }
};

另一个子类可能会添加一些其他行为,由于虚拟性,这些行为将被正确调用。

还请注意其他参与者关于您的设计和代码风格的建议。