《c++入門經典》筆記9

《c++入門經典》筆記9

第九章 高級類

9.1 const成員函數

若是使用關鍵字const將成員函數聲明爲常量函數(常函數),則代表它不會修改任何類成員的值。要將函數聲明爲常量函數,可在括號後面添加關鍵字const:ios

void displayPage() const;

若是將函數聲明爲常函數,但其實現修改了成員,編譯器就會報錯。c++

對於那些不用來修改類成員的函數,應儘量將其聲明爲常函數,這是一種良好的編程習慣。這樣可以讓編譯器發現您對成員變量的無心間修改,避免在運行階段出現這些錯誤。程序員

9.2接口和實現

編程

9.3組織類聲明和函數定義

在c++的源代碼中,類的定義和實現一般是分開的。您在類中聲明的每一個函數都必須有定義。與常規函數同樣,類函數也包含函數頭與函數體。函數

定義必須放在編譯器可以找到的文件中,大多數c++編譯器都要求這種文件的擴展名爲.cppcode

雖然能夠將聲明放在源代碼文件中,可是大多數程序員採起的作法是,將聲明放在頭文件中,其文件名與源代碼文件相同,但擴展名爲.hpp(或是不那麼常見的.h或.hp)blog

所以,若是您將Tricycle類的聲明放在文件Tricycle.hpp中,那麼類函數的定義位於文件Tricycle.cpp中。經過使用預處理編譯指令,可在.cpp文件中包含頭文件。接口

#include "Tricycle.hpp"

將他們分開的緣由是,類的客戶不關心實現細節,他們須要知道的信息都包含在頭文件中。get

9.4內聯實現

可將常規函數聲明爲內聯,一樣,也可將成員函數聲明爲內聯的,爲此,須要在返回類型前面指定關鍵字inline,以下所示:編譯器

inline int Tricycle::getSpeed()
{
 return speed;
}

也可將函數定義放在類聲明中(也就是寫在裏面),這樣函數將自動變成內聯的,以下所示:

class Tricycle
{
public:
 int getSpeed() const 
 {
     return speed;
 }
 void setSpeed(int newSpeed);
};

程序清單9.1與9.2實現了Tricycle類的聲明與實現的分離:

程序清單9.1 Tricycle.hpp

#include <iostream>

class Tricycle
{
public:
    Tricycle(int initialAge);
    ~Tricycle();
    int getSpeed() const { return speed; }
    void setSpeed(int speed);
    void pedal()
    {
        setSpeed(speed + 1);
        std::cout << "\nPedaling;tricycle speed " << speed << " mph\n";
    }
    void brake()
    {
        setSpeed(speed - 1);
        std::cout << "\nBraking;tricycle speed " << speed << " mph\n";
    }

private:
    int speed;
};

程序清單9.2 Tricycle.cpp

#include "Tricycle.hpp"

Tricycle::Tricycle(int initialAge)
{
    setSpeed(initialAge);
}
Tricycle::~Tricycle()
{
    std::cout << "Destructed\n";
}
void Tricycle::setSpeed(int newSpeed)
{
    if (newSpeed >= 0)
    {
        speed = newSpeed;
    }
}

int main()
{
    Tricycle wichita(5);
    wichita.pedal();
    wichita.pedal();
    wichita.brake();
    wichita.brake();
    wichita.brake();
    return 0;
}

能夠看到運行效果與筆記8 程序清單8.2是同樣的。

9.5將其餘類用做成員數據的類

建立複雜類時,常常將簡單類做爲其成員

程序清單9.3與9.4爲例:(其實書上給的這個例子裏面太多的初始化方法,俺表示無語)

程序清單9.3 Rectangle.hpp

#include <iostream>

class Point
{
public:
    void setX(int newX) { x = newX; }
    void setY(int newY) { y = newY; }
    int getX() const { return x; }
    int getY() const { return y; }

private:
    int x;
    int y;
};

class Rectangle
{
public:
Rectangle(int newTop,int newLeft,int newBottom,int newRight);
~Rectangle(){std::cout << "Destructed\n";}

int getTop() const {return top;}
int getLeft() const {return left;}
int getBottom() const {return bottom;}
int getRight() const {return right;}

Point getUpperLeft() const {return upperLeft;}
Point getLowerLeft() const {return lowerLeft;}
Point getUpperRight() const {return upperRight;}
Point getLowerRight() const {return lowerRight;}

void setUpperLeft(Point location);
void setLowerLeft(Point location);
void setUpperRight(Point location);
void setLowerRight(Point location);

void setTop(int newTop);
void setLeft(int newLeft);
void setBottom(int newBottom);
void setRight(int newRight);

int getArea() const;

private:
Point upperLeft;
Point upperRight;
Point lowerLeft;
Point lowerRight;
int top;
int left;
int bottom;
int right;
};

程序清單9.4 Rectangle.cpp

#include "Rectangle.hpp"

Rectangle::Rectangle(int newTop,int newLeft,int newBottom,int newRight)
{
    top=newTop;
    left=newLeft;
    bottom=newBottom;
    right=newRight;

    upperLeft.setX(left);
    upperLeft.setY(top);

    upperRight.setX(right);
    upperRight.setY(top);

    lowerLeft.setX(left);
    lowerLeft.setY(bottom);

    lowerRight.setX(right);
    lowerRight.setY(bottom);
}

void Rectangle::setUpperLeft(Point location)
{
    upperLeft = location;
    upperRight.setY(location.getY());
    lowerLeft.setX(location.getX());
    top = location.getY();
    left = location.getX();
}

void Rectangle::setLowerLeft(Point location)
{
    lowerLeft=location;
    lowerRight.setY(location.getY());
    upperLeft.setX(location.getX());
    bottom = location.getY();
    left = location.getX();
}

void Rectangle::setLowerRight(Point location)
{
    lowerRight=location;
    lowerLeft.setY(location.getY());
    upperRight.setX(location.getX());
    bottom = location.getY();
    right = location.getX();
}

void Rectangle::setUpperRight(Point location)
{
    upperRight = location;
    upperLeft.setY(location.getY());
    lowerRight.setX(location.getX());
    top = location.getY();
    right = location.getX();
}

void Rectangle::setTop(int newTop)
{
    top = newTop;
    upperLeft.setY(top);
    upperRight.setY(top);
}

void Rectangle::setLeft(int newLeft)
{
    left = newLeft;
    upperLeft.setX(left);
    lowerLeft.setX(left);
}

void Rectangle::setBottom(int newBottom)
{
    bottom = newBottom;
    lowerLeft.setY(bottom);
    lowerRight.setY(bottom);
}

void Rectangle::setRight(int newRight)
{
    right=newRight;
    upperRight.setX(right);
    lowerRight.setX(right);
}

int Rectangle::getArea() const
{
    int width = right - left;
    int height = top - bottom;
    return (width*height);
}

int main()
{
    Rectangle myRectangle(100,20,50,80);
    int area = myRectangle.getArea();
    std::cout<<"Area: "<<area<<std::endl;
    std::cout<<"Upper Left X Coordinate: ";
    std::cout<<myRectangle.getUpperLeft().getX()<<std::endl;
}

相關文章
相關標籤/搜索