简体   繁体   中英

I'm trying to store values in variables in a class, from main() by using accessor and mutator functions

My teacher wants me to use three accessor functions, getMonth(), getDate(), and getYear() as well as three mutator functions setMonth(), setDate(), and setYear(). Please ignore the comments in my program as they may be false. SO far I've written the three mutator functions (the sets). I get these confused on these and so haven't written the accessor (get) functions yet. The main question is, why when it runs does it only retrieve the day, while month and year display as 0? Thanks in advance!

#include <iostream>
using namespace std;

class Date
{
public :
void setDate(int,int,int); //behavior
void displayDate();        //behavior
    Date();         // default constructor prototype
    Date(int, int, int); // parameterized contructor prototype
int setMonth(int); // mutator function prototype
int setDay(int);   // mutator function prototype
int setYear(int);  // mutator function prototype

private:
int month; //attributes
int day;   //attributes
int year;  //attributes
};

Date::Date() {month = 1; day = 01; year = 2012;}   // default constructor 

void Date::setDate(int num1, int num2,int num3) 
{ month = num1; //instance
day = num2;   //instance
year = num3;  //instance
}

void Date::displayDate()
{  cout<<month<<'-'<<day<<'-'<<year;
}

int Date::setMonth(int month) //accessor function to return month
{
cout<<"Enter the month: ";
cin>>month;
return month;
}

int Date::setDay(int day) //accessor function to return day
{
cout<<"Enter the day: ";
cin>> day;
return day;
}

int Date::setYear(int year) //accessor function to return year 
{
cout<<"Enter the year: ";
cin>> year;
return year;
}


main()
{
 Date anvsy;
 int month, day, year;
 cout<<"Please Enter the information for your anniversary" << endl << endl;

anvsy.setMonth(month); //call to accessor function to return month

anvsy.setDay(day); //call to accessor function to return day

anvsy.setYear(year); //call to accessor function to return year

anvsy.setDate( month, day, year);
cout<<endl;
cout<<"The Anniversary Date is on ";
anvsy.displayDate();

cin.get();
cin.get();
return 0;
}

int Date::setYear(int year)

For these functions you are passing in a variable called "year", "month", etc which is then the variable that is modified in that function (as opposed to modifying the private class variable with the same name).

Also worth noting is that the way you have it laid out, you shouldn't have to keep a copy of year/month/day in your main function as that is the point of having it in the class.

That said, generally you want your class to represent an object, and using cin/cout within the class is doing more than representing the object. If you wanted to take this class to another project that didn't require you to cin/cout the variables when entered, or required you to enter them from a different source (like a file) you'd have to change all of that code. I'd keep your input/output separated from your class (in this case, probably in main()) and have your set functions take an input and use that input to update the internal private variable and the get functions simply return that value.

RyanP is absolutely right, the class and how the data is input could do with refactoring for better seperation of responsibilities.

That said, the main reason your code is not working is because when the day, month and year are input, you then return the values. But in main it looks like you intend to pass the variables in by reference. Either of these approaches is valid, but you must choose one or the other. See the two examples below, you could use either of them:

Pass by reference:

// Note the & after int - this indicates the paramater is now passed by reference
// No need for return type
void Date::setMonth(int& month)
{
  cout<<"Enter the month: ";
  cin>>month;
  //return month; // Don't need this line. Function now returning void
}

// Pass month by reference
anvsy.setMonth(month);

Or this, use return value:

// No need for any parameters
int Date::setMonth()
{
  cout<<"Enter the month: ";
  cin>>month;
  return month;
}

// Use return value
month = anvsy.setMonth();

You mentioned the comments might not be correct - I think you've got a bit confused about what is an accessor and what is a mutator, because the functions don't have clearly defined responsibilities. A mutator should only set the value of a member variable, and an accessor should only return the value of a member variable. Any other responsibilities (eg user input) should be handled elsewhere

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM