Question
Write C++ code to modify your Car class to add two new member functions that allow for operator overloading: have the first new member function
Write C++ code to modify your Car class to add two new member functions that allow for operator overloading: have the first new member function allow for overloading of the addition (+) operator, and the second member function allow for overloading of the subtraction operator (-). Each should have a car object passed in as an argument, and return the sum or difference of the speed of the this object and the passed in car object. Have the return values be floats, ints, or doubles, depending upon the data type you used for speed. Here is a copy of what I got so far:
class Car { private: int speed(); public: Car(int = 0); virtual void accelerate(); virtual void stop(); virtual void getSpeed() const; Car operator - (const Car& aCar); Car operator + (const Car& aCar); }; Car Car::operator + (const Car& aCar) { int sumOfSpeeds; sumOfSpeeds = (this -> speed + aCar.speed) cout<< sumOfSpeeds << endl; return sumOfSpeeds; } Car Car::operator - (const Car& aCar) { int difference; difference = (this -> speed - aCar.speed) cout<< difference << endl; return difference; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started