Question on classes

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!

This code was given as an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  #include <iostream>
#include <string>
using namespace 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"?
A friend function is not part of the class, but can nonetheless access the private members for the respective 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.
Last edited on
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.

Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <string>
using namespace std;

class Product
{
private:
	int id_code;
	string product_name;
	double price;
	int in_stock;

public:
	// public getters ( accessors )
	int get_id_code() { return id_code; }
	string get_product_name() { return product_name; }
	double get_price() { return price; }
	int get_in_stock() { return in_stock; }

	// public setters ( mutators )
	void set_id_code(int id) { id_code = id; }
	void set_product_name(string name) { product_name = name; }
	void set_price(double current_price) { price = current_price; }
	void set_in_stock(int stock) { in_stock = stock; }

	friend ostream& operator << (ostream& out, Product my_product);
	friend istream& operator >> (istream& in, Product my_product);

};

int main()
{
	Product my_product;
	cout << "Please enter the product information below:" << endl;
	cin >> my_product;
	cout << "The content of the product you entered:" << endl;
	cout << my_product;

	return 0;
}
ostream& operator << (ostream& out, Product my_product) //This is where I think the error is! I tried using get in this area and it didn't work.
{
	out << "ID Code:" << my_product.id_code << endl << "Product Name:" << my_product.product_name << endl << "Price:" << my_product.price << endl << "In Stock:" << my_product.in_stock << endl;
	return out;
}

istream& operator >> (istream& in, Product my_product) //I tried using set in this but it didn't work.
{
	in >> my_product.id_code;
	in >> my_product.product_name;
	in >> my_product.price;
	in >> my_product.in_stock;
	return in;
}


This is my output:

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
Is this solved?
pass my_product argument as a reference,that should solve your problem.
change everything that is Product my_product to Product &my_product.

I've made these changes //HERE!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <string>
using namespace std;

class Product
{
private:
	int id_code;
	string product_name;
	double price;
	int in_stock;

public:
	// public getters ( accessors )
	int get_id_code() { return id_code; }
	string get_product_name() { return product_name; }
	double get_price() { return price; }
	int get_in_stock() { return in_stock; }

	// public setters ( mutators )
	void set_id_code(int id) { id_code = id; }
	void set_product_name(string name) { product_name = name; }
	void set_price(double current_price) { price = current_price; }
	void set_in_stock(int stock) { in_stock = stock; }

        //HERE
	friend ostream& operator << (ostream& out, Product &my_product);
	friend istream& operator >> (istream& in, Product &my_product);

};

int main()
{
	Product my_product;
	cout << "Please enter the product information below:" << endl;
	cin >> my_product;
	cout << "The content of the product you entered:" << endl;
	cout << my_product;

	return 0;
}
//HERE
ostream& operator << (ostream& out, Product &my_product) //This is where I think the error is! I tried using get in this area and it didn't work.
{
	out << "ID Code:" << my_product.id_code << endl << "Product Name:" << my_product.product_name << endl << "Price:" << my_product.price << endl << "In Stock:" << my_product.in_stock << endl;
	return out;
}
//and HERE
istream& operator >> (istream& in, Product &my_product) //I tried using set in this but it didn't work.
{
	in >> my_product.id_code;
	in >> my_product.product_name;
	in >> my_product.price;
	in >> my_product.in_stock;
	return in;
}
Last edited on
Topic archived. No new replies allowed.