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.
class="centertext">#include<iostream>
#include <string>
usingnamespace 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.
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.
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:
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
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. :)
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.