C++之類-2

#include<iostream>

using namespace std;

class Screen
{
public:
    //用typedef進(jìn)行同義替換,且同義替換詞在內(nèi)部在外部都是一樣的
    typedef std::string::size_type index;
    //寫在類的內(nèi)部的都是內(nèi)聯(lián)函數(shù)
    //定義構(gòu)造函數(shù)
    Screen(index ht = 0, index wd = 0) :contents(ht*wd, 'A'), cursor(0), height(ht), width(wd)
    {

    }
    Screen(index ht, index wd, const std::string &conts);

    /*char get() const;
    {
        return contents[cursor];
    }*/
    /*//如果在類的內(nèi)部只寫類的聲明而將函數(shù)的內(nèi)容寫在類的外部,那么這個(gè)函數(shù)就不再是內(nèi)聯(lián)函數(shù)了
    如果還是希望將寫在外面的函數(shù)是內(nèi)聯(lián)的,那么就需要在函數(shù)前面添加關(guān)鍵字inline,外部函數(shù)或者內(nèi)部聲明前面都可以*/
    char get() const;
    //和普通的函數(shù)一樣,類的成員函數(shù)也是可以重載的
    char get(index r, index c) const
    {
        index row = r*width;

        return contents[row + c];
    }
private:
    std::string contents;
    index cursor;
    index height, width;
};
inline char Screen::get() const
{
    return contents[cursor];
}

Screen::Screen(index ht, index wd, const std::string &conts) :contents(conts), cursor(0), height(ht), width(wd)
{

}
int main()
{
    Screen a(10,100);
    cout << a.get() << endl;
    cout << a.get() << endl;

    Screen b(6, 16, "hello screen class");
    cout << b.get() << endl;
    cout << b.get(0,4) << endl;

    cout << "測試一下" << endl;


    return 0;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容