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