Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

abstract classes and constructor in C++

I read this question "C++ Abstract Class: constructor yes or no?" and the answers belonging to it.

But according to answers, I understand that we need the constructor to initialize it data members, however I can use its member functions like setter functions in my derived class to initialize the data members, so Why is it important to define a constructor?

like image 519
Mahmoud Emam Avatar asked Aug 23 '26 06:08

Mahmoud Emam


1 Answers

The default constructor definition and the member initializations make the class self contained regarding proper setup conditions (valid state).
Usage of setter methods to manipulate the class instance is optional for class clients (including inheriting classes).

You may consider adding more constructor signatures, that the clients can use to inititialze the class members with a single call, and don't require these applying additional setter calls.

It depends on the particular use case, what's more convenient and semantically correct in the end.

like image 171
πάντα ῥεῖ Avatar answered Aug 25 '26 19:08

πάντα ῥεῖ