A beginner's class and assignment. May a little bit help?

Hi! Currently, I'm learning C++ classes and object oriented programming. It's hard for me to learn because this is my first language. I have an assignment and I didn't figure out how to solve it. And I write some base code to understand how all these work. Also can you check about my indentation, typing mistakes and not using popular choices by current developers. Because I'm not so sure.

The problem, I translated it from Turkish, is inside the code as comment on the top. May It can't make any sense but it's the problem and I put my best to translate it. I know here is not assignment making platform but you know I just want to learn through its right way.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129`` `````` /* The distance between A and B is 100km. If there is no wind, the car consumes 7 liter of fuel to get through A to B. If there is a wind and the car goes along with it, it consumes 10% less than normal. If the car goes against the wind, it consumes 15% more than normal. Thr wind's direction is A to B. The car starting at A and goes through the A-B-A. Write a C++ problem that calculates fuel consumption of the car on this route. Create a Fuel class. All the objects of this class must be creatable with three different constructor types. (No-args, with distance, with distance and fuel) There must be a Cost() friend-function that gives consumed fuel amount (That will be just "fuel" in code.). Must write "+" and "=" overloaded operators that belongs to the Fuel class. */ #include using namespace std; class Fuel { private: //Class Attributes double distance; double fuel; //Friend-Function Class Prototype friend double Cost (Fuel &car); public: //Constructors Fuel (); Fuel (double distance); Fuel (double distance, double fuel); //Copy Constructor Fuel (const Fuel &object); //Destructor ~Fuel(); //Setters void set_distance (double distance); void set_fuel (double fuel); //Getters double get_distance (); double get_fuel (); }; //No-args Constructor Implementation Fuel::Fuel () : distance {0}, fuel {0} { cout << "No-arg Constructor is called." << endl; } //One-Double Constructor Implementation Fuel::Fuel (double distance) : distance {distance}, fuel {0} { cout << "Constructor is callad that includes a double." << endl; } //Two-Doubles Constructor Implementation Fuel::Fuel (double distance, double fuel) : distance {distance}, fuel {fuel} { cout << "Constructor is called that includes two doubles." << endl; } //Copy Constructor Implementation Fuel::Fuel (const Fuel &object) { cout << "Object is copied." << endl; } //Destructor Implementation Fuel::~Fuel () { cout << "Destructor is called." << endl; } //Distance Setter Implementation void Fuel::set_distance (double distance) { this -> distance = distance; } //Fuel Setter Implementation void Fuel::set_fuel (double fuel) { this -> fuel = fuel; } //Distance Getter Implementation double Fuel::get_distance () { return distance; } //Fuel Getter Implementation double Fuel::get_fuel () { return fuel; } //Friend-Function Implementation double Cost (Fuel &car) { return car.fuel; } int main () { Fuel car; cout << "This is the cars' consuming : " << Cost(car) << endl; Fuel bus {50}; cout << "This is the bus' distance: " << bus.get_distance() << endl; cout << "This is the bus' fuel: " << bus.get_fuel() << endl; cout << "This is the bus' consuming : " << Cost(bus) << endl; Fuel taxi {50,5}; cout << "This is the taxi's distance: " << taxi.get_distance() << endl; cout << "This is the taxi's fuel: " << taxi.get_fuel() << endl; cout << "This is the taxi's consuming : " << Cost(taxi) << endl; }``````

Bus and taxi objects are only for testing purposes. I think we will use only the car object.
using namespace std is considered bad practice

lines 24 and 25 can initialize:
double fuel{}; //0.0
which eliminates the need for your basic () constructor.

what you have is more typical and usually necessary, but in this specific case, you can default parameter to save making so many ctors:

 ``123456789101112131415161718192021`` ``````class foo { int x{}; int y{}; public: foo(int a = 0, int b = 0) : x{a}, y{b} {} void print() {cout << x << " " << y << endl;} }; int main() { foo f; f.print(); foo g (42); g.print(); foo h (88,123); h.print(); }``````

you do not need to write the destructor if it does nothing.

if you use different names, you can get rid of extra this-> syntax
consider:
 ``1234`` ``````void Fuel::set_distance (double distance_in) { distance = distance_in; }``````

what you have looks good. The above changes save you typing, but the result is identical.
I strongly suggest you finish this program, and ask for help here if you get stuck (post the new code please). You have a good start but its mostly empty print statements. If you can finish this one, you will be able to do the next one much faster and easier than if you put this aside and get a new assignment. The next one will expect you to know everything you did here AND something new.
Last edited on
You also don't need a copy constructor as the default one is fine here.

Getters should be marked as const (any function that doesn't change member variables should be const).

Last edited on
Thanks for your kind suggestions jonnin and seeplus. I tried to apply these to my code according to my assignment. If there anything you wanted to add, you can tell.
Here is the last code;

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859`` ``````#include class Fuel { private: //Class Attributes double distance; double fuelConsumed; //Friend-Function Class Prototype friend double Cost (const Fuel &route); public: //Constructor used with default parameters. Fuel (double distance = 0, double fuelConsumed = 0); //Overloading of "+" Operator Fuel operator+ (const Fuel &route1); }; //Two-Doubles Constructor Implementation Fuel::Fuel (double distance, double fuel) : distance {distance}, fuelConsumed {fuel} { //Nothing to write here. } //Friend-Function Implementation double Cost (const Fuel &route) { return route.fuelConsumed; } //+ Operator Overload Implementation Fuel Fuel::operator+ (const Fuel &route) { Fuel temp; temp.distance = distance + route.distance; temp.fuelConsumed = fuelConsumed + route.fuelConsumed; return temp; } int main () { //Wind's Effect Over Consumed Fuel double fuelConsAlongWind = 7 * 0.9; double fuelConsAgainstWind = 7 * 1.15; Fuel route1 {100,fuelConsAlongWind}; // A to B, Goes Along with the Wind Fuel route2 {100,fuelConsAgainstWind}; // B to A, Goes Against the Wind //Assignment Operator (=) is already overloeaded? Fuel routeAll = route1 + route2; // A - B - A std::cout << "This is the cost: " << Cost(routeAll) << std::endl; return 0; }``````
Topic archived. No new replies allowed.