《C++ Primer》7.3.1节练习

练习7.23:

class Screen {
	private:
		unsigned height = 0,width =0;
		unsigned cursor = 0;
		string contents;
}

/*对于Screen类来说,必不可少的数据成员有:屏幕的宽度和高度、屏幕的内容以及光标的当前位置*/

练习7.24:

class Screen {
	private:
		unsigned height = 0,width =0;
		unsigned cursor = 0;
		string contents;
	public:
		Screen() = default;
		Screen(unsigned ht,unsigned wd):height(ht),width(wd),contents(ht*wd,' '){
		}
		Screen(unsigned ht,unsigned wd,char c):height(ht),width(wd),contents(ht*wd,c){
		}
}

练习7.25:

含有指针数据成员的类一般不宜使用默认的拷贝和赋值操作,如果类的数据成员都是内置类型的,则不受干扰。

Screen的4个数据成员都是内置类型(string类定义了拷贝和赋值运算符),因此直接使用类对象执行拷贝和赋值操作是可以的。

练习7.26:
要想把类的成员函数定义成内联函数,有几种不同的途径。第一种是直接把函数定义放在类的内部,第二种是把函数定义放在类的外部,并且在定义之前显式地指定inline。

class Sales_data
{
	public:
		double avg_price()const
		{
			if (units_sold)
				return revenue/units_sold;
			else
				return 0;
		}
}

class Sales_data
{
	double avg_price()const;
}

inline double Sales_data::avg_price()const
{
	if (units_sold)
		return revenue/units_sold;
	else
		return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值