mutator functions

Hi! I'm supposed to create a class named Appointment. I have given the private member functions, All data should be private, and reached by get- and set-functions.

The class should have appropriate constructors. I have to decide what constructors are needed / helpful.

What kind of constructors do I need?
Is my mutator/accessor functions correct?

My code:

class Appointment
{
public:
void set(string newTime); //makes it possible to change the time
void set(string newPlace); //makes it possible to change the place
void set(string newHeader); //makes it possible to change the header
void set(string newDescription); //makes it possible to change the description

string getTime(); // returns the time
string getPlace(); //returns the place
string getHeader(); //returns the header
string getDescription(); //returns the description

private:
string time;
string place;
string header; //quick summary of the appointment
string description; //longer description
};
Last edited on
I normally use two constructors: one that initializes all variables, and one that doesn't take any arguments at all. For example:

1
2
3
4
5
6
7
8
class A
{
  private:
    int b;
  public:
    A(){};
    A(int valueB) {b = valueB;}
};


[edit]
In this case I would create three constructors; the two I mentioned and one for the most important data (time and place, for example).
Last edited on
ok! Thanks!

do I need a mutator function for each private memeber?
Yes, and maybe you could add a set-function for all members to. So you have a "reset all" option.
Btw, these:
1
2
3
4
void set(string newTime); //makes it possible to change the time
void set(string newPlace); //makes it possible to change the place
void set(string newHeader); //makes it possible to change the header
void set(string newDescription); //makes it possible to change the description 


Will NOT work. You cannot overload a function simply based on the name of parameter (the compiler doesn't care what the name is if you are prototyping it)

You will need to make functions like:
1
2
3
4
void setTime(string newTime); //makes it possible to change the time
void setnewPlace (string newPlace); //makes it possible to change the place
void setHeader(string newHeader); //makes it possible to change the header
void setDescription(string newDescription); //makes it possible to change the description 
Topic archived. No new replies allowed.