class problem. would this work as I tried to?

"class tire" is given and it calculates distance a tire ran by the inputted number of rolls.
Now I would like to build "class Auto" that has frontRight/Left, rearRight/Left and a function void drive(int amount); that rolls each tire.

this is given class Tire
1
2
3
4
5
6
7
8
9
10
11
12
13
class Tire {
public:
	Tire();
	void roll(int& amount);
private:
	int myDistance;

};

Tire::Tire() :myDistance(0) {}
void Tire::roll(int& amount) {
	myDistance += amount;
}


and this is class Auto I wrote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Auto {
public:
	Auto();
	void drive(int amount);
private:
	Tire frontRight;
	Tire frontLeft;
	Tire rearRight;
	Tire rearLeft;
};

Auto::Auto() {}
void Auto::drive(int amount){
	frontRight.roll(amount);
	frontLeft.roll(amount);
	rearRight.roll(amount);
	rearLeft.roll(amount);

}


would this run as I meant to??
I'm confused because the function Tire::roll() uses a private member...
Last edited on
I dont believe that will work. Only way to access it would be via member function so create one and add it to the Tire::roll() function
@ejkang62, at first glance your idea makes sense to me.
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
#include <iostream>

class Tyre {
public:
    Tyre();
    void roll(int& amount);
    int getDistance() const;
private:
    int myDistance;
};

Tyre::Tyre() :myDistance(0) {}

void Tyre::roll(int& amount)
{ myDistance += amount; }

int Tyre::getDistance() const
{ return myDistance; }

class Auto {
public:
    Auto();
    void drive(int amount);
    void relateAboutTyres();
private:
    Tyre frontRight;
    Tyre frontLeft;
    Tyre rearRight;
    Tyre rearLeft;
};

Auto::Auto() {}

void Auto::drive(int amount){
    frontRight.roll(amount);
    frontLeft.roll(amount);
    rearRight.roll(amount);
    rearLeft.roll(amount);
}

void Auto::relateAboutTyres()
{
    std::cout << "Front right tyre: " << frontRight.getDistance() << "; "
              << "Front left tyre: " << frontLeft.getDistance() << "; "
              << "Rear right tyre: " << rearRight.getDistance() << "; "
              << "Rear left tyre: " << rearLeft.getDistance() << std::endl;
}


int main()
{
    Auto car;
    car.drive(50);
    car.relateAboutTyres();

    return 0;
}


Output:
Front right tyre: 50; Front left tyre: 50; Rear right tyre: 50; Rear left tyre: 50


Am I misunderstanding something?
Of course there could be other solutions.
at the moment all tyres/tires roll the same distance. i'd add another variable to Auto to take into a/c at what distance reading each tire was installed
Topic archived. No new replies allowed.