operator +

Pages: 12
(i) /*Create a Homework class with fields for class name, the assignment (for example, “Read chapter 11”), and the number of minutes predicted it will take to complete the assignment. Include functions to set the values for the Homework fields, to provide output, and to overload the operator + to add the minutes required by multiple Homework objects. The result of the addition is a summary Homework object.*/

and currently implementation coding
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
#include <iostream>
#include <string>

using namespace std;

template<class T>
class Homework{
private:
	string className;
	string assignment;
public:
	Homework();
	void setClassName( string );
	string getClassName();
	Homework operator + (Homework);
};

template<class T>
Homework<T>:: setClassName( string theClassName ){
	className = theClassName;
}

template<class T>
Homework<T>:: getClassName(){
	return className;
}

int main(){

	system( "pause" );
	return 0;
}


am i doing correctly ? and what the question meaning actually? sometime the question are hard to understand and i'm not relaly know what the question want me to do..
any one?
Why the need to template them, whiles you're using explicit data types in the class?

and the number of minutes predicted it will take to complete the assignment.

I think you should have made a float variable for this.

Thanks,
Aceix.
i know i should make a float variable too. i have to use template because question required . just want to know how the output display look like?

can u provide for me the output display see? not really get it
how bout operator??
How do you need to use operator+? Do you have to do something like
1
2
Homework hw1, hw2;
int total = hw1 + hw2;

?
Last edited on
can someone explain to me what i did wrong until now?

my coding
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
#include <iostream>
#include <string>

using namespace std;

template<class T>
class Homework{
private:
	string className;
	string assignment;
	//int numOfMinutes;
public:
	Homework( T value1 );
	void setClassName( string );
	string getClassName();
	void operator+();
};

template<class T>
Homework<T>:: setClassName( string theClassName ){
	className = theClassName;
}

template<class T>
Homework<T>:: getClassName(){
	return className;
}

template<class T>
void Homework::operator+ (){
	Homework totalhomework , thehomework1 , thehomework2;
	totalhomework = thehomework1 + thehomework2;
	return totalhomework;
}


int main(){
	
	int theInteger;

	//Homework<int> myint( 7 , 8 );
	//Homework<double> mydouble( 8.25 , 9,25 );


	system( "pause" );
	return 0;
}


question

1
2
3
4
5
6
7
8
9
10
11
12
(i)	Create a Homework class with fields for 
class name, the assignment (for example, “Read chapter 11”), and the number of minutes predicted it will take to complete
 the assignment.  Include functions to set the 
values for the Homework fields, to provide 
output, and to overload the operator + to
 add the minutes required by multiple 
Homework objects.  The result of the addition is a summary Homework object.


(ii)	Create a function template that adds two values and returns their sum.

(iii)	Write a main() function that tests the function template with integer, double, and Homework objects.
Last edited on
The operator + has two arguments. In this case:
1
2
3
int a = 8;
int b = 3;
int c = a + b;

a and b are the arguments in the sum on line 3. So, when overloading the + operator, you need to have two parameters:
1
2
3
4
5
6
7
8
9
10
11
12
struct Numbers {
    int a;
};

int operator + ( Numbers a, Numbers b ) {     // two parameters!
    return a.a + b.a;
}

// now you can do for example this (it has an unexpected result):
Numbers numbers1;
Numbers numbers2;
int integer = numbers1 + numbers2;


Have a look here for a full explanation of operator overloading: http://www.learncpp.com chapter 9
i get it for the operator+ d .

but how i pass the value to the operator using templates?
my currently implementation coding.
i'm know it's wrong. but hope someone give some advise which part im wrong at
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
#include <iostream>
#include <string>

using namespace std;

class Homework{
private:
	string className;
	string assignment;
	
public:
	Homework();
	int numOfminutes;
	void setClassName( string );
	string getClassName();
	template< class T >
	int operator+( T value1 , T value 2 );
};

void Homework :: setClassName( string theClassName ){
	className = theClassName;
}


string Homework :: getClassName(){
	return className;
}

template<class T>
int Homework :: operator+ (T homework1 , T homework2){
	return numOfminutes;
}


