The whole purpose behind using get's and sets is data hiding. So people can't directly manipulate your variables from other classes.
Realistically sets aren't needed so much as the constructor can set the variable by use of parameters. Making the variable private, and simply having a get method for classes not of this type will allow access.
1 2 3 4 5 6 7 8 9 10 11 12
|
// myclass.h
#include<iostream>
class myclass
{
public:
myclass(int theValue);
int getValue();
private:
int value;
};
|
1 2 3 4 5 6 7 8 9 10 11 12 13
|
// myclass.cpp
#include "myclass.h"
myclass::myclass(int theValue)
{
value = theValue; // this sets the private int value
}
int myclass::getValue()
{
return value; // this is the private int value that we can access via the method.
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
// main
#include "myclass.h"
using std::cout;
int main()
{
// create an object of the class
myclass mc(653);
// use the objects method
cout << mc.getValue() << endl;
return 0;
}
|
edit: TBH, I don't understand this much as if you create an object of a class then you have access to all it's members regardless, having sets and gets simply makes it slightly more difficult(slightly) to mess with the variable.... It's to do with good OO design apparently.
this is a decent link:
http://www.kirit.com/C%2B%2B%20killed%20the%20get%20&%20set%20accessors
The only thing I could find in google was about java:
Did some research and found the following:
accessor methods
Normally you don't make variables in your class public. Instead you provide public set and get
methods for the variable. This lets you then later change the way the variable is stored, or add
extra validation checks.
Method names made up of a member name with a get or set prefix are methods that are
required when the members are private and therefore cannot be accessed from outside the
class (which is good programming I understand). Any program that creates instances of a class
will use these methods to access the members. A member does not need a set method when its
value is changed in other methods.
The get/set methods are not compulsory, only include them for members when other classes
require access to them.
The get methods (also called an accessor method) return the value of the member they are
associated with and the set method (also called a mutator method) sets the member to a new
value.
Using get and set methods aids maintainability, flexibility and extensibility. Good design includes
encapsulation – to do this you make public asscessor methods and force calling code to use
those methods. I.e. set and get
|
This post also has a bit of info:
http://www.cplusplus.com/forum/general/6593/
If you haven't noticed a recurring theme in my post yet it is: get/set's are pretty useless in C++, but still remain good in C# java etc...
C++ you should still use the idea of encapsulation and the first link I posted explains it well, but there's better ways to do it rather than sets/gets..
:) Have fun reading. I hope I didn't dump too much on you, you can't handle.
Just for the meanwhile do as your teacher expects, just remember there's a bigger better world out there...