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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  /*
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 <iostream>

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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:
1
2
3
4
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;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>

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.