Program in c++
Ali Raza is a frequent long distance driver. He wants to compute how much distance he covers over a number of days. Ali also wants to know the average distance travelled.
To perform this task you will need to create a class called distance. The class will contain the fuel price in liter and also the distance he has travelled. To find the average and the distance travelled you will need to overload two operators as follows:
+ operator to compute the total distance travelled.
/ operator to compute the average distance travelled.
In the main( ) you will create six objects and then you will write a single expression that will demonstrate the use of + and the / operator. Your expression should resemble the following:
obj1+obj2+obj3+obj4+obj5+obj6 / 6
#include<iostream>
using namespace std;
class Distance
{
double fuelPrice;
double dist;
public:
Distance(double _fuelPrice, double _dist)
:fuelPrice(_fuelPrice), dist(_dist){}
Distance& operator+ (const Distance& d)
{
dist += d.dist;
fuelPrice +=d.fuelPrice;
return *this;
}
Distance& operator/ (int d)
{
dist /= d;
fuelPrice /= d;
return *this;
}
void Display()
{
cout << "\nThe average fuel price is " << fuelPrice;
cout << "\nThe average distance is " << dist;
}
};
int main()
{
Distance obj1(5,100);
Distance obj2(5, 105);
Distance obj3(6, 80);
Distance obj4(6.5, 70);
Distance obj5(5, 120);
Distance obj6(7, 90);
(obj1 + obj2 + obj3 + obj4 + obj5 + obj6)/6;
obj1.Display();
}
Comments
Leave a comment