Sams Teach Yourself C++ in 21 Days

(singke) #1
The Rectangleclass, demonstrated in Listing 10.1, has two DrawShape()functions.
One, which takes no parameters, draws the rectangle based on the class’s current values.
The other takes two values, width and length, and draws the rectangle based on those
values, ignoring the current class values.

LISTING10.1 Overloading Member Functions
1: //Listing 10.1 Overloading class member functions
2: #include <iostream>
3:
4: // Rectangle class declaration
5: class Rectangle
6: {
7: public:
8: // constructors
9: Rectangle(int width, int height);
10: ~Rectangle(){}
11:
12: // overloaded class function DrawShape
13: void DrawShape() const;
14: void DrawShape(int aWidth, int aHeight) const;
15:
16: private:
17: int itsWidth;
18: int itsHeight;
19: };
20:
21: //Constructor implementation
22: Rectangle::Rectangle(int width, int height)
23: {
24: itsWidth = width;
25: itsHeight = height;
26: }
27:
28:
29: // Overloaded DrawShape - takes no values
30: // Draws based on current class member values
31: void Rectangle::DrawShape() const
32: {
33: DrawShape( itsWidth, itsHeight);
34: }
35:
36:
37: // overloaded DrawShape - takes two values
38: // draws shape based on the parameters
39: void Rectangle::DrawShape(int width, int height) const
40: {
41: for (int i = 0; i<height; i++)
42: {

290 Day 10

Free download pdf