Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class named Car that has the following member variables: year – an int t

ID: 3881751 • Letter: W

Question

Write a class named Car that has the following member variables:

year – an int that holds the car’s model year

make – a string that holds the make of the car

speed – an int that holds the car’s current speed

In addition, the class should have the following member functions.

Constructor – the constructor should accept the car’s year and make as arguments and assign these values to the object’s year and make member variables. The constructor should initialize the speed member variable to 0.

Accessors – Appropriate accessor functions should be created to allow values to be retrieved from an object’s year, make and speed member variables.

accelerate – The accelerate function should add 5 to the speed member variable each time it is called.

brake –The brake function should subtract 5 from the speed member variable each time it is called.

overloaded relational operator== – compare if two cars have the same year and make

overloaded input>> operator – input a car’s year, make and speed

overloaded output<< operator – output a car’s year, make and speed

Demonstrate the class in a program that creates a Car object, and then calls the accelerate function five times. After each call to the accelerate function, get the current speed of the car and display it. Then, call the brake function five times. After each call to the brake function, get the current speed of the car and display it. Lastly, test the three overloaded operators.

Explanation / Answer

#include<iostream>

#include<string>

using namespace std;

class car

{

int year;

string make;

int speed;

public:

car(int yr, string mak);

car();

//accessors

int getYear() const;

string getMake() const;

int getSpeed() const;

void accelerate();

void appyBreak();

friend istream &operator>>(istream &in, car &obj);

friend ostream &operator<< (ostream &out, car &obj);

bool operator == (const car &obj);

};

car::car()

{

year = 2000;

make = "";

speed = 0;

}

bool car::operator==(const car &obj)

{

if (year == obj.year && make == obj.make)

{

return true;

}

else

{

return false;

}

}

istream &operator>>(istream &in, car &obj)

{

in >> obj.year;

in >> obj.make;

return in;

}

ostream &operator<<(ostream &out, car &obj)

{

out << "Year: "<<obj.getYear() << " Make : " << obj.getMake() << " Speed: " << obj.getSpeed() << endl;

return out;

}

car::car(int yr, string mak)

{

year = yr;

make = mak;

speed = 0;

}

int car::getYear() const

{

return year;

}

string car::getMake() const

{

return make;

}

int car::getSpeed() const

{

return speed;

}

void car::accelerate()

{

speed += 5;

}

void car::appyBreak()

{

speed -= 5;

}

int main()

{

int yr;

string str;

car obj1(2005, "Honda"),obj2(2006,"Hundai"),obj3;

//test overloading of >>

cout << "Checking operator overloading >> for obj3" << endl;

cin >> obj3;

//accelerate the speed 5 times

for (int i = 0; i < 5; i++)

{

obj1.accelerate();

//output using overloaded operator <<

cout << obj1 << endl;

}

//test appyBreak function

for (int i = 0; i < 5; i++)

{

obj1.appyBreak();

//output using overloaded operator <<

cout << obj1 << endl;

}

//compare two car objects with operator ==

if (obj1 == obj3)

{

cout << "Two car objects obj1 and obj3 are equal" << endl;

}

else

{

cout << "Two car objects obj1 and obj3 are not equal" << endl;

}

if (obj1 == obj2)

{

cout << "Two car objects obj1 and obj2 are equal" << endl;

}

else

{

cout << "Two car objects obj1 and obj2 are not equal" << endl;

}

}

/*output

Checking operator overloading >> for obj3

2005

Honda

Year: 2005 Make : Honda Speed: 5

Year: 2005 Make : Honda Speed: 10

Year: 2005 Make : Honda Speed: 15

Year: 2005 Make : Honda Speed: 20

Year: 2005 Make : Honda Speed: 25

Year: 2005 Make : Honda Speed: 20

Year: 2005 Make : Honda Speed: 15

Year: 2005 Make : Honda Speed: 10

Year: 2005 Make : Honda Speed: 5

Year: 2005 Make : Honda Speed: 0

Two car objects obj1 and obj3 are equal

Two car objects obj1 and obj2 are not equal

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote