Cpp - Member Functions Overloading

Introduction

Member functions can be overloaded.

Rectangle class has two drawShape() functions.

One takes no parameters and draws the Rectangle based on the object's current values.

The other takes two values, width and length, and draws a rectangle using those values, ignoring the current values.

Demo

#include <iostream> 
 
class Rectangle /*from   w w  w .  j  a v a2s .c  om*/
{ 
public: 
    Rectangle(int width, int height); 
    ~Rectangle() {} 
 
    void drawShape() const; 
    void drawShape(int width, int height) const; 
 
private: 
    int width; 
    int height; 
}; 
 
Rectangle::Rectangle(int newWidth, int newHeight) 
{ 
    width = newWidth; 
    height = newHeight; 
} 
 
void Rectangle::drawShape() const 
{ 
    drawShape(width, height); 
} 
 
void Rectangle::drawShape(int width, int height) const 
{ 
    for (int i = 0; i < height; i++) 
    { 
            for (int j = 0; j < width; j++) 
                 std::cout << "*"; 
            std::cout << std::endl; 
    } 
} 
 
int main() 
{ 
    Rectangle box(30, 5); 
    std::cout << "drawShape():" << std::endl; 
    box.drawShape(); 
    std::cout << "\ndrawShape(40, 2):" << std::endl; 
    box.drawShape(40, 2); 
    return 0; 
}

Result

Related Topic