Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

The BIU contains FIFO register of size bytes 12 8 16 O 4

Answered: 1 week ago

Question

What is topology? Explain with examples

Answered: 1 week ago

Question

What is linear transformation? Define with example

Answered: 1 week ago

Question

2. Place a value on the outcomes.

Answered: 1 week ago