c++ - Accessing and changing parent values from subclass instances -



c++ - Accessing and changing parent values from subclass instances -

i beginner , tryed find reply on website not able figure out how solve specific c++ oop problem.

short: want access , alter values of parent class subclass instances, somehow approach seems not working.

example: there many car instances in programme (created new construct). if 1 of car objects detects collision, car instances should inverse movement. car registered collision should phone call parents' changespeed method or alter speed value directly.

problem: speed variable seems not updated. there wrong particular code/ approach or have search 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 accessible? } car::detectcollision() { speed *= (-1); // inverse speed variable in parent class inverse direction of cars // alternative: // changespeed(-1); // phone call parent function inverse speed }

this looks problem observer pattern right solution.

c++ oop inheritance parent subclass

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -