Solved

Problem: Class B should take values from class A

Posted on 2003-11-27
6
248 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
6 Comments
 
LVL 24

Expert Comment

by:shivsa
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 10

Expert Comment

by:Sys_Prog
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now