很多粉丝朋友留言表示在程序设计二(面向对象)_实训13_虚函数实验的第二关:普通非成员函数的动态绑定 的答案当中,缺少了Linked List部分的答案,现在予以补充:
建立一个继承体系,List
是基类,ArrayList
和 LinkedList
是其派生类。并且编写实现下述函数并达到如下效果。
ostream& operator << (ostream&os, const List&rhs);
做一个流输出运算符重载,其第二个参数是List
的常引用类型。我们知道子类的对象天生可以作为父类类型使用,因此
ArrayList a;
LinkedList b;
operator << (cout,a);
operator << (cout,b);
这上面的调用显然都是合法的。但是现在要求实现如下效果:第 3 行的函数执行的是适合 ArrayList
输出的代码,而第 4 行执行的是适合 LinkedList
输出的代码。即,虽然调用的函数一样,但需要根据当时的实参类型选择合适的实现。相当于对非成员函数做到动态绑定。
相关知识
前面说过,虚函数可以实现动态绑定。也就是:
class List{
public:
virtual void disp(ostream&os)const;
};
class ArrayList : public List{
public:
void disp(ostream&os)const{/*这里是ArrayList的实现*/}
}
class LinkedList : public List{
public:
void disp(ostream&os)const{/*这里是LinkedList的实现*/}
}
List *p = new ArrayList;
p->disp(cout);//此时调用的是ArrayList的disp函数
delete p;
p = new LinkedList;
p->disp(cout);//此时调用的是LinkedList的disp函数
delete p;
如果是非成员函数,能不能实现类似效果呢?即
ostream& operator << (ostream&os, const List&rhs);
ostream& operator << (ostream&os, const ArrayList&rhs);
ostream& operator << (ostream&os, const LinkedList&rhs);
List *p = new ArrayList;
cout<<*p<<endl;//此时希望调用的是operator<<(ostream,ArrayList&)
delete p;
p = new LinkedList;
cout<<*p<<endl;//此时希望调用的是operator<<(ostream,LinkedList&)
delete p;
遗憾的是,非成员函数不具备这样的功能。对于上述流输出运算符调用而言,它的实参会被看作是 List
类型,因此只会调用上文中的第一个重载函数。而不会根据 p
实际指向的类型动态调用其他流输出重载函数。 如何令非成员函数也体现出动态绑定的特性呢?答案是通过成员函数,而不是通过函数重载。例如,可以这样实现流输出运算符重载:
ostream& operator << (ostream&os,const List&rhs){
rhs.disp(os);
return os;
}
/**************************************************
*这里不再需要实现
* ostream& operator << (ostream&os, const List&rhs);
*和
* ostream& operator << (ostream&os, const List&rhs);
*等函数
*****************************************************/
编程要求
根据提示,在右侧编辑器的Begin-End区域内补充代码。
Arry.list.cpp
/****************start from here**********************/
#include"ArrayList.h"
#include<iostream>
using namespace std;
ArrayList::ArrayList()
{
size = 0;
data = new int[10];
capacity = 10;
}
ArrayList::ArrayList(const ArrayList &rhs)
{
data = new int[rhs.capacity];
size = rhs.size;
for (int i = 0; i < size; i++)
{
data[i] = rhs.data[i];
}
}
ArrayList::ArrayList(int const a[], int n)
{
data = new int[n];
size = n;
for (int i = 0; i < n; i++)
{
data[i] = a[i];
}
}
ArrayList::ArrayList(int n, int value)
{
data = new int[n];
size = n;
for (int i; i < n; i++)
{
data[i] = value;
}
}
ArrayList::~ArrayList()
{
delete[] data;
}
void ArrayList::insert(int pos, int value)
{
if (size == capacity)
{
int *old = data;
data = new int[2 * size];
for (int i = 0; i < size; i++)
{
data[i] = old[i];
}
capacity = 2 * size;
}
for (int i = size - 1; i >pos; i--)
data[i + 1] = data[i];
data[pos] = value;
size++;
}
void ArrayList::remove(int pos)
{
for (int i = pos; i < size - 1; i++)
data[i] = data[i + 1];
size--;
}
int ArrayList::at(int pos) const
{
return data[pos];
}
void ArrayList::modify(int pos, int newValue)
{
data[pos] = newValue;
}
void ArrayList::setCapacity(int newCapa)
{
if(newCapa<capacity)
return ;
else
{
capacity=newCapa;
int *newdata=new int[capacity];
for(int i=0;i<size;i++)
{
newdata[i]=data[i];
}
delete[]data;
data=newdata;
}
}
link.list.cpp
/****************start from here**********************/
#include"LinkedList.h"
#include<iostream>
using namespace std;
LinkedList::LinkedList() : List(0)
{
head = new Node();
}
LinkedList::LinkedList(const LinkedList &rhs) : List(rhs.size)
{
head = new Node;
Node *p = head;
for (Node *q = rhs.head->next; q; q = q->next)
{
p->next = new Node(q->data, nullptr);
p = p->next;
}
}
LinkedList::LinkedList(int const a[], int n) : List(n)
{
head = new Node();
Node *p = head;
for (int i = 0; i < n; i++)
{
p->next = new Node(a[i], nullptr);
p = p->next;
}
}
LinkedList::LinkedList(int n, int value) : List(n)
{
head = new Node();
Node *p = head;
for (int i = 0; i < n; i++)
{
p->next = new Node(value, nullptr);
p = p->next;
}
}
LinkedList::~LinkedList()
{
Node *p = head;
while (head->next != NULL)
{
p = head->next;
delete head;
head = p;
}
delete head;
}
LinkedList::Node* LinkedList::advance(int pos)const
{
Node *p=head;
for(int i=-1;i<pos;i++)
{
p=p->next;
}
return p;
}
void LinkedList::insert(int pos, int value)
{
Node *p = head;
for (int i = -1; i < pos - 1; i++)
{
p = p->next;
}
Node *q = new Node(value, p->next);
p->next = q;
size++;
}
void LinkedList::remove(int pos)
{
Node *p = head;
for (int i = -1; i < pos - 1; i++)
{
p = p->next;
}
Node *q = head;
for (int j = -1; j < pos + 1; j++)
{
q = q->next;
}
p->next = q;
size--;
}
int LinkedList::at(int pos) const
{
Node *p = head;
for (int i = -1; i < pos; i++)
{
p = p->next;
}
return p->data;
}
void LinkedList::modify(int pos, int newValue)
{
Node *p = head;
for (int i = -1; i < pos; i++)
{
p = p->next;
}
p->data = newValue;
}
void LinkedList::disp(ostream &os) const
{
Node *p = head;
while (p->next != NULL)
{
p = p->next;
cout << p->data << " ";
}
}
linklist.h
#ifndef _LIST_H_
#define _LIST_H_
#include <iostream>
using std::ostream;
using namespace std;
class List{
protected:
int size;
public:
//¼æ¾ßĬÈϹ¹Ô캯ÊýºÍ¹¦Äܹ¹Ô캯Êý
List(int s=0):size(s){}
//¿½±´¹¹Ô캯Êý
List(const List&rhs):size(rhs.size){}
/*ÒÔÏÂΪÐ麯Êý*/
//×÷Ϊ¼Ì³ÐµÄ»ùÀàµÄÎö¹¹º¯ÊýÒ»¶¨ÒªÊÇÐéµÄ
virtual ~List(){}
//ÆÕͨµÄ·ÇÐ麯Êý
int getSize()const{return size;}
/*ÒÔÏÂΪ´¿Ð麯Êý£¬¼´Ã»ÓÐʵÏÖ*/
virtual void insert(int pos,int value)=0;
virtual void remove(int pos)=0;
virtual int at(int pos)const=0;
virtual void modify(int pos,int newValue)=0;
/*ÄãµÄ¹¤×÷ÔÚÕâÀ´Ë´¦Éè¼Ædispº¯Êý*/
virtual void disp(ostream&os)const
{
for(int i=0,n=getSize();i<n;i++)
{
os<<at(i)<<" ";
}
}
};
#endif // _LIST_H_