0
class Triangle: public Shape{
      public:
             Triangle(int a=0, int b=0):Shape(a,b){}
             int area()
             {
                 cout << "in triangle class";
                 return width*height/2;
             }
};

in the above code, wt is the meaning of the line

Triangle(int a=0, int b=0):Shape(a,b){}
tshepang
  • 12,111
  • 21
  • 91
  • 136
user3610534
  • 61
  • 1
  • 1
  • 2
  • I'm sure this is a duplicate of something, but you can't search for ":" in the finder :) – M.M May 07 '14 at 03:26
  • +1 (-1 plus +1 -> 0) For asking a question that others may thinks is dumb or is duplicated, but, deserves to be answered – umlcat May 12 '14 at 16:46

4 Answers4

1

If Derived class inherits Base class, then when constructing a Derived object, the constructor of Derived class must call the constructor of Base class. The Destructors of these classes will be called in reversed order. In Triangle(int a=0, int b=0):Shape(a,b){}, the Triangle constructor is calling Shape constructor passing the required arguments. There is no casting involved. Check this and this.

Community
  • 1
  • 1
Rakib
  • 7,435
  • 7
  • 29
  • 45
0

Nothing is being cast here. The Triangle constructor is invoking the Shape base class' constructor with the parameter list, (a,b).

John Dibling
  • 99,718
  • 31
  • 186
  • 324
0

There is no cast here. This is a constructor initialization list.

When you construct a Triangle, since Shape is a base class, it must construct a Shape first. The list Shape(a,b) says that a,b are the arguments given to the constructor for this Shape.

M.M
  • 138,810
  • 21
  • 208
  • 365
0

Your code:

class Triangle: public Shape{
      public:
             Triangle(int a=0, int b=0):Shape(a,b)
             {
               // may do something
             }

             int area()
             {
                 cout << "in triangle class";
                 return width*height/2;
             }
};

How does internally work:

class Triangle: public Shape{
          public:
                     Triangle(int a=0, int b=0):
                 {
                   // always before the others instructions
                   base(a,b);

                   // may do something
                 }

                 int area()
                 {
                     cout << "in triangle class";
                     return width*height/2;
                 }
    };

There are several ways to work with constructors:

(1) Declare a new class with a new constructor

class NewConstructorClass: public SomeBaseClass // dont care about parent class constructor
{
   NewConstructorClass {
     // do something new instruction 1
     // do something new instruction 2
     // do something new instruction 3
   }

  public:
    void DoAnything()
    {
      // ...
    }
};

(2) Declare a new class with a new and empty, constructor. Some developers do this, in order, to add code to the constructor, later, or, explicitly, add an empty constructor, as a "best practice", to indicate that there should be a constructor.

class NewConstructorClass: public SomeBaseClass // dont care about parent class constructor
{
   NewConstructorClass {
     // does nothing, on purpouse
   }

  public:
    void DoAnything()
    {
      // ...
    }
};

(3) Declare a new class without a new constructor. The compilers adds an automatic constructor that does nothing.

class NewConstructorClass: public SomeBaseClass // dont care about parent class constructor
{
   NewConstructorClass {
     // do something new
   }

  public:
    void DoAnything()
    {
      // ...
    }
};

(4) Declare a new class with a new constructor, that calls the base constructor. Like your scenario:

class NewConstructorClass: public SomeBaseClass // dont care about parent class constructor
{
   NewConstructorClass: base() {
     // do something new
   }

  public:
    void DoAnything()
    {
      // ...
    }
};

Each case, depends, on what you want to achieve. Some of this cases, may be consider "Bad practices", others "good practices", depending on coding styles.

Cheers.

umlcat
  • 4,091
  • 3
  • 19
  • 29