Shiklo Tools
PDF40
Image20
Calculators & Converters25
Text & Writing13
Developer26
Video14
Audio14
Document14
Data & Spreadsheet12
Web & SEO9
OCR & Scanning5
Archive & File12
Security & Privacy23
Health & Medical29
QR & Barcode5

C++ Class Methods

Functions that belong to a class are called member functions or methods. They work on the object they are called on, and they can use its private data.

Inside or outside the class

A method can be defined inside the class, or only declared there and defined outside with the class name and :: in front. Defining outside keeps the class definition short, which helps when a class has many methods; in larger programs the class goes into a header and the definitions into a .cpp file.

#include <iostream>

class Rectangle {
    double width = 0, height = 0;
public:
    void setSize(double w, double h);
    double area() const { return width * height; }   // defined inside
    double perimeter() const;
};

void Rectangle::setSize(double w, double h) {         // defined outside
    if (w >= 0 && h >= 0) {
        width = w;
        height = h;
    }
}

double Rectangle::perimeter() const {
    return 2 * (width + height);
}

int main() {
    Rectangle r;
    r.setSize(4, 3);
    std::cout << "area " << r.area() << ", perimeter " << r.perimeter() << "\n";
    return 0;
}
Output
area 12, perimeter 14

const methods

A method marked const after its parameter list promises not to change the object. Only const methods can be called on a const object or through a const&, so mark every method that only reads as const. Forgetting it shows up as an error when someone passes your object by const&.

The this pointer

Inside a method, this is a pointer to the object the method was called on. It is used when a parameter has the same name as a member, and to return the object itself so calls can be chained:

#include <iostream>
#include <string>

class Message {
    std::string text;
public:
    Message& add(const std::string& text) {
        this->text += text;     // this->text is the member, text the parameter
        return *this;           // return the object itself
    }
    void show() const { std::cout << text << "\n"; }
};

int main() {
    Message m;
    m.add("Hello").add(", ").add("World");
    m.show();
    return 0;
}
Output
Hello, World

Methods using methods

Methods can call each other directly, without an object in front: inside the class, a call like area() means this->area().

#include <iostream>

class Circle {
    double r;
public:
    explicit Circle(double radius) : r(radius) {}
    double area() const { return 3.14159 * r * r; }
    bool biggerThan(const Circle& other) const { return area() > other.area(); }
};

int main() {
    Circle a(2), b(3);
    std::cout << std::boolalpha << a.biggerThan(b) << " " << b.biggerThan(a) << "\n";
    return 0;
}
Output
false true

The Circle(double radius) line is a constructor, the topic of the next page.

Practice exercises

  1. Add a method scale(double factor) to Rectangle that multiplies both sides.
  2. Add bool isSquare() const and test it.
  3. Make setSize return *this so that r.setSize(2, 2).scale(3) works (change scale too).