dav
dav

Reputation: 936

Class constructor without a body or member initialization?

Reading through The C++ Programming Language, 4th Edition, there's a class Circle inheriting from Shape like so

class Circle : public Shape {
public:
    Circle(Point p, int rr); // constructor
    Point center() const { return x; }

    void move(Point to) { x=to; }
    void draw() const;
    void rotate(int) {} // nice simple algorithm
private:
    Point x; // center
    int r; // radius
};

and the Shape class

class Shape {
public:
    virtual Point center() const =0; // pure virtual
    virtual void move(Point to) =0;

    virtual void draw() const = 0; // draw on current "Canvas"
    virtual void rotate(int angle) = 0;

    virtual ˜Shape() {} // destructor
// ...
};

The part confusing me is Circle's constructor:

Circle(Point p, int rr);

Where is the return type? In all previous constructors the return type was specified void. I couldn't find a C++ implicit return type (like C's implicit-int rule)

Where is the body/initialization? All previous constructors either initialized via member-list initialization (:) or in the function body {}. I see neither here and so am wondering how the values are initialized at all.

Upvotes: 0

Views: 1712

Answers (1)

Steve
Steve

Reputation: 1757

Constructors don't specify a return type, and don't return anything.

And it doesn't have a body because it's only the declaration - the definition will be somewhere else, and will look like

Circle::Circle (Point p, int rr)
{
 ...
}

Upvotes: 4

Related Questions