Question: using this code for c + + : #include using namespace std; class Mass { public: Mass ( double kilograms = 0 . 0 ,

using this code for c++: #include
using namespace std;
class Mass {
public:
Mass(double kilograms =0.0, double grams =0.0);
void Print() const;
Mass operator+(Mass rhs);
Mass operator+(double rhs);
private:
double kg;
double g;
};
Mass::Mass(double kilograms, double grams){
kg = kilograms;
g = grams;
}
// No need to accommodate for overflow or negative values
/* Your code goes here */
Mass Mass::operator+(const Mass& rhs) const {
double totalKg = kg + rhs.kg;
double totalG = g + rhs.g;
if (totalG >=1000.0){
totalKg += totalG /1000.0;
totalG = static_cast(totalG)%1000;
}
return Mass(totalKg, totalG);
}
void Mass::Print() const {
cout kg " kilograms, " g " grams";
}
int main(){
double kilograms1;
double grams1;
double kilograms2;
double grams2;
cin >> kilograms1;
cin >> grams1;
cin >> kilograms2;
cin >> grams2;
Mass mass1(kilograms1, grams1);
Mass mass2(kilograms2, grams2);
Mass sum1= mass1+ mass2;
Mass sum2= mass1+ kilograms2;
mass1.Print();
cout endl;
mass2.Print();
cout endl;
cout "Sum: ";
sum1.Print();
cout endl;
cout endl;
mass1.Print();
cout endl;
cout kilograms2" kilograms" endl;
cout "Sum: ";
sum2.Print();
cout endl;
return 0;
} Four doubles are read from input, where the first two doubles are the kilograms and grams of mass1 and the second two doubles are the kilograms and grams of mass2. Define two functions to overload the + operator. The first function overloads the + operator to add two masses. The second function overloads the + operator to add a mass and a double representing the number of kilograms.
Ex: If the input is 9.0369.54.02.5, then the output is:
9 kilograms, 369.5 grams
4 kilograms, 2.5 grams
Sum: 13 kilograms, 372 grams
9 kilograms, 369.5 grams
4 kilograms
Sum: 13 kilograms, 369.5 grams
Note: The sum of two masses is:
the sum of the number of kilograms
the sum of the number of grams
Note: The sum of a mass and a double representing the number of kilograms is:
the sum of the number of kilograms and the double
the number of grams is unchanged 603602.4281174.q3zqy7
Jump to level 1
Four doubles are read from input, where the first two doubles are the kilograms and grams of mass1 and the second two doubles
are the kilograms and grams of mass2. Define two functions to overload the + operator. The first function overloads the +
operator to add two masses. The second function overloads the + operator to add a mass and a double representing the number
of kilograms.
Ex: If the input is 9.0369.54.02.5, then the output is:
9 kilograms, 369.5 grams
4 kilograms, 2.5 grams
Sum: 13 kilograms, 372 grams
9 kilograms, 369.5 grams
4 kilograms
Sum: 13 kilograms, 369.5 grams
Note: The sum of two masses is:
the sum of the number of kilograms
the sum of the number of grams
Note: The sum of a mass and a double representing the number of kilograms is:
the sum of the number of kilograms and the double
the number of grams is unchanged #include g = grams;
}
// No need to accommodate for overflow or negative values
/* Your code goes here */
void Mass::Print() const {
cout kg " kilograms, " g " grams";
}
int main(){
double kilograms1;
double grams1;
double kilograms2;
double grams2; cin >> kilograms1;
cin >> grams1;
cin >> kilograms2;
cin >> grams2;
Mass mass1(kilograms1, grams1);
Mass mass2(kilograms2, grams2);
Mass sum1= mass1+ mass2;
Mass sum2= mass1+ kilograms2;
mass1.Print();
cout endl;
mass2.Print();
cout endl;
cout "Sum: ":cout endl;
cout "Sum: ";
sum1.Print();
cout endl;
cout endl;
mass1.Print();
cout endl;
cout kilograms2" kilograms" endl;
cout "Sum: ";
sum2.Print();
cout endl;
return 0;
}
using this code for c + + : #include using

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!