《C++ Primer》7.3.1节练习
生活随笔
收集整理的這篇文章主要介紹了
《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 {double avg_price()const; }inline double Sales_data::avg_price()const {if (units_sold)return revenue/units_sold;elsereturn 0; }
總結
以上是生活随笔為你收集整理的《C++ Primer》7.3.1节练习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 西瓜视频信用分怎么看
- 下一篇: 《C++ Primer》7.3.2节练习