0

Hi friends i have one question about OOP.
I have

class A 
{
/function body here
};

class B:public A
{
/function body here
};

class C:public A
{
/function body here
};

int main()
{
B b[20];
C c[40];
}

Each object of the derived classes(B and C)has x,y and z coordiantes. My problem is here how can i update new x,y and z values for all the objects simultaneously. Every time i calculate x,y and z values for all the objects and i need ti update x,y and z values for all the objects simultaneously...not one by one...

3
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by vijayan121
0

Hmm, tough cookies. By the time you've instantiated objects, each object has a separate copy of the variables, so you have to update each object separately.

That said, it's possible to update all of the variables at once, but only if they're inherited as static members to class A:

#include <iostream>

class A {
public:
  static int x;
};

int A::x;

class B: public A {};
class C: public A {};

int main()
{
  B b;
  C c;

  b.x = 12345;

  std::cout<< b.x <<'\n';
  std::cout<< c.x <<'\n';
}

However, this also means that every object will always have the same set of values. They can't be independent and simultaneously updated at the same time.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.