BK Perera
BK Perera

Reputation: 9

C++ creating more than one constructor

Is it possible to have more than one parameterized constructors in c++ and is it possible to have destructors with parameters

Upvotes: 0

Views: 374

Answers (1)

VolAnd
VolAnd

Reputation: 6407

Is it possible to have more than one parameterized constructors in c++?

Yes.

E.g.:

class square
{
    int m_top;
    int m_left;
    int m_right;
    int m_bottom;
public:
    // Constructor for case when all data is provided
    square(int top, int left, int right, int bottom) : m_top(top), m_left(left), m_right(right), m_bottom(bottom)
    {
    }
    // Constructor for case when some data is missing 
    square(int top, int left) : m_top(top), m_left(left), m_right(top+1), m_bottom(left+1)
    {
    }
    // ... other members (like default constructor, getters, setters, etc.)
};

and is it possible to have destructors with parameters?

No.

You can find some options about destrtuctor in C++ reference, but name and signature (parameters) not in the options.

Upvotes: 4

Related Questions