1

This question is about OOP design. What are the advantages/disadvantages of passing all arguments to class constructor vs passing arguments to member functions ?

In my case I know all arguments in the beginning of the program and I don't need to change them until the program is over.

In C++ the situation would be something like that (although in my code I need to parse more arguments and the member functions are more complex):

// All arguments in class constructor
Rectangle::Rectangle(float base, float height, string rectColor){
    this->area = 0;
    this->base = base;
    this->height = height;
    this->rectColor = rectColor;
}

void Rectangle::calcArea(){
    area = base * height;
}

void Rectangle::paintRectangle(){
    // use area
    // whatever
}

vs

// Arguments in member functions
Rectangle::Rectancle(){
    this->area = 0;
}

void Rectangle::calcArea(float base, float height){
    area = base * height;
}

void Rectangle::paintRectangle(string rectColor){ 
    // use area
    // whatever
}

One strategy that I'm using is: If I need the variable in multiple member functions I make it a class variable. Is that good or the best approach ?

A bad thing about passing everything into constructor is that it would have lots of arguments. And also I wouldn't need to call the class member functions in my main.

Please explain the main principles I should follow.

1
  • Honestly, the second proposition is not bad..but this is not oop anymore. Sep 21, 2016 at 14:44

2 Answers 2

0

Benefits for putting arguments in the constructor:

  • The instance is more completely initialized (no problems with the order of functions to be called to get a 'complete'useful instance.

Benefits for putting arguments to specific functions:

  • More flexibility, since the functions use variables instead of 'constants' passed to the constructor
  • The arguments passed to functions tend to belong better to the functions (e.g. for paintRectangle it is logical to pass the color, but if it never change, why making the flexibility to change the color afterwards?)

To prevent too many arguments in the cnstructor

  • Create a structure to pass the variables
  • Create a sub class ... if you need more than 5-7 parameters, possibly the responsibility of the class is too big.
  • Use named arguments (more clearer, but still the same amount of arguments)

In general, make classes as limited as possible, do not make them more flexible than needed UNLESS you know beforehand the functionality is needed at a later stage.

0

The one of the most important thing about constructors is that they make your OOP code consistent. For any object, it is good approach, if you already know, that someone created with properties need to have to exist.

i.e. Rectangle cannot exist without "height".

Therefore the "minimum parameters constructor" is great advantage. (the minimum required parameters need to have for object, to be usable and for not able to crash, when computing i.e. area)

If you have more parameters, that they are not neccesary, it is good to create more constructors based on what is probably "often use" of your object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.