syntax help!

Hello Guys!, I need help undestanding the next line from the code below.

Rectangle( int a = 0, int b = 0):Shape(a, b) { }

How can i rewrite it so make it clear to me??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Shape {
   protected:
      int width, height;
		
   public:
      Shape( int a = 0, int b = 0) {
         width = a;
         height = b;
      }
		
      int area() {
         cout << "Parent class area :" <<endl;
         return 0;
      }
};

class Rectangle: public Shape {
   public:
      Rectangle( int a = 0, int b = 0):Shape(a, b) { }
      int area () { 
         cout << "Rectangle class area :" <<endl;
         return (width * height); 
      }
};
Last edited on
Because width and height are protected, you can rewrite Rectangle's constructor to match Shape's constructor.
1
2
3
4
     Rectangle (int a = 0, int b = 0) 
    {   width = a;
         height = b;
    }


However, the form given on line 19 is preferred.



Thanks.

so ..

Shape(a, b) { } means -> Use overloaded constructor from Shape??
Rectangle is derived from Shape. So any time you construct a Rectangle object, the program must first construct the Shape base object. Shape(a,b) {} tells the compiler to construct the base Shape object by calling Shape(a,b) on it. Once that's done, it will run what's inside the braces of the Rectangle object's constructor. In this case, { } means that there is no code to run.

Topic archived. No new replies allowed.