#《Essential C++》讀書筆記# 第四章 基於對象的編程風格

基礎知識

Class的定義由兩部分組成:class的聲明,以及緊接在聲明以後的主體。主體部分由一對大括號括住,並以分號結尾。主體內的兩個關鍵字public和private,用來標示每一個塊的「member訪問權限」。Public member能夠在程序的任何地方被訪問,private member只能在member function或是class friend內被訪問。ios

class的前置聲明(forward declaration),將class名稱告訴編譯器,並未提供此class的任何其餘信息(像class支持的操做行爲及所包含的data member等)。前置聲明使咱們得以進行類指針(class pointer)的定義,或以此class做爲數據類型。編程

全部member function都必須在class主體內進行聲明。至因而否同時進行定義,可自由決定。若是要在class主體內定義,這個member function會自動被視爲inline函數。要在class主體以外定義member function,必須使用特殊的語法,目的在於分辨該函數究竟屬於哪個class,使用雙冒號「::」,即所謂的class scope resolution(類做用域解析)運算符。數組

對於inline函數而言,定義與class主體內或主體外,並無什麼分別。然而就像non-member-inline function同樣,它也應該放在頭文件中。class定義一塊兒inline member function一般都會被放在與class同名的頭文件中。non-inline member function應該在程序代碼文件中定義,該文件一般與class同名,其後接着擴展名.C、.cc、.cpp或.cxx(x表明橫放的+)。session

初始化data member,魔法不會自動產生,編譯器不會自動爲咱們處理。若是咱們提供一個或多個特別的初始化函數,編譯器就會在每次class object被定義出來時,調用適當的函數加以處理。這些特別的初始化函數稱爲constructor(構造函數)。Constructor的函數必須與class名稱相同。語法規定,constructor不該指定返回類型,亦不用返回任何值,它能夠被重載(overloaded)。最簡單的constructor是所謂的default constructor,它不須要任何參數(argument)。(沒有參數不須要加括號,加括號會將其解釋爲一個函數,這是爲了兼容C所致)。app

和constructor對立的是destructor。所謂destructor乃是用戶自定義的一個class member。一旦某個class提供有destructor,當其object結束生命時,便會自動調用destructor處理善後。Destructor主要用來釋放在constructor中或對象生命週期中分配的資源。Destructor的名稱有嚴格規定:class名稱再加上‘~’前綴。它絕對不會有返回值,也沒有任何參數。因爲其參數列表是空的,因此也毫不可能別重載(overloaded)。Destructor並不是絕對必要,咱們沒有義務非得提供destructor,事實上,C++編程的最難部分之一,即是瞭解什麼時候須要定義destructor而什麼時候不須要。ide

當咱們設計class時,必須問問本身,在此class之上進行「成員逐一初始化」的行爲模式是否適當?若是答案確定,咱們就不須要另外提供copy constructor。若是答案否認的,咱們就必須另行定義copy constructor,並在其中編寫正確的初始化操做。若是有必要爲某個class編寫copy constructor,那麼一樣有必要爲它編寫copy assignment operator。函數

static member function 能夠在「與任何對象都無瓜葛」的情形之下調用。member function 只有在「不訪問任何non-static member」的條件下才可以被聲明爲static,聲明方式是在聲明以前加上關鍵字static。當咱們在class主體外部進行member function的定義時,無需重複加上關鍵字static,這個規則也適用於static data member。測試

運算符重載規則:this

  • 不能夠引入新的運算符。除了「.」、「.*」、「::」、「?:」四個運算符,其餘運算符皆可被重載。
  • 運算符的操做數(operand)個數不能夠改變。每一個二元運算符都須要兩個操做數,每一個一元運算符都須要剛好一個操做數。所以,咱們沒法定義出一個equality運算符,並令它接受兩個以上或兩個如下的操做數。
  • 運算符的優先級(precedence)不可改變。例如,除法的運算優先級永遠高於加法。
  • 運算符函數的參數列表中,必須至少有一個參數爲class類型。也就是說,咱們沒法爲諸如指針之類的non-class類型,從新定義其原已存在的運算符,固然沒法爲它引進新的運算符。

對於increment遞增運算符(或遞減)的重載,分爲前置和後置兩個版本。前置版的參數列表是空的,後置版的參數列表本來也應該時空,然而重載規則要求,參數列表必須獨一無二。所以,C++想出一個變通辦法,要求後置版得有一個int參數。int參數從何發生,又到哪裏去,編譯器會自動會後置產生一個int參數(其值必爲0),用戶沒必要爲此煩惱。spa

