(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.*/
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..
(i) Create a Homework class with fields forclass 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.
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;
};
intoperator + ( 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) 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.*/
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()
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?