Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

class containing classes

Posted on 1998-03-09
8
Medium Priority
?
263 Views
Last Modified: 2012-05-04
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.
0
Comment
Question by:simi
[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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 23

Expert Comment

by:chensu
ID: 1183289
I need some clarification on your question. What do you mean by sending messages?
0
 

Author Comment

by:simi
ID: 1183290
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
 
LVL 22

Expert Comment

by:nietod
ID: 1183291
"methods" and "sending messages" are terminology used by some other object oriented langauges.  C++ does not use that terminology.
0
Industry Leaders: 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 22

Expert Comment

by:nietod
ID: 1183292
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
 

Author Comment

by:simi
ID: 1183293
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
 
LVL 2

Accepted Solution

by:
shogi earned 80 total points
ID: 1183294
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
 
LVL 23

Expert Comment

by:chensu
ID: 1183295
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
 
LVL 22

Expert Comment

by:nietod
ID: 1183296
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.

715 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