所謂friend,具有了與class member function相同的訪問權限,能夠訪問class的private member。只要在某個函數原型(prototype)前加上關鍵字friend,就能夠將它聲明爲某個class的friend。這份聲明能夠出如今class定義的任意位置上,不受private或public的影響。若是你但願將數個重載函數都聲明爲某個class的friend,必須明確地爲每一個函數加上關鍵字friend。

maximal munch編譯規則,此規則要求,每一個符號序列(symbol seuqence)老是以「合法符號序列」中最長的那個解釋。由於>>是個合法的運算符序列,所以若是兩個>之間沒有空白,這兩個符號一定會被合在一塊兒看待。一樣道理,若是咱們寫下a+++p,它一定會被解釋爲「a++ +p」。

練習題答案

練習4.1 創建Stack.h和Stack.suffix,此處的suffix是你的編譯器所能接受的擴展名,或是你的項目所使用的擴展名。編寫main()函數,練習操做Stack的全部公開接口,並加以編譯執行。程序代碼文件和main()都必須包含Stack.h:#include 「Stack.h」

Stack.h
#include <string>
#include <vector>

using namespace std;

class Stack
{
public:
    bool push(const string&);
    bool pop(string& elem);
    bool peek(string& elem);
    bool empty() const { return _stack.empty(); }
    bool full() const { return _stack.size() == _stack.max_size(); }
    int size() const { return _stack.size(); }
private:
    vector<string> _stack;
};

Stack.cpp
#include "Stack.h"

bool Stack::pop(string& elem)
{
    if (empty())    return false;
    elem = _stack.back();
    _stack.pop_back();
    return true;
}

bool Stack::peek(string& elem)
{
    if (empty())    return false;
    elem = _stack.back();
    return true;
}

bool Stack::push(const string& elem)
{
    if (full()) return false;
    _stack.push_back(elem);
    return true;
}

main.cpp
#include "Stack.h"
#include <iostream>

int main()
{
    Stack st;
    string str;
    while (cin >> str && !st.full())
    {
        st.push(str);
    }
    if (st.empty())
    {
        cout << '\n' << "Oops: no strings were read -- bailing out\n ";
        return 0;
    }
    st.peek(str);
    if (st.size() == 1 && str.empty())
    {
        cout << '\n' << "Oops: no strings were read -- bailing out\n ";
        return 0;
    }
    cout << '\n' << "Read in " << st.size() << " strings!\n"
        << "The strings, in reverse order: \n";
    while (st.size())
    {
        if (st.pop(str))
            cout << str << ' ';
    }
    cout << '\n' << "There are now " << st.size()
        << " elements in the stack!\n";
}

練習4.2 擴展Stack功能,以支持find()和count()兩個操做。find()會查看某值是否存在而返回true或false。count()返回某字符串的出現次數。從新實現練習4.1的main(),讓它調用這兩個函數。

Stack_2.h
#include <string>
#include <vector>

using namespace std;

class Stack
{
public:
    bool push(const string&);
    bool pop(string& elem);
    bool peek(string& elem);
    bool empty() const { return _stack.empty(); }
    bool full() const { return _stack.size() == _stack.max_size(); }
    int size() const { return _stack.size(); }
    bool find(const string& elem) const;
    int count(const string& elem) const;
private:
    vector<string> _stack;
};

Stack_2.cpp
#include "Stack_2.h"
#include <algorithm>

bool Stack::pop(string& elem)
{
    if (empty())    return false;
    elem = _stack.back();
    _stack.pop_back();
    return true;
}

bool Stack::peek(string& elem)
{
    if (empty())    return false;
    elem = _stack.back();
    return true;
}

bool Stack::push(const string& elem)
{
    if (full()) return false;
    _stack.push_back(elem);
    return true;
}

bool Stack::find(const string& elem) const
{
    vector<string>::const_iterator end_it = _stack.end();
    return ::find(_stack.begin(), end_it, elem) != end_it;
}

int Stack::count(const string& elem) const
{
    return ::count(_stack.begin(), _stack.end(), elem);
}

main.cpp
#include "Stack_2.h"
#include <iostream>

int main()
{
    Stack st;
    string str;
    while (cin >> str && !st.full())
    {
        st.push(str);
    }
    if (st.empty())
    {
        cout << '\n' << "Oops: no strings were read -- bailing out\n ";
        return 0;
    }
    st.peek(str);
    if (st.size() == 1 && str.empty())
    {
        cout << '\n' << "Oops: no strings were read -- bailing out\n ";
        return 0;
    }
    cout << '\n' << "Read in " << st.size() << " strings!\n";
    cin.clear();    //清除end-of-file的設定
    cout << "what word to search for? ";
    cin >> str;
    bool found = st.find(str);
    int count = found ? st.count(str) : 0;
    cout << str << (found ? " is " : "isn't ") << "in the stack.";
    if (found)
        cout << "It occurs " << count << " times\n";
}

練習4.3 考慮如下所定義的全局(global)數據:

string program_name;
string vector_stamp;
int version_number;
int tests_run;
int tests_passed;

編寫一個用以包裝這些數據的類。

#include <string>

using std::string;

class globalWrapper
{
public:
    static int tests_passed() { return _tests_passed; }
    static int tests_run() { return _tests_run; }
    static int version_number() { return _version_number; }
    static string version_stamp() { return _version_stamp; }
    static string program_name() { return _program_name; }

    static void tests_passed(int nval) { _tests_passed = nval; }
    static void tests_run(int nval) { _tests_run = nval; }

    static void version_stamp(const string&nstamp)
    {
        _version_stamp = nstamp;
    }

    static void version_number(int nval)
    {
        _version_number = nval;
    }

    static void program_name(const string& npn)
    {
        _program_name = npn;
    }

private:
    static string _program_name;
    static string _version_stamp;
    static int _version_number;
    static int _tests_run;
    static int _tests_passed;
};

string globalWrapper::_program_name;
string globalWrapper::_version_stamp;
int globalWrapper::_version_number;
int globalWrapper::_tests_run;
int globalWrapper::_tests_passed;

練習4.4 一份「用戶概況記錄(user profile)」內含如下數據:登錄記錄、實際姓名、登入次數、猜過次數、猜對次數、等級——包括初級、中級、高級、專家、以及猜對百分比(可實時計算得到,或將其值儲存起來備用)。請寫出一個名爲UserProfile的class,提供如下操做:輸入、輸出、相等測試、不等測試。其constructor必須可以處理默認的用戶等級、默認的登錄名稱(「guest」)。對於一樣的名爲guest的多個用戶,你如何保證每一個guest有他本身獨有的登錄會話(login session),不會和其餘人混淆?

UserProfile.h
#include <iostream>
#include <string>
#include <map>

using namespace std;

class UserProfile
{
public:
    enum uLevel
    {
        Beginner, Intermediate, Advanced, Guru
    };
    UserProfile(string login, uLevel = Beginner);
    UserProfile();

    //default memberwise initilaization和default memberwise copy已足夠所需,
    //沒必要另行設計copy constructor或copy assignment operator,
    //也不須要destructor

    bool operator==(const UserProfile&);
    bool operator!=(const UserProfile& rhs);

    //如下函數用來讀取數據
    string login()const { return _login; }
    string user_name() const { return _user_name; }
    int login_count() const { return _times_logged; }
    int guess_count() const { return _guesses; }
    int guess_correct() const{ return _correct_guesses; }
    double guess_average() const;
    string level() const;

    //如下函數用來寫入數據
    void reset_login(const string& val) { _login = val; }
    void user_name(const string& val) { _user_name = val; }

    void reset_level(const string&);
    void reset_level(uLevel newlevel) { _user_level = newlevel; }

    void reset_login_count(int val) { _times_logged = val;}
    void reset_guess_count(int val) { _guesses = val; }
    void reset_guess_correct(int val) { _correct_guesses = val; }

    void bump_login_count(int cnt = 1) { _times_logged += cnt; }
    void bump_guess_count(int cnt=1) { _guesses = cnt; }
    void bump_guess_correct(int cnt = 1) { _correct_guesses = cnt; }

private:
    string _login;
    string _user_name;
    int _times_logged;
    int _guesses;
    int _correct_guesses;
    uLevel _user_level;

    static map<string, uLevel> _level_map;
    static void init_level_map();
    static string guest_login();
};

inline double UserProfile::guess_average() const
{
    return _guesses
        ? double(_correct_guesses) / double(_guesses) * 100
        : 0.0;
}

inline UserProfile::UserProfile(string login,uLevel level)
    :_login(login),_user_level(level),
    _times_logged(1), _guesses(0), _correct_guesses(0) {}

#include <cstdlib>

inline UserProfile::UserProfile()
    : _login("guest"), _user_level(Beginner),
    _times_logged(1), _guesses(0), _correct_guesses(0)
{
    static int id = 0;
    char buffer[16];

    //_itoa()是C標準庫所提供的的函數,會將整數轉換爲對應的ASCII字符串形式
    _itoa(id++, buffer, 10);

    //針對guest,加入一個獨一無二的會話標識符(session id)
    _login += buffer;
}

inline bool UserProfile::
operator==(const UserProfile& rhs)
{
    if (_login == rhs._login && _user_name == rhs._user_name)
        return true;
    return false;
}

inline bool UserProfile::operator!=(const UserProfile& rhs)
{
    return !(*this == rhs);
}

inline string UserProfile::level() const
{
    static string _level_table[] = {
        "Beginner","Intermediate","Advanced","Guru"
    };
    return _level_table[_user_level];
}

//如下難度頗高,不過恰可做爲示範
map<string, UserProfile::uLevel> UserProfile::_level_map;

void UserProfile::init_level_map()
{
    _level_map["Beginner"] = Beginner;
    _level_map["Intermediate"] = Intermediate;
    _level_map["Advanced"] = Advanced;
    _level_map["Guru"] = Guru;
}

inline void UserProfile::reset_level(const string& level)
{
    map<string, uLevel>::iterator it;
    if (_level_map.empty())
        init_level_map();

    //確保level的確表明一個可識別的用戶等級
    _user_level = ((it = _level_map.find(level)) != _level_map.end()) ? it->second : Beginner;
}

main.cpp
#include "UserProfile.h"
#include <iostream>

ostream& operator <<(ostream& os, const UserProfile& rhs)
{
    //輸出格式,如:stanl Beginner 12 100 10 10%
    os << rhs.login() << ' '
        << rhs.level() << ' '
        << rhs.login_count() << ' '
        << rhs.guess_count() << ' '
        << rhs.guess_correct() << ' '
        << rhs.guess_average() << endl;
    return os;
}

istream& operator >>(istream& is, UserProfile& rhs)
{
    //是的,如下假設全部輸入都有效,不作錯誤檢驗
    string login, level;
    is >> login >> level;
    int lcount, gcount, gcorrect;
    is >> lcount >> gcount >> gcorrect;
    rhs.reset_login(login);
    rhs.reset_level(level);
    rhs.reset_login_count(lcount+10);
    rhs.reset_guess_count(gcount);
    rhs.reset_guess_correct(gcorrect);
    return is;
}

int main()
{
    UserProfile anon;
    cout << anon;    //測試output運算符
    UserProfile anon_too;    //看看咱們是否取得一份獨一無二的標識符
    cout << anon_too;

    UserProfile anna("Annal", UserProfile::Guru);
    cout << anna;
    anna.bump_guess_count(27);
    anna.bump_guess_correct(25);
    anna.bump_login_count();
    cout << anna;
    cin >> anon;    //測試input運算符
    cout << anon;
    return 0;
}

練習4.5 請實現一個4*4的Martix class,至少提供如下接口:矩陣加法、矩陣乘法、打印函數print()、複合運算符+=,以及一組支持下標操做(subscripting)的function call運算符,像下面這樣:

float& operator()(int row, int cloumn);
float operator()(int row, int cloumn) const;

請提供一個default constructor,可選擇地接受16個數據值。再提供一個constructor,可接受一個擁有16個元素的數組。你不須要爲此class 提供copy constructor,copy assignment operator、destructor。第六章從新實現Matrix class時纔會須要這幾個函數,用以支持任意行列的矩陣。

Matrix.h
#include <iostream>

using namespace std;

typedef float elemType;    //方便咱們轉爲template

class Matrix
{
    //friend聲明不受訪問權限的影響
    //我喜歡把它們放在class一開始處
    friend Matrix operator+(const Matrix&, const Matrix&);
    friend Matrix operator*(const Matrix&, const Matrix&);

public:
    Matrix(const elemType*);
    Matrix(const elemType = 0., const elemType = 0., const elemType = 0., const elemType = 0.,
        const elemType = 0., const elemType = 0., const elemType = 0., const elemType = 0.,
        const elemType = 0., const elemType = 0., const elemType = 0., const elemType = 0.,
        const elemType = 0., const elemType = 0., const elemType = 0., const elemType = 0.);

    //不須要爲Matrix提供copy constructor、destructor、
    //copy assignment operator

    //簡化「轉換至通用型矩陣(general Matrix)」的過程
    int rows() const { return 4; }
    int cols() const { return 4; }

    ostream& print(ostream&) const;
    void operator+=(const Matrix&);
    elemType operator()(int row, int column) const
    {
        return _Matrix[row][column];
    }
    elemType& operator()(int row, int column) 
    {
        return _Matrix[row][column];
    }

private:
    elemType _Matrix[4][4];
};

inline ostream& operator<<(ostream& os, const Matrix& m)
{
    return m.print(os);
}

Matrix operator+(const Matrix& m1, const  Matrix& m2)
{
    Matrix result(m1);
    result += m2;
    return result;
}

Matrix operator*(const Matrix& m1, const Matrix& m2)
{
    Matrix result;
    for (int ix = 0;ix < m1.rows();ix++)
    {
        for (int jx = 0;jx < m1.cols();jx++)
        {
            result(ix, jx) = 0;
            for (int kx = 0;kx < m1.cols();kx++)
            {
                result(ix, jx) += m1(ix, kx) * m2(kx, jx);
            }
        }
    }
    return result;
}

void Matrix::operator+=(const Matrix& m)
{
    for (int ix = 0;ix < 4;++ix)
    {
        for (int jx = 0;jx < 4;++jx)
        {
            _Matrix[ix][jx] += m._Matrix[ix][jx];
        }
    }
}

ostream& Matrix::print(ostream& os) const
{
    int cnt = 0;
    for (int ix = 0;ix < 4;++ix)
    {
        for (int jx = 0;jx < 4;++jx, ++cnt)
        {
            if (cnt && !(cnt % 8)) 
                os << endl;
            os << _Matrix[ix][jx] << ' ';
        }
    }
    os << endl;
    return os;
}

Matrix::Matrix(const elemType* array)
{
    int array_index = 0;
    for (int ix = 0;ix < 4;++ix)
    {
        for (int jx = 0;jx < 4;++jx)
            _Matrix[ix][jx] = array[array_index++];
    }
}

Matrix::Matrix(elemType a11, elemType a12, elemType a13, elemType a14,
    elemType a21, elemType a22, elemType a23, elemType a24,
    elemType a31, elemType a32, elemType a33, elemType a34,
    elemType a41, elemType a42, elemType a43, elemType a44)
{
    _Matrix[0][0] = a11;_Matrix[0][1] = a12;
    _Matrix[0][2] = a13;_Matrix[0][3] = a14;
    _Matrix[1][0] = a21;_Matrix[1][1] = a22;
    _Matrix[1][2] = a23;_Matrix[1][3] = a24;
    _Matrix[2][0] = a31;_Matrix[2][1] = a31;
    _Matrix[2][2] = a33;_Matrix[2][3] = a34;
    _Matrix[3][0] = a41;_Matrix[3][1] = a42;
    _Matrix[3][2] = a43;_Matrix[3][3] = a44;
}

main.cpp
#include "Matrix.h"

int main()
{
    Matrix m;
    cout << m << endl;
    elemType ar[16] = {
        1.,0.,0.,0.,0.,1.,0.,0.,
        0.,0.,1.,0.,0.,0.,0.,1.,
    };
    Matrix identity(ar);
    cout << identity << endl;

    Matrix m2(identity);
    m = identity;
    cout << m2 << endl;
    cout << m << endl;

    elemType ar2[16] = {
        1.3,0.4,2.6,8.2,6.2,1.7,1.3,8.3,
        4.2,7.4,2.7,1.9,6.3,8.1,5.6,6.6
    };
    Matrix m3(ar2);
    cout << m3 << endl;
    Matrix m4 = m3 * identity;
    cout << m4 << endl;
    Matrix m5 = m3 + m4;
    cout << m5 << endl;
    m3 += m4;
    cout << m3 << endl;
    return 0;
}
end。

「沒有一個冬天不可逾越,沒有一個春天不會來臨。」

相關文章
相關標籤/搜索