运行时多态性和操作员超负荷

发布于 2025-02-07 19:37:17 字数 2687 浏览 1 评论 0原文

问题在于,在主要功能指针中, list 呼叫operator+此类。但是,如何通过指针到父母类 stack )来调用子类(queue stack )的超载运算符。

#include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std;

class list
{
public:
    list* head; 
    list* tail; 
    list* next; 
    int num;
    virtual ~list() {}
    list() { head = tail = next = NULL; num = 0; }
    virtual list operator+(int i) { return *this; };
    virtual int operator-() { return 0; };
};
class queue : public list
{
public:
    list operator+(int i);
    int operator-();
};

class stack : public list
{
public:
    list operator+(int i);
    int operator-();
};

int main()
{
    srand(time(NULL)); rand();
    list* p;
    queue q_ob;
    stack s_ob;
    char ch;
    for (;;)
    {
        cout << "Enter something else to stop.\nStack, Queue or ULL? (S/Q): \n";
        cin >> ch;
        ch = tolower(ch);
        if (ch == 'q')
            p = &q_ob;
        else if (ch == 's')
            p = &s_ob;
        else break;
        p + (rand() % 100);
    }
    cout << "Enter T to terminate\n";
    for (;;)
    {
        cout << "Remove from Stack, Queue or ULL? (S/Q):";
        cin >> ch;
        ch = tolower(ch);
        if (ch == 'q')
            p = &q_ob;
        else if (ch == 's')
            p = &s_ob;
        else break;
        cout << -(*p) << '\n';
    }
    return 0;
}

list queue::operator+(int i)
{
    list* item;
    item = new queue;
    if (!item)
    {
        cout << "Allocation error.\n";
        exit(1);
    }
    item->num = i;
    // put on end of list:
    if (tail)
        tail->next = item;
    tail = item;
    item->next = NULL;
    if (!head)
        head = tail;
    return *this;
}

int queue::operator-()
{
    int i;
    list* p;
    if (!head)
    {
        cout << "List empty.\n";
        return 0;
    }
    // remove from start of list
    i = head->num;
    p = head;
    head = head->next;
    delete p;
    return i;
}

list stack::operator+(int i)
{
    list* item;
    item = new stack;
    if (!item)
    {
        cout << "Allocation error.\n";
        exit(1);
    }
    item->num = i; // put on front of list
    // for stack - like operation
    if (head)
        item->next = head;
    head = item;
    if (!tail)
        tail = head;
    return *this;
}

int stack::operator-()
{
    int i;
    list* p;
    if (!head)
    {
        cout << "List empty.\n";
        return 0;
    }
    // remove from start of list:
    i = head->num;
    p = head;
    head = head->next;
    delete p;
    return i;
}

The problem is that in main function pointer to abstract class list calls operator+ of this class. But how can I call overloaded operators from child classes (queue and stack) with pointer to parent class list

#include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std;

class list
{
public:
    list* head; 
    list* tail; 
    list* next; 
    int num;
    virtual ~list() {}
    list() { head = tail = next = NULL; num = 0; }
    virtual list operator+(int i) { return *this; };
    virtual int operator-() { return 0; };
};
class queue : public list
{
public:
    list operator+(int i);
    int operator-();
};

class stack : public list
{
public:
    list operator+(int i);
    int operator-();
};

int main()
{
    srand(time(NULL)); rand();
    list* p;
    queue q_ob;
    stack s_ob;
    char ch;
    for (;;)
    {
        cout << "Enter something else to stop.\nStack, Queue or ULL? (S/Q): \n";
        cin >> ch;
        ch = tolower(ch);
        if (ch == 'q')
            p = &q_ob;
        else if (ch == 's')
            p = &s_ob;
        else break;
        p + (rand() % 100);
    }
    cout << "Enter T to terminate\n";
    for (;;)
    {
        cout << "Remove from Stack, Queue or ULL? (S/Q):";
        cin >> ch;
        ch = tolower(ch);
        if (ch == 'q')
            p = &q_ob;
        else if (ch == 's')
            p = &s_ob;
        else break;
        cout << -(*p) << '\n';
    }
    return 0;
}

list queue::operator+(int i)
{
    list* item;
    item = new queue;
    if (!item)
    {
        cout << "Allocation error.\n";
        exit(1);
    }
    item->num = i;
    // put on end of list:
    if (tail)
        tail->next = item;
    tail = item;
    item->next = NULL;
    if (!head)
        head = tail;
    return *this;
}

int queue::operator-()
{
    int i;
    list* p;
    if (!head)
    {
        cout << "List empty.\n";
        return 0;
    }
    // remove from start of list
    i = head->num;
    p = head;
    head = head->next;
    delete p;
    return i;
}

list stack::operator+(int i)
{
    list* item;
    item = new stack;
    if (!item)
    {
        cout << "Allocation error.\n";
        exit(1);
    }
    item->num = i; // put on front of list
    // for stack - like operation
    if (head)
        item->next = head;
    head = item;
    if (!tail)
        tail = head;
    return *this;
}

int stack::operator-()
{
    int i;
    list* p;
    if (!head)
    {
        cout << "List empty.\n";
        return 0;
    }
    // remove from start of list:
    i = head->num;
    p = head;
    head = head->next;
    delete p;
    return i;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文