运行时多态性和操作员超负荷
问题在于,在主要功能指针中, 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论