-6

Suppose I have two classes Class A and Class B.

class A {
    uint32 A_1;
    uint32 A_2;
    uint32 A_3;
    uint32 A_4;
} 

First Question: How can I make the class members of class A that is uint_32 A_1, A_2, A_3, A_4 visible to an object created from class B?

// Object Created from Class B is below:

Object_B = new Class B(); 

My Syntax for making Class A class members visible to Class B is below for first question:

Class A(Class B *);

Second Question: How can I access those class members (uint_32 A_1, A_2, A_3, A_4) of Class A through the object I created that is: Object_B?

Below is the syntax for second question:

Object_B->A_1;

Are the above syntax for two classes correct?

Also, please provide me some good links for class, object in C++ so that I can read through deeply.

andre
  • 7,018
  • 4
  • 43
  • 75

2 Answers2

3
new A( B * bPointer);

Tells us that an object of type A owns a pointer to the type B. In no way does this associate B to A.

In fact B should not know about A if this is a well designed class. Which makes your second question moot. So the important question you should ask, is why do you want this relationship. What is the semantic relationship between A and B?

Is it a Book(Page* pages) ?

Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67
0

from the link: http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fcplr129.htm

Protected members (C++ only)

A protected nonstatic base class member can be accessed by members and friends of any classes derived from that base class by using one of the following:

A pointer to a directly or indirectly derived class A reference to a directly or indirectly derived class An object of a directly or indirectly derived class

If a class is derived privately from a base class, all protected base class members become private members of the derived class.

If you reference a protected nonstatic member x of a base class A in a friend or a member function of a derived class B, you must access x through a pointer to, reference to, or object of a class derived from A. However, if you are accessing x to create a pointer to member, you must qualify x with a nested name specifier that names the derived class B. The following example demonstrates this:

 class A {
  public:
  protected:
  int i;
  };


  class B : public A {
  friend void f(A*, B*);
  void g(A*);
  };

  void f(A* pa, B* pb) {
  //  pa->i = 1;
  pb->i = 2;

 //  int A::* point_i = &A::i;
 int A::* point_i2 = &B::i;
  }

void B::g(A* pa) {
//  pa->i = 1;
i = 2;

//  int A::* point_i = &A::i;
int A::* point_i2 = &B::i;
}

void h(A* pa, B* pb) {
//  pa->i = 1;
//  pb->i = 2;
}

int main() { }
user1066524
  • 373
  • 2
  • 11
  • 24