Reputation: 1
What's the difference between getter setter function and constructor? I think since both getter setter and constructor objectives are to initialize private member variable in a class. I'm kind of confused because they both are doing this same thing. Is there a particular situation where each it used, or can they be used interchangeably?
Upvotes: 0
Views: 6004
Reputation:
Constructors initializes the instance you're creating.
Destructor gets called when an object (instance of a class) gets deleted.
Getters are used to get the value of properties.
Setters are used to set the values for properties.
Example *ex = new Example(); // Constructor is called
char *ch = ex->getSomeProperty(); // Getter is called
ex->setSomeProperty("parameter"); // Setter is called with a passed argument
delete ex; // Destructor is called
Upvotes: 0
Reputation: 10726
Wikipedia answers your question about getters
and setters
quite nicely:
In computer science, a mutator method is a method used to control changes to a variable. They are also widely known as setter methods. Often a setter is accompanied by a getter (also known as an accessor), which returns the value of the private member variable.
The mutator method is most often used in object-oriented programming, in keeping with the principle of encapsulation. According to this principle, member variables of a class are made private to hide and protect them from other code, and can only be modified by a public member function (the mutator method), which takes the desired new value as a parameter, optionally validates it, and modifies the private member variable.
And, comparing that to the definition of a constructor:
In object-oriented programming, a constructor (sometimes shortened to ctor) in a class is a special type of subroutine called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set member variables required.
A constructor resembles an instance method, but it differs from a method in that it has no explicit return type, it is not implicitly inherited and it usually has different rules for scope modifiers. Constructors often have the same name as the declaring class. They have the task of initializing the object's data members and of establishing the invariant of the class, failing if the invariant is invalid. A properly written constructor leaves the resulting object in a valid state. Immutable objects must be initialized in a constructor.
So, using these definitions we can infer the following:
Upvotes: 0
Reputation: 1760
Whereas a constructor is used to initialize instance variables, i.e. to give them their first (initial) value, setter methods serve to change them later on. Getters are used to retrieve the current value they possess.
Upvotes: 2
Reputation: 4291
An instance's constructor is only called once. Getters/setters can be called at any point in the lifespan of the object. Constructors are used to initialize an object, while setters are used to maintain object state for its more dynamic members (and getters would be used to access that state).
Upvotes: 0