0
#include <iostream>
using namespace std;

class simpleFunction
{
protected:
int score;

public:
simpleFunction()
{
     score = 5;
}

int retScore()
{
    return score;
}
};

class changeFunction : public simpleFunction
{
public:
void change()
{
    score = 6;
}
};

int main() 
{
simpleFunction SimpleFunction;
changeFunction ChangeFunction;
cout << SimpleFunction.retScore() << endl; 
ChangeFunction.change(); // Changes Score To 6
cout << SimpleFunction.retScore() << endl; // Should Return 6 But Returns 5 Instead
return 0;
}

ive set the score to 5 and when i use the change function it should change it to 6 but instead it returns the value 5.

the only way ive managed to make it work as intended to is by changing the int score variable to a global varible but all the classes can access it which makes it flawed can anyone help or try to explain this problem to me.

The Program Works but im having a problem with returning the correct values just to clear up any misunderstadnings

  • Welcome to Stack Overflow. Please take the time to read [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) what and how you can ask here. – πάντα ῥεῖ May 25 '17 at 16:22
  • 1
    What's your particular problem? Any error messages, unexpected results at runtime? Provide a [MCVE] please. – πάντα ῥεῖ May 25 '17 at 16:24
  • 1
    You should re-think what deriving a user from a database means. – George May 25 '17 at 16:32
  • Why do you have some variables named with lowercase and some with uppercase? Be consistent. Most prefer lower case. – Christopher Pisz May 25 '17 at 16:33
  • Include the output you are getting and the expected output in your post. – Christopher Pisz May 25 '17 at 16:34
  • Read this over several times and see if it addresses your problems: https://isocpp.org/wiki/faq/basics-of-inheritance – Christopher Pisz May 25 '17 at 16:35
  • 1
    admin.AccountHolderName is a different variable than User.AccountHolderName. – stark May 25 '17 at 17:39
  • So `UserPrivilages` **is-a** `Database`? It should sound silly to you, but that's how you model your data. – StoryTeller - Unslander Monica May 25 '17 at 19:46
  • Sorry about that i was trying to understand classes and kinda forgot to clean up my code before posting ive made a more cleaner version if you would have a look at it please it would be appreciated – LearningToCodexix May 25 '17 at 23:56
  • @LearningToCodexix `SimpleFunction` and `ChangeFunction` are two completely different objects, it therefore does not follow that changing ones state (excluding side effects) would affect the others. Oh and i'm not really sure how to phrase this correctly, but you should seek better learning materials, if you got the notion for your question from your current learning materials, drop them and don't look back :) [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) is a good starting point for grabbing good learning materials. – George May 26 '17 at 20:19

0 Answers0