Problem: Class B should take values from class A

Hi there...
I've got a Class 'A' with some functions and values and a class 'B' which should use class 'A', like this:
class B { private: A *someAs;};
The problem is how do I get strings from an object of type 'A' into an object of type 'B'?
Can I use A's functions? And if that's possible, how?
LVL 1
lorcanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

shivsaCommented:
yes u can A's functions just like u use any other function. remember to put reference to A class.
itsmeandnobodyelseCommented:
class A
{
private:
      string any;
public:
      A(string a) :any(a) {}
      string getAny() { return any; }

};

class B
{
private:
      A* pA;
public:

      void doSomethingWithA()
      {  
         pA = new A("any text");  
         string any = pA->getAny();
        // here it is
      }

};

Hope that helps

Alex


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
monkesdbCommented:
you can also use...

pA[index].getAny();

if you declare pA like...

pA = new A[100];

but for this you need to give A a public default constructor.
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Sys_ProgCommented:
Hi lorcan,

Whenever u create a class, u can either have data-members/functions as public, protected OR private.

Now, U can access public data-members/functions outside the class using a variable/pointer of that particular class.

You cannot access private members/functions from outside the class. i.e. only functions within the class can access other private functions/data-members of that class.

Thus, in your case, you are declaring a pointer to class A in class B.
Thus, u can access ONLY PUBLIC data-members / functions using this pointer. Not to mention, that data-membsrs are normally NOT MADE PUBLIC. Only functions should be normally made PUBLIC. You can always have PUBLIC member functions thru which u can access the data members
You will have to use the deference operator -> as shown in other posts if u use a pointer OR the dot '.' operator if u declare an object of type A in B.

As shown in above posts, u will have to allocate memory to the pointer of type A in constructor
e.g. A *p = new A ;

HTH
Amit
lorcanAuthor Commented:
Thank you a lot but that's what i've got so far:
class A
{
    private:
        char *name;
        int number;
        double price;
    public:
        A(char *nameE,int numberE,double priceE);
        A();
        ~A();
        A(A &a);
        A & operator=(A &a);
};
class B
{
    private:
        A *b;        //my array of type A
        int menge, *anzahl;
    public:
        B(A &a);
        B();
        ~B();
};
A::A()
{
    name = new char[1];
    number = 0;
    price = 0.0;
    name[0] = '\0';
}

    and now I want to use:
    1. a default constructor to create b of type A
    2. a constructor to get the values from a into b[0] and b[1] and so on...
    3. copy b into c of Type B

itsmeandnobodyelseCommented:
i am always excited about names that other developers choose. Mostly i wouldn't have dreamed to take such names.  But to call a member array of objects of class A 'b' in class B tops all i've seen before... ;-)

To your questions:

> a default constructor to create b of type A

Default constructors normally don't create instances  because a default constructor - by definition - has no arguments. So, i think that  in default constructor you should set the pointer to NULL and provide a public member function like

class B
{
private:
     ...
     int  menge;  //  counts the size of array A
     ...
public:
    A* addA(const char* nam, int num, double pric)
    {
          A* oldb = b;
          if (b)
          {
              b = new A[menge + 1];
              for (int i = 0; i < menge; i++)
                    b[i] = oldb[i];   // using A::operator=
              A a(nam, num, pric);
              b[menge] = a;
              menge++;
              delete [] oldb;
          }
    }
    ...
}


> a constructor to get the values from a into b[0] and b[1] and so on...

that i have done by 'addA'  and not by a constructor. You should consider that class B has a 1 to N relation to A. So, if you want to add a new A to B you don't need a constructor of B but a member function.


> copy b into c of Type B

I suggest that naming an object of class B  'c' is just this sense of humour men need to protect individualism in a world of uniformness. ;-)

To copy array b to another instance of B you may either use the copy constructor of B

class B
{
     ....
     B(const B& anyB)
     {
          menge = anyB.menge;
          if (menge > 0)
          {
                b   = new A[menge];
                for (int i = 0; i < menge; i++)
                     b[i] = anyB.b[i];
          }
          else
                b = NULL;
           anzahl = new int;
          *anzahl = *anyB.anzahl;
     }

};

or you use a constructor as defined above:

class B
{
     ....
     B( A& a)
     {
          menge = 1;
           b   = new A(a);
          anzahl = new int;
          *anzahl = 0;
     }
};

Actually, it doesn't seem to make much sense creating a lot of copies of the same object. It's good OO practice to have singletons for any physical existing object. So, if class A represents some individuals and class B is the container for those individuals, you should have exactly one container B and a set of A's and nothing else.

Regards, Alex




It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.