Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Problem: Class B should take values from class A

Posted on 2003-11-27
6
Medium Priority
?
298 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 500 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 500 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

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!

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

730 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