1

I am a beginner and tryed to find an answer on this website but I was not able to figure out how to solve my specific C++ OOP problem.

Short: I want to access and change values of a parent class from subclass instances, but somehow my approach seems not to be working.

Example: There are many Car instances in my program (created with new construct). If one of the Car objects detects a collision, all Car instances should inverse their movement. The Car that registered the collision should call the parents' ChangeSpeed method or change the speed value directly.

Problem: the speed variable seems not to be updated. Is there something wrong with this particular code/ approach or do I have to search for my problem somewhere else?

// SpeedControl.h ------------------------------
class SpeedControl
{
public:
    void ChangeSpeed(int);
protected:
    int speed;
};

class Car:
        public SpeedControl
{
public:
    void MoveCar();
    void DetectCollision();

private:
    int position;
};


// SpeedControl.cpp ------------------------------
#include SpeedControl.h

SpeedControl::SpeedControl(void)
{
    speed = 10;
}

SpeedControl::~SpeedControl(void)
{
}

SpeedControl::ChangeSpeed(int _value)
{
    speed *= _value;
}


// Car.cpp ------------------------------
#include SpeedControl.h

Car::Car(void)
{
    position = 100;
}

Car::~Car(void)
{
}

Car::MoveCar()
{
    position += speed; // speed should be accessible?
}

Car::DetectCollision()
{
    speed *= (-1); // inverse speed variable in parent class to inverse direction of ALL cars
    // alternative:
    // ChangeSpeed(-1); // call parent function to inverse speed

}
Mykola
  • 3,343
  • 6
  • 23
  • 39
  • 1
    change *class Car:SpeedControl* to *class Car: public SpeedControl* (http://stackoverflow.com/questions/3811424/default-class-inheritance-access) – thang Feb 06 '13 at 12:47
  • You should also stay *what exactly is not working*. In this case, it's as thang suggested, private inheritance. – Bartek Banachewicz Feb 06 '13 at 12:48
  • you've changed the question now. speed is now accessible. remove that comment. inheritance is **the wrong construct to use here**. you want aggregation. create SpeedControl as a separate object. delare a (smart)pointer to it in Car. have all your cars share the same SpeedControl. – thang Feb 06 '13 at 13:00
  • specified my problem in the text, sorry. also my "SpeedControl" was already public, I only forgot to type it in. – user2046619 Feb 06 '13 at 13:00
  • so then speed is accessible. what is with that comment that implies that speed is not accessible. – thang Feb 06 '13 at 13:01

4 Answers4

0

This looks like a problem where the observer pattern is the right solution.

Marius Bancila
  • 16,053
  • 9
  • 49
  • 91
0

You should implement a GetSpeed() in your SpeedControl class, which returns the current speed.

Then you can use this in MoveCar function:

ChangeSpeed(GetSpeed() + GetSpeed());  // Assuming you want to double the speed. 

and in your collision:

ChangeSpeed(-GetSpeed());   

Alternatively, you could make "ChangeSpeed" a multiplication rather than an addition.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
0

You didn't state what exactly is wrong with your code, but from glancing on it, you're using private inheritance, which hides all parent's members from derived classes.

class Child : public Parent { };

The line above will ensure that the Parent members are accessible from Child.

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
0

You seem to misunderstand inheritance. What you need is a static variable.

Define static int speed in Car class. You can drop SpeedControl class.

palindrom
  • 18,033
  • 1
  • 21
  • 37