练习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;elsereturn 0;}
}
class Sales_data
{double avg_price()const;
}inline double Sales_data::avg_price()const
{if (units_sold)return revenue/units_sold;elsereturn 0;
}