int main(){
	
	Homework thehomework1 , thehomework2;
	thehomework1.setClassName( "class 1 Batch 1 CE");
	thehomework2.setClassName( "class 2 Batch 1 CS" );
	int totaltime = thehomework1 + thehomework2;
Last edited on
According to your requirement you need to do this:

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
#include <iostream>
#include <string>

using namespace std;

class Homework{
private:
	string className;
	string assignment;
	int number_of_minutes_predicted;

public:
	Homework();
	int numOfminutes;
	void setClassName( string );
	string getClassName();
	Homework operator+(Homework); // much better parameter(avoids copy): const Homework &
};

template< class T >
T AddTwoValues(T value1 , T value2 )
{
  return value1 + value2;
}


void Homework :: setClassName( string theClassName ){
	className = theClassName;
	number_of_minutes_predicted = 0;
}


string Homework :: getClassName(){
	return className;
}

template<class T>
Homework Homework :: operator+ (Homework hw){
	Homework hw_result = *this;
	hw_result.number_of_minutes_predicted += hw.number_of_minutes_predicted;
	return hw_result;
}


int main(){
	
	Homework thehomework1 , thehomework2;
	thehomework1.setClassName( "class 1 Batch 1 CE");
	thehomework2.setClassName( "class 2 Batch 1 CS" );
	Homework thehomework3 = thehomework1 + thehomework2;
...AddTwoValues...
}
i get an error for this
error C2244: 'Homework::operator +' : unable to match function definition to an existing declaration
inside my main.cpp
1
2
3
4
5
6
7
8
9
10
int main(){
	
	int total = 0;
	Homework thehomework1 , thehomework2;
	thehomework1.setClassName( "PSDC Batch 18 CE");
	thehomework2.setClassName( "Inti Batch 1 CS" );

	Homework homework3 = thehomework1 + thehomework2;
	AddTwoValues( 2 , 3 );
	
my full implementation 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
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
#include <iostream>
#include <string>

using namespace std;

//Class Homework
class Homework{
	//...Member function for class Homework...

//Private data for class Homework
private:
	//Local and private variable
	string className;
	string assignment;
	int numOfminutes;
//Public data for class Homework	
public:
	Homework();//Constructor
	void setClassName( string );//Mutator for set class name
	string getClassName();//Accessor to get class name
	void setAssignment( string );//Mutator for set assignment
	string getAssignment();//accessor to get assignment
	void set_numOfminutes( int );
	int get_numOfminutes( );
	Homework operator+(Homework);//overloading operator + to add the minutes required by multiple Homework object
};

//Template function that return sum of the two parameters
template< class T >
T AddTwoValues(T value1 , T value2 ){
  return value1 + value2;
}

//Overloading operator+
Homework Homework :: operator+ (Homework hw){
	Homework hw_result = *this;
	hw_result.numOfminutes += hw.numOfminutes;
	return hw_result;
}

//Constructor
//Initialize the variable for string & int
Homework :: Homework(){
	className = "";
	assignment = "";
	numOfminutes = 0;
}

//Mutator
void Homework :: setClassName( string theClassName ){
	className = theClassName;
	numOfminutes = 0;
}

//Accessor
string Homework :: getClassName(){
	return className;
}

//Mutator
void Homework :: setAssignment( string theAssignment ){
	assignment = theAssignment;
}

//Accesor
string Homework :: getAssignment(){
	return assignment;
}

void Homework :: set_numOfminutes( int theminutes ){
	numOfminutes = theminutes;
}

int Homework :: get_numOfminutes(){
	return numOfminutes;
}

int main(){
	//Set multiple Homework object
	Homework thehomework1 , thehomework2;

	thehomework1.setClassName( "PSDC Batch 18 CE");
	thehomework1.setAssignment( "Read Chapter 11" );
	thehomework1.set_numOfminutes( 150 );
	
	thehomework2.setClassName( "Inti Batch 1 CS" );
	thehomework2.setAssignment( "Read Chapter 22" );
	thehomework2.set_numOfminutes( 280 );

	Homework homework3 = thehomework1 + thehomework2;

	cout << "Name for first class  : " << thehomework1.getClassName() << endl
		 << "Assignment title      : " << thehomework1.getAssignment() << endl << endl
		 << "Number of minutes that will take to complete assignment : " << thehomework1.get_numOfminutes() << endl << endl
		 << "Name for second class : " << thehomework2.getClassName() << endl
		 << "Assignment title      : " << thehomework2.getAssignment() << endl << endl
		 << "Number of minutes that will take to complete assignment : " << thehomework2.get_numOfminutes() << endl << endl ;

	cout << "Summary of time for the assignments that required [integer] : " 
		 << AddTwoValues( 30 , 40 )//with integer arguments & Homework obj
		 << "minutes" << endl;   

	cout << "Summary of time for the assignments that required [double]  : " 
		 << AddTwoValues( 30.28 , 49.18 )//with double arguments & Homework obj
		 << "minutes" << endl; 

	//cout << "The result of addition : " << homework3 << endl;

	system( "pause" );//Pause window
	return 0;//Terminate the program
}


i dont know which part i wrong at .
can u confirm with me that am i do correct due to my question ?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*(i)	Create a Homework class with
 fields for class name, the assignment
 (for example, “Read chapter 11”), and the number 
of minutes predicted it will 
take to complete the assignment.  
Include functions to set the values 
for the Homework fields, to provide output, 
and to overload the operator + to add the minutes
 required by multiple Homework objects. 
 The result of the addition is a summary Homework object.

 
(ii)	Create a function template that adds two values and returns their sum.

(iii)	Write a main() function that tests the function template with integer, double, and Homework objects.*/
Last edited on
Whoops, i forgot to remove the template:
1
2
3
4
5
6
template<class T>
Homework Homework :: operator+ (Homework hw){
	Homework hw_result = *this;
	hw_result.number_of_minutes_predicted += hw.number_of_minutes_predicted;
	return hw_result;
}


By the way you need to implement the constructor.
Set number_of_minutes_predicted to 0 there
but i changed it to numOfminutes variable . so it's correct right?
by the way .. how i gonna to display out the homework3?

and am i doing correctly so far?
last part only....
but i changed it to numOfminutes variable . so it's correct right?
Sure, you can and should rename my examples according to your needs.

how i gonna to display out the homework3?
There's no difference to thehomework1 or 2. you may name it thehomework3

you have public getters and setters for your member variables (fields). You'd have less overhead if you'd make the fields public and omit the getters/setters.

This
<< AddTwoValues( 30 , 40 )//with integer arguments & Homework obj
is not true. You do not use any Homework obj

instead of adding the homework object (on line 90) directly you should use AddTwoValues()
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
int main(){
	//Set multiple Homework object
	Homework thehomework1 , thehomework2;

	//Set information for first homework
	thehomework1.setClassName( "PSDC Batch 18 CE");
	thehomework1.setAssignment( "Read Chapter 11" );
	thehomework1.set_numOfminutes( 150 );
	
	//Set information for second homework
	thehomework2.setClassName( "Inti Batch 1 CS" );
	thehomework2.setAssignment( "Read Chapter 22" );
	thehomework2.set_numOfminutes( 280 );

	Homework homework3 = thehomework1 + thehomework2;

	//Output display for Homework 1 and homework 2
	cout << "Name for first class  : " << thehomework1.getClassName()    << endl
		 << "Assignment title      : " << thehomework1.getAssignment()   << endl << endl
		 << "Number of minutes that will take to complete assignment : " << thehomework1.get_numOfminutes() << endl << endl
		 << "Name for second class : " << thehomework2.getClassName()    << endl
		 << "Assignment title      : " << thehomework2.getAssignment()   << endl << endl
		 << "Number of minutes that will take to complete assignment : " << thehomework2.get_numOfminutes() << endl << endl ;

	cout << "Summary of time for the assignments that required [integer] : " 
		 << AddTwoValues( 30 , 40 )//with integer arguments 
		 << "minutes" << endl;   

	cout << "Summary of time for the assignments that required [double]  : " 
		 << AddTwoValues( 30.28 , 49.18 )//with double arguments
		 << "minutes" << endl; 

	cout << "The result of addition of Homework objects : " << homework3 << endl;

	system( "pause" );//Pause window
	return 0;//Terminate the program
}



for the Homework homework3 = thehomework1 + thehomework2;
no need to display it out 1?
but then how i have to show out for the result of addition of homework objects then?
Pages: 12