Υπερφόρτωση (Overloading)

Γενικά

Με την Υπερφόρτωση μπορούμε να έχουμε δύο μεθόδους με το ίδιο όνομα αλλά διαφορετικό αριθμό ή/και τύπο παραμέτρων.

Αυτό μπορεί να γίνει στην ίδια κλάση. Κλασικό παράδειγμα η υπερφόρτωση των δημιουργών.

Παράδειγμα overloading με δημιουργούς

class Circle {
    private:
        int radius;
        int x;
        int y;
    public:

        Circle() {
            this->x = 10;
            this->y = 10;
            this->radius = 20;
        }

        Circle(int radius) {
            this->radius = radius;
        }

        Circle(int radius, int x, int y) {
            this->radius = radius;
            this->x = x;
            this->y = y;
        }
};

Παράδειγμα overloading με μέθοδο.

class Graphics {

    public:
        void draw(string text) {
            // draws the text
        }

        void draw(Circle circle) {
            // draws the circle
        }

        void draw(Rectangle rect) {
            // draws the rectangle
        }
};