I have a base class (A) and two derived classes (B,C) (mode of inheritance is public).

I have two values in base class A that needs to be modified by derived classes B,C so i declared their get() and set() under protected in base class.

Now each of the derived class is able to modify the values in base class. But the thing is, if derived class B modifies a private varible in base class A using the inherited set function, the updated value is not coming in derived class C. Upated value appears in both base class A and in the derived class B. But the other derived class C still getting old value.

I know it's hard to resolve it without seeing the code. But, it's a very big code. Just wanted to know if am missing conceptually.

Please help.

Thanks

Please look into your get() once again. There should be something to do in there.

Rgds,

Kanha

My objective is to link them both classes together through that common variable in Base class A. Any change made by Object of derived class B should reflect when the same value is fetched by object of derived class C.

Hi Thomas

I don't code in C++ but in C# we would do this with a static property.

I do think you may be misunderstanding inheritance.

Could you post the simplest version possible of code needed to demonstrate what you are trying to achieve and I can comment further.

Hi @DaveAmour,

Sure. Please check this.

class MasterAccount //Base class

{

public:
    MasterAccount(); //constructor
    ~MasterAccount(); //destructor
    double getbalance(); //function to get balance

private:
    double cashbalance; 

protected:
    void setcashbalance(double); //function to set balance

};


class ChildAccount1: public MasterAccount // Derived Class

{

public:

    ChildAccount(); // constructor 
    ~ChildAccount(); // destructor for stockaccount class


private:

};




class ChildAccount2: public MasterAccount

{

private:


public:
    ChildAccount2(); // constructor
    ~ChildAccount2(); // Destructor


};

This is my class structure.

I'll be creating objects of base class and derived classes.

Derived class objects will make use of private members in base class through getbalance() functions. What i need is, whatever changes the derived class objects makes to base class private members should be reflected next time it is accessed by any object.

For example:

If cashbalance is modified by object of Childaccount1, then childaccount2 or masteraccount should get that modified value then onwards. basically every object should be able to update the same value.

I'm not sure but i think that derived class objects have different copies of the same base class variable. Please correct if i'm wrong.

Your basic problem is that A is a base class of both B and C, and the instance of A in an instance of B, is NOT the same instance in C! If you make the variable a static member rather than an instance variable, then both B and C will see the changes the other made to it.

@rubbeman - Yes, i too think that's the issue.

static double cashbalance; // this will solve the issue i think

I have a question. If i declare this member as static, does the get and set functions that access this variables needs to be converted to static member functions?

because when i made that variable static, i got a bunch of errors.
*
ld: 6 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1*

Hi all,

Please ignore my last message. I got it fixed. I forgot to initialize static variable and that caused the issue.

I learned something new today. Thank you all who helped me and thank you daniweb!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.