Nested Classes

Apr 26, 2012 at 11:38am
Hello Everyone, this is my first thread here and also i am new to this forum as well as the world of object oriented programming and the C++.

I recently studied about making nested classes and came across a question about the nested classes exercise in my book.

The problem:

Create a Hen class. Inside this,nest a Nest class. Inside Nest, place an Egg class. Each class should have a display() member function. In main(), create an instance of each class and call the display() function for each one.

I came up with this solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class="centertext">#include<iostream> #include <string> using namespace std; class Hen{ string name; public: class Nest{ int size; public: class Egg{ int age; public: void display(); }; void display(); }; void display(); }; void Hen::Nest::Egg::display() { cout<<"Egg display called... "<<endl <<"Egg's age is: "<<age<<endl; } void Hen::Nest::display() { cout<<"Nest display called... "<<endl <<"Size of nest is : "<<size<<endl; } void Hen::display() { cout<<"Hen display called... "<<endl <<"Hen's name is: "<<name<<endl; } int main() { Hen h; Hen::Nest n; Hen::Nest::Egg e; h.display(); n.display(); e.display(); }


I am having some questions in my mind like:

When i create a object of class Hen am i also creating objects of the class Nest and Egg implicitly?

or/and

is it possible to define just one print() function in the outermost class Hen such that it prints all the data members including the members of the classes Nest and Egg? but i know we can only print an data member if it actually exists or the object exists. Which brings me back to my first question.

Thanks to everyone in advance...
Apr 26, 2012 at 11:44am
When i create a object of class Hen am i also creating objects of the class Nest and Egg implicitly?
No. Hen, Hen::Nest and Hen::Nest::Egg are 3 separate classes with nothing in common (except that they are nested).
Apr 26, 2012 at 11:45am
Ans 1 : No once you instantiate any class, only that class object is created.

Ans 2 : Yes you can define only one print function that would work for all, as well as you can extend the functionality of the parent class function in the derived class.

You should go over here :

http://www.cplusplus.com/doc/tutorial/inheritance/

http://www.cplusplus.com/doc/tutorial/polymorphism/
Apr 26, 2012 at 11:48am
Peter87, Danishx83 Thanks so much for the replies...
Apr 26, 2012 at 11:49am
is it possible to define just one print() function in the outermost class Hen such that it prints all the data members including the members of the classes Nest and Egg? but i know we can only print an data member if it actually exists or the object exists. Which brings me back to my first question.

Only if you an instance of a class inside a class I guess:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Nest
{
private:
   int size;
public:
   void Display();
};

class Hen
{
private:
   string name;
   Nest n;
public:
   void Display();
};

void Nest::Display()
{
   cout << "Size of nest is " << size << endl;
}

void Hen::Display()
{
   cout << "Hen's name is << " name << endl;
   n.Display();
}
Apr 26, 2012 at 11:51am
your code should be more like

1
2
3
4
5
6
7
8
9
class EGG{
  //code
}
class NEST : public EGG{
  //code
}
class HEN : public NEST, EGG{
  //code
}


NEST objects has and can access all public data and functions of EGG class
HEN objects has and can access all public data and functions of EGG and NEST class

I think reading more on inheritance should make it clearer
Apr 26, 2012 at 12:07pm
Danishx83, i cannot understand this code. is it another method to declare nested classes? i found it a little confusing cause i have never seen it before.

I have not studied inheritance and polymorphism yet and will wait till its explained later in my book (Thinking in C++), but I truly appreciate your help and valuable suggestion to me. :)
Last edited on Apr 26, 2012 at 12:07pm
Apr 26, 2012 at 12:09pm

No. Hen, Hen::Nest and Hen::Nest::Egg are 3 separate classes with nothing in common (except that they are nested).


Because of that, you can't access nonstatic members of an enclosing class, without explicitly providing an object. Honestly, this behaviour makes nested classes in C++ plain useless. They do not add anything above what can be accomplished by regular classes.
Apr 26, 2012 at 12:12pm
Thank you rapidcoder, so nested classes are just like the nested structures?
Apr 26, 2012 at 12:25pm
@Danishx83,

Let's not get nested classes mixed up with inheritance.

Different things for different needs.
Apr 26, 2012 at 12:30pm
iHutch105, was the code written by danishx83 that i could not understand, is about inheritance?
Last edited on Apr 26, 2012 at 12:30pm
Apr 26, 2012 at 12:39pm
Yeah, it was.
Apr 26, 2012 at 12:42pm
oh.. Thanks for telling me abt it :)
Topic archived. No new replies allowed.