Solved

Problem: Class B should take values from class A

Posted on 2003-11-27
6
286 Views
Last Modified: 2010-04-01
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?
0
Comment
Question by:lorcan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 24

Expert Comment

by:shivsa
ID: 9832928
yes u can A's functions just like u use any other function. remember to put reference to A class.
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 250 total points
ID: 9832971
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


0
 
LVL 3

Expert Comment

by:monkesdb
ID: 9833820
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9835088
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
0
 
LVL 1

Author Comment

by:lorcan
ID: 9838696
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

0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 250 total points
ID: 9840425
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




0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
I could not build boost code, 10 116
need help ...get stuck at middle of coding ...need to submit by Sunday C++ 1 54
Outlook 13 101
C++ Code Issue 4 48
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question