I have a hw assignment asking me to create a class that will have 4 private member variables. These will be an integer representing the product’s unique ID code, a string representing the product’s name, a double representing the product’s price, and an integer representing the number in stock of the product. You need to write public getter and setter methods for each of these variables. The class must also have two friend functions that will overload the << and >> operators respectively. What sucks is that he gave it to us over spring break and its due over spring break which means I cant ask him questions. All Im going for is understanding and no code. Thanks in advance!
#include <iostream>
#include <string>
usingnamespace std;
class DayOfYear
{
private: //When you list variables as private is that so you can have multiple classes with the same type of variables so that don't get mixed up?
int month;
int day;
public:
// public getters ( accessors ) //What does it mean by getters?
int get_month() { return month; } //Is this like a function?
int get_day() { return day; }
// public setters ( mutators ) //What does it mean by setters?
void set_month(int new_month) { month = new_month; }
void set_day(int new_day) { day = new_day; }
void output();
void output2();
friend ostream& operator << (ostream& out, DayOfYear my_day);
friend istream& operator >> (istream& in, DayOfYear my_day);
};
int main()
{
DayOfYear today, tomorrow;
// today.month = 3; this is not gonna work with private member vars
// cout << today.day << endl;
today.set_month(3);
today.set_day(17);
cout << today.get_day() << endl;
cout << today.get_month() << endl;
today.output();
today.output2();
cin >> today;
cout << today;
return 0;
}
void DayOfYear::output()
{
cout << "Month: " << month << endl
<< "Day: " << day << endl;
}
void DayOfYear::output2()
{
string str_month;
switch (month)
{
case 1:
str_month = "Jan";
break;
case 2:
str_month = "Feb";
break;
case 3:
str_month = "Mar";
break;
case 4:
str_month = "Apr";
break;
case 5:
str_month = "May";
break;
case 6:
str_month = "Jun";
break;
case 7:
str_month = "Jul";
break;
case 8:
str_month = "Aug";
break;
case 9:
str_month = "Sep";
break;
case 10:
str_month = "Oct";
break;
case 11:
str_month = "Nov";
break;
case 12:
str_month = "Dec";
break;
default:
str_month = "";
break;
}
cout << "Month: " << str_month << endl
<< "Day: " << day << endl;
}
ostream& operator << (ostream& out, DayOfYear my_day) //I have no clue as to what this is...
{
out << my_day.month << endl << my_day.day << endl;
return out;
}
istream& operator >> (istream& in, DayOfYear my_day) //Same with this section
{
in >> my_day.month;
in >> my_day.day;
return in;
}
What does this mean? friend functions that will overload the << and >> operators respectively?
I did read the tutorials on here so I have a basic understanding as to what is going on but this code is confusing to me. So with a class you are defining functions outside of the "class" and calling them within the "class"?
on line 105 and 111 you are overloading the << and >> operators for cin and cout.In other words you are customizing cin>> and cout<< to work with objects made with your class DayOfYear.
Ok, so I wrote the code and Im getting an error on my output. He wants me to use the friend function overloading the << operator to print all of the information about the product to the command line and return the ostream type. The friend function overloading the >> operator should allow a user to enter information for the 4 private member variables for a product and return the istream type.
Please enter the product information below:
2341
yay
34
3
The content of the product you entered:
ID Code: -858993460
Product Name:
Price:-9.25596e+061
In Stock:-858993460