《C++ Primer Plus(第六版)》(29)(第十四章 C++中的代码重用 复习题答案)

14.6 复习题

1.以A栏的类为基类时,B栏的类采用共有派生还是私有派生更合适。

AB派生类型
class Bearclass PolarBear公有派生
class Kitchenclass Home私有派生
class Personclass Programmer公有派生
class Personclass HorseAndJockey私有派生
class Person, class Automobileclass Driver人是公有派生,汽车是私有派生

2.假设有下面的定义:

	class Frabjous
	{
	private:
		char fab[20];
	public: 
		Frabjous(const char* s = "C++") :fab(s){}
		virtual void tell(){ cout << fab; }
	}; 

	class Gloam
	{
	private:
		int glip;
		Frabjous fb;
	public: 
		Gloam(int g = 0, const char* s = "C++");
		Gloam(int g, const Frabjous& f);
		void tell();
	};

假设Gloam版本的tell()应显示glip和fb的值,请为这3个Glom方法提供定义。

	class Frabjous
	{
	private:
		char fab[20];
	public: 
		Frabjous(const char* s = "C++")
		{
			strcpy_s(fab, 20, s);
		}
		virtual void tell(){ cout << fab; }
	}; 

	class Gloam
	{
	private:
		int glip;
		Frabjous fb;
	public: 
		Gloam(int g = 0, const char* s = "C++") :fb(s), glip(g){}
		Gloam(int g, const Frabjous& f) : fb(f), glip(g){}
		void tell()
		{
			fb.tell();
			cout << glip << endl;
		}
	};
好像VS对一些字符串的处理有了更多的限制。


3.假设有下面的定义:

	class Frabjous
	{
	private:
		char fab[20];
	public: 
		Frabjous(const char* s = "C++")
		{
			strcpy_s(fab, 20, s);
		}
		virtual void tell(){ cout << fab; }
	}; 

	class Gloam : private Frabjous
	{
	private:
		int glip;
	public: 
		Gloam(int g = 0, const char* s = "C++");
		Gloam(int g, const Frabjous& f);
		void tell();
	};
假设Gloam版本的tell()应显示glip和fab的值,请为这3个Gloam方法提供定义。

	class Frabjous
	{
	private:
		char fab[20];
	public: 
		Frabjous(const char* s = "C++")
		{
			strcpy_s(fab, 20, s);
		}
		virtual void tell(){ cout << fab; }
	}; 

	class Gloam : private Frabjous
	{
	private:
		int glip;
	public: 
		Gloam(int g = 0, const char* s = "C++") :Frabjous(s), glip(g){}
		Gloam(int g, const Frabjous& f) : Frabjous(f), glip(g){}
		void tell()
		{
			Frabjous::tell();
			cout << glip << endl;
		}
	};

4.假设有下面的定义,它是基于程序清单14.13中的Stack模板和程序清单14.10中的Worker类的:

Stack<Worker * > sw;

请写出将生成的类声明。只实现类声明,不实现非内联类方法。

	 class Stack<Worker*>
	 {
	 private:
		 enum{ MAX = 10 };
		 Worker* items[MAX];
		 int top;
	 public:
		 Stack();
		 bool isempty();
		 bool isfull();
		 bool push(const Worker*& item);
		 bool pop(Worker* & item);
	 };
	  
	 Stack<Worker*>::Stack()
	 {
		 top = 0;
	 }
	  
	 bool Stack<Worker*>::isempty()
	 {
		 return top == 0;
	 }
	  
	 bool Stack<Worker*>::push(const Worker*& item)
	 {
		 if (top < MAX)
		 {
			 items[top++] = item;
			 return true;
		 }
		 else
		 {
			 return false;
		 }
	 }
	  
	 bool Stack<Worker*>::pop(Worker* & item)
	 {
		 if (top > 0)
		 {
			 item = items[--top];
			 return true;
		 }
		 else
		 {
			 return false;
		 }
	 }

5.使用本章中的模板定义对下面内容进行定义:

string对象数组

double数组栈

指向Worker对象的指针栈数组。

程序清单14.18生成了多少个模板类定义?

ArrayTP<string> sa;

StackTP<ArrayTP<double> > stack_arr_db;

ArrayTP< StackTP<Worker*> > arr_stk_wpr;

程序清单14.18,生成了四种模板:ArrayTP<int, 10>, ArrayTP<double, 10>,ArrayTP<int,5>,Array<ArrayTP<int,5>,10>

6.指出虚基类与非虚基类之间的区别。

对于非虚基类,如果两条继承路线有相同的祖先,则类中将包含祖先成员的两个拷贝

虚基类可以解决这种问题。






转载于:https://www.cnblogs.com/fablegame/p/6430235.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值