class containing classes

Hi

If a class contains classes.
Are those classes members or methods(well I presume they are members anyway).
If they are members, to whom are the methods from the class containing classes sending messages to. The contained classes, isn't it ? But when we deal with a class having int, strings or chars as members, the methods are sending messages to those members by changing their vallue. But what about sending messages to classes ?
What happens, actually will the members inside the classes wich are members get changed(recieve the message)?
I need some clarification on that matter.
Thanks.
simiAsked:
Who is Participating?
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.

chensuCommented:
I need some clarification on your question. What do you mean by sending messages?
0
simiAuthor Commented:
Well I tried to sound like a experienced C++ programmer wich obviously I am not. Actually I am at the very beginning.
I understood that the functions inside a class in C++ are called methods, and the variables inside the same class are called members.
When you make use of a function in order to change the value of a variable you are "sending a message to that member using a method"
My confusion, regarding a class that contains ohter classes, is:
class A
{
class B;
        public:
void a_function();
};

What is a_function doing to class B ?
0
nietodCommented:
"methods" and "sending messages" are terminology used by some other object oriented langauges.  C++ does not use that terminology.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

nietodCommented:
The way you have it written a_function is part of class A and has nothing to do with class B.    There is no way to tell what it does to/with A either.  Do you have a better code sample?
0
simiAuthor Commented:
I do not have the code. I have to write an assignment.
A virtual zoo.
A first menu asks if you are a visitor or a zoo keeper.
If you are a keeper you can add, delete and feed animals in 5 sections.Each section can hold 5 animals.
I do not continue describing the rest of the assignment.
My first assignment was to write the code in C wich I did.
Now I have to decide how I approach the matter in C++.
I thought I would declare a base class animals and 5 derived classes, one for each section.
Such a class can hold each five elements(animals), add, delete, and print those animals(actually there are a few variables involved like, name, a logo, a special feature).
I wrote the class that way, and it looks OK to me.
My problem is that reading through some guiding lines of how that program should be written I got to the point where the approach is different than the one I thought of. The solution is : declare a base class animal and 5 derived for each type of section. But declare also a class called zoo, as a class containing other classes, and there declare the animal classes as arrays.
Well here I start getting confused. What can I do to, or with those classes wich are members of another class. I can eventually declare some operators to perform operations using those classes but it does not look to me like something I can make use of.
So ,back to my question, generally, why should one declare a class that has other classes as members ?
0
shogiCommented:
Sorry but it's not the place to have solution for you home work. But I will give you some examples:

// how to use a class in an other class
#include <iostream.h>

class A
{
  public:
     A(int n=0);
     void Print() const;
       class B
       {
           public:
           B(int n=0)
               {
               valueB = n;
               }
               void Print() const
               {
                 cout << "the valueB is " << valueB << endl;
               }

         private:
           int valueB;
       } objectB;
  private:
     int valueA;
};


A::A(int n)
{
  valueA = n;
}

void A::Print() const
{
  cout << "the valueA is " << valueA << endl;
}


int main()
{
  A test(33);
  test.Print();
  test.objectB.Print();
  return 0;
}

==============================================================
==============================================================
But it's not the best because in you class B you will always
have the default value so you need in the constructor of A to
put extra parameter and call the objectB with the value look:

#include <iostream.h>

class A
{
  public:
     A(int n=0, int n2=0);
     void Print() const;
       class B
       {
           public:
           B(int n=0)
               {
               valueB = n;
               }
               void Print() const
               {
                 cout << "the valueB is " << valueB << endl;
               }

         private:
           int valueB;
       } objectB;
  private:
     int valueA;
};


A::A(int n, int n2) : objectB(n2)
{
  valueA = n;
}

void A::Print() const
{
  cout << "the valueA is " << valueA << endl;
}


int main()
{
  A test(33, 66);
  test.Print();
  test.objectB.Print();
  return 0;
}

==============================================================
==============================================================
Generally we never put the class definition in an other class, you have to put it outside, look:

#include <iostream.h>

class B
{
  public:
    B(int n=0);
    void Print() const;

   private:
      int valueB;
};

B::B(int n)
{
   valueB = n;
}

void B::Print() const
{
 cout << "the valueB is " << valueB << endl;
}

class A
{
  public:
     A(int n=0, int n2=0);
     void Print() const;

   private:
     B objectB;
     int valueA;
};


A::A(int n, int n2) : objectB(n2)
{
  valueA = n;
}

void A::Print() const
{
  cout << "the valueA is " << valueA << endl;
  objectB.Print();
}


int main()
{
  A test(33, 66);
  test.Print();
  return 0;
}

==============================================================
==============================================================
About the objetB, generally it suppose to be private :

#include <iostream.h>

class B
{
  public:
    B(int n=0);
    void Print() const;

   private:
      int valueB;
};

B::B(int n)
{
   valueB = n;
}

void B::Print() const
{
 cout << "the valueB is " << valueB << endl;
}

class A
{
  public:
     A(int n=0, int n2=0);
     void Print() const;
     B objectB;
  private:
     int valueA;
};


A::A(int n, int n2) : objectB(n2)
{
  valueA = n;
}

void A::Print() const
{
  cout << "the valueA is " << valueA << endl;
}


int main()
{
  A test(33, 66);
  test.Print();
  test.objectB.Print();
  return 0;
}
==============================================================
==============================================================

About your assignment

class A
{
};

class B1: public A
{
};

...

class B5: public A
{
};

class BIG_ONE
{
  private:
    A a[5];  // array of class A
};

but it can be

class BIG_ONE
{

  private:
    // each derived class Bx
    B1 b1;    
    B2 b2;
    B3 b3;
    B4 b4;
    B5 b5;
};


Hope that it will help you...
==============================================================

0

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
chensuCommented:
Interestngly no one answered the important question--

"So ,back to my question, generally, why should one declare a class that has other classes"

You can place classes, type definitions (typedef), and enumerated types (enum { ]) inside a class to give a scope local to the class.  By this I mean you could have

class Zoo
{

class snake
{
};
};

where a snake class is an animal class in a zoo class.  If in the same program you also had

class PlumbingTools
{
 class snake
{
}
};

where snake is a tool class of the plumbing tools.  You have two classes called snake here.  But the compiler knows that they are different because they are local to differnet classes.  If they were not local to a class, they would conflict.

Another advantage to this is that the classes (typepde, etc) can be made private or protected.  This is used for cases where the classes (etc.) are used to impliment the class, but should not be used by the code that uses the classes.  That is, it makes sure the class is used only within the class.
0
nietodCommented:
Thank you.
I got a pretty good immage of it now.
About my assignment, I really did not intend to have more involvment from anyone but the kind of help I got from you. Actually I did not post at the beginning anything about the assignment, but as I could not make clear what I wanted to ask without reffering to the assignment itself, I made known a part of it. I am really interested in C++ and I want to get good at it and not necesarly only to have good assignments. Thanks again.
0
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.