Overloaded Member Functions - C++ Class

C++ examples for Class:Member Function

Description

Overloaded Member Functions

Demo Code

                                                   
#include <iostream> 
                                                    
class Rectangle //from  ww w  .  j  av a2s  .  c  o  m
{ 
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 Tutorials