Question

In object oriented programming, is there differences between using constructor or using setValues like method for that. And shouldn't I use setValues method?

class Controller{
public:
    Controller();
    Controller(int,int);
    setControllerValues(int,int);
private:
    int transactionValue;
    int transactionId;
};

No correct solution

OTHER TIPS

You should always use a member initializer list to initialize your class members.

Note that I say initialize, which means providing a value at the time of construction. Unless you do so these members have an Indeterminate value until you explicitly call the set method to explicitly set the member with a particular value.

Is there differences between using constructor or using setValues like method for that.

Yes there is a difference. C++ provides you a opportunity to initialize class members, this is through the member intializer list. If you do not initialize members in member initializer list the members remain uninitialized i.e they have Indeterminate values. One would never want the state of members to be indeterminate.

When you use a set method, you are actually assigning and not initializing the member. There is a difference between the two:

Cost Member Initialization = Object Construction 
Cost of Member Assignment = Object Construction + Assignment

It's a very broad question and the answer will vary with context. You always want to initialise your values on construction and it looks like you want to provide sensible defaults, you can do this with one constructor. This will save you constructing an object and subsequently setting the values.

If you want to set your values later during the life of an object, you will need "setters". You may prefer separate "setters" to one combined setter, to tweak these individually.

Here is some example code:

class Controller{
public:
    Controller(int value = SOME_SENSIBLE_DEFAULT,
               int id = SOME_OTHER_SENSIBLE_DEFAULT)
        :transactionValue(value)
        ,transactionId(id)
    {
    }

    void set_transaction_value(int value)
    {
        transactionValue = value;
    }

    void set_transaction_id(int id)
    {
        transactionid = id;
    }

private:
    int transactionValue;
    int transactionId;
};

A constructor is a method in the class which gets executed when its object is created. Usually, we put the initialization code in the constructor. Writing a constructor in the class is damn simple, have a look at the following sample:

public class Test
{
    public Test()
    {
        // Initialize code in the constructor.
    }
}
  • Constructors don't return any value.
  • References and pointers cannot be used on constructors and destructors because their addresses cannot be taken.
  • Constructors cannot be declared with the keyword virtual.
  • If the constructor is defined as private, we can not create instance of that class.

Method:

  • In methods we can define variables.These variables scope is within methods only.
  • If you declare a public variable, it will be accessed in all methods.
  • Methods may/may not contain return type.
  • Methods could be inherited in derived class (note that method should be public).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top