程序设计二(面向对象)_实训13_虚函数实验答案补充2

很多粉丝朋友留言表示在程序设计二(面向对象)_实训13_虚函数实验的第二关:普通非成员函数的动态绑定 的答案当中,缺少了Linked List部分的答案,现在予以补充:

建立一个继承体系,List 是基类,ArrayListLinkedList 是其派生类。并且编写实现下述函数并达到如下效果。

 
  1. ostream& operator << (ostream&os, const List&rhs);

做一个流输出运算符重载,其第二个参数是List的常引用类型。我们知道子类的对象天生可以作为父类类型使用,因此

 
  1. ArrayList a;
  2. LinkedList b;
  3. operator << (cout,a);
  4. operator << (cout,b);

这上面的调用显然都是合法的。但是现在要求实现如下效果:第 3 行的函数执行的是适合 ArrayList 输出的代码,而第 4 行执行的是适合 LinkedList 输出的代码。即,虽然调用的函数一样,但需要根据当时的实参类型选择合适的实现。相当于对非成员函数做到动态绑定。

相关知识

前面说过,虚函数可以实现动态绑定。也就是:

 
  1. class List{
  2. public:
  3. virtual void disp(ostream&os)const;
  4. };
  5. class ArrayList : public List{
  6. public:
  7. void disp(ostream&os)const{/*这里是ArrayList的实现*/}
  8. }
  9. class LinkedList : public List{
  10. public:
  11. void disp(ostream&os)const{/*这里是LinkedList的实现*/}
  12. }
  13. List *p = new ArrayList;
  14. p->disp(cout);//此时调用的是ArrayList的disp函数
  15. delete p;
  16. p = new LinkedList;
  17. p->disp(cout);//此时调用的是LinkedList的disp函数
  18. delete p;

如果是非成员函数,能不能实现类似效果呢?即

 
  1. ostream& operator << (ostream&os, const List&rhs);
  2. ostream& operator << (ostream&os, const ArrayList&rhs);
  3. ostream& operator << (ostream&os, const LinkedList&rhs);
  4. List *p = new ArrayList;
  5. cout<<*p<<endl;//此时希望调用的是operator<<(ostream,ArrayList&)
  6. delete p;
  7. p = new LinkedList;
  8. cout<<*p<<endl;//此时希望调用的是operator<<(ostream,LinkedList&)
  9. delete p;

遗憾的是,非成员函数不具备这样的功能。对于上述流输出运算符调用而言,它的实参会被看作是 List 类型,因此只会调用上文中的第一个重载函数。而不会根据 p 实际指向的类型动态调用其他流输出重载函数。 如何令非成员函数也体现出动态绑定的特性呢?答案是通过成员函数,而不是通过函数重载。例如,可以这样实现流输出运算符重载:

 
  1. ostream& operator << (ostream&os,const List&rhs){
  2. rhs.disp(os);
  3. return os;
  4. }
  5. /**************************************************
  6. *这里不再需要实现
  7. * ostream& operator << (ostream&os, const List&rhs);
  8. *和
  9. * ostream& operator << (ostream&os, const List&rhs);
  10. *等函数
  11. *****************************************************/

编程要求

根据提示,在右侧编辑器的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_

 

  • 5
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值