classes + constructors

#include<iostream>
using namespace std;

class polygon{
protected:
int x,y;
public:
polygon(int a,int b)
{

x=a; y=b;
}
};

class rectangle:public polygon{
public:
int area () {return (x * y);}

};

class triangle: public polygon{
public:
int area() {return ((1.0/2) * x * y);}

};



int main()
{
rectangle rect1(5,5);
triangle tri1(3,5);

cout<<"the area of rectangle is : "<<rect1.area()<<endl;
cout<<"the area of triangle is: "<<tri1.area()<<endl;
return 0;

}

Why isn't this working? :(
Isn't the constructor inherited by derived classes ??
Isn't the constructor inherited by derived classes ??


Not in the way you're thinking, no.

You have to give rectangle and triangle a constructor that takes parameters, then explicitly call the parent constructor:

1
2
3
4
5
6
7
8
9
class rectangle : public polygon
{
public:
    rectangle(int a, int b)  // <- give rectangle a constructor
        : polygon( a, b )  // <- have it call its parent's constructor
    {
    }

//... 
The simplest way to make your code to work is to add only one line in the definitions of derived classes

class rectangle:public polygon{
public:
int area () {return (x * y);}
using polygon:polygon;
};

class triangle: public polygon{
public:
int area() {return ((1.0/2) * x * y);}
using polygon:polygon;

};


But not all compilers support this feature.:)

Last edited on
Isn't the constructor inherited by derived classes ??
No
But you can do something like: triangle(int x, int y) :polygon(x, y) {}
@MiiNiPaa
Isn't the constructor inherited by derived classes ??

No


Yes, they are inherited. Otherwise this code would not be compiled.:)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Base
{
protected:
   Base() = default;
};
 
class Derived: public Base
{
};
 
int main()
{
    Derived d;
}
Last edited on
Yes, they are inherited
No, they don't, in sense of using it as their constructor. Derived classes do inherit constructors, but they use them only to construct base class part of itself. If isn't specified otherwise (which I did in my snippet) derived class constructors will use default base class constructor.

Default constructor and copy constructor are created by compiler if not specified. In your code compiler-reated default constructor of derived class called, which calls default constructor of base class. Which defined as default i.e. compiler generated.
Last edited on

@MiiNiPaa
No, they don't, in sense of using it as their constructor.


I never said that base class constructors are used as constructors of derived classes. And nobody here said that. So it is only your fantasies you are trying to argue with yourself.:)


@MiiNiPaa
In your code compiler-reated default constructor of derived class called, which calls default constructor of base class. Which defined as default i.e. compiler generated.


In my example the derived class constructor calls protected member of the base class. If it would not be inherited the derived class could not call it.:)

By the way the same way the base class copy assignment operator also is inherited by a derived class. For example

1
2
3
4
5
Derived & operator = ( const Derived &d )
{
   Base::operator =( d );
   return ( *this );
}


But nobody are saying that the base class copy assignment operator is used instead of the derived copy assignment operator.:)
Last edited on
Topic archived. No new replies allowed.