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
#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;
}
Comments
Leave a comment