Notes on c++ Getting Started classic 9

Notes on c++ Getting Started classic 9

Chapter IX advanced class

9.1 const member function

If a member function is declared as a constant function (constant function) with the keyword const, it does not modify the value of any class member. To declare a function as a constant function, add the keyword const:

void displayPage() const;

If a function is declared as a constant function, but its implementation modifies its members, the compiler will report an error.

For those functions that do not need to modify class members, they should be declared as constant functions as far as possible, which is a good programming habit. This allows the compiler to detect unintentional modifications to member variables and avoid these errors at run time.

9.2 interface and Implementation

slightly

9.3 organization class declaration and function definition

In c++ source code, the definition and implementation of classes are usually separated. Every function you declare in a class must have a definition. Like regular functions, class functions also contain function headers and function bodies.

The definition must be placed in a file that the compiler can find. Most c++ compilers require this file to have an extension of cpp

Although you can put the declaration in the source code file, most programmers take the practice of putting the declaration in the header file with the same file name as the source code file, but the extension is hpp (or less common.h or.hp)

Therefore, if you place the declaration of the Tricycle class in the file Tricycle In HPP, the definition of class functions is located in the file Tricycle CPP. By using preprocessing compilation instructions, you can The cpp file contains header files.

#include "Tricycle.hpp"

The reason for separating them is that the customers of the class do not care about the implementation details, and the information they need to know is contained in the header file.

9.4 inline implementation

Regular functions can be declared as inline, and member functions can also be declared as inline. To do this, you need to specify the keyword inline in front of the return type, as shown below:

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

You can also put the function definition in the class declaration (that is, write it in it), so that the function will automatically become inline, as shown below:

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

Program listings 9.1 and 9.2 implement the separation of the declaration and implementation of the Tricycle class:

Procedure listing 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;
};

Procedure listing 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;
}

You can see that the running effect is the same as that in note 8 program listing 8.2.

9.5 classes that use other classes as member data

When creating complex classes, simple classes are often used as members

Program listings 9.3 and 9.4 are examples: (in fact, there are too many initialization methods in the example given in the book. I said nothing.)

Procedure listing 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;
};

Procedure listing 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;
}

Posted by johno on Tue, 31 May 2022 04:41:16 +0530