Answer to Question #330817 in C++ for Harsh

Question #330817

Choose data members for class Car to define its properties. Create 2 objects of class Car and



overload the operator ‘greater than’ > to compare both the objects concerning their data members

1
Expert's answer
2022-04-20T03:44:59-0400
#include <iostream>
#include <string>

using namespace std;

class Car
{
	string brand;
	string color;
	string carType;
	int carCC;
public:
	Car(){}
	Car(string _brand, string _color, string _carType, int _carCC)
	:brand(_brand), color(_color), carType(_carType), carCC(_carCC){}
	void Assign()
	{
		cout << "Please, enter a brand of car: ";
		cin >> brand;
		cout << "Please, enter a color of car: ";
		cin >> color;
		cout << "Please, enter a car type: ";
		cin >> carType;
		cout << "Please, enter a car cc (100-1299): ";
		cin >> carCC;
	}
	void Display()
	{
		cout << "\nInfo about car:\n";
		cout << brand << "\t"
			<< color << "\t"
			<< carType << "\t"
			<< carCC << "\n";
	}
	friend bool operator> (Car& a, Car& b);
};


bool operator> (Car& a, Car& b)
{
	return a.carCC > b.carCC;
}


int main()
{
	Car a("Toyota","white","sedan",900);
	a.Display();
	Car b;
	b.Assign();
	b.Display();
	if(a > b)
		cout<<"Car a greater than Car b"<< endl;
	else
		cout << "Car a is not greater than Car b" << endl;
}





Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS