Hey everyone! I have a question as to how we delete the storage after calling the display() function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
class Hen {
public:
class Nest {}
int a;
friendclass Hen;
public:
class Egg {}
int b;
friendclass Nest;
You can do this iteratively. Start with just a Hen class and nothing else:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
using std::string;
using std::cout;
class Hen {
public:
Hen() { cout << "creating Hen " << this << '\n'; }
~Hen() {cout << "destroy Hen " << this << '\n'; }
void display() { cout << "this is Hen " << this << '\n'; }
};
int
main()
{
Hen h;
h.display();
return 0;
}
I'm printing the address (this) in each of the 3 methods. Here's what it looks like when I run it. Note that the address will be different on your computer:
creating Hen 0xffffcbff
this is Hen 0xffffcbff
destroy Hen 0xffffcbff
So far so good. Now add the Nest class inside class Hen. You can cut & paste and find/replace to do this. Along the way I re-read the directions and realized that the instances in main should be allocated with new, so I've fixed that.
You can do this iteratively. Start with just a Hen class and nothing else:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
using std::string;
using std::cout;
class Hen {
public:
Hen() { cout << "creating Hen " << this << '\n'; }
~Hen() {cout << "destroy Hen " << this << '\n'; }
void display() { cout << "this is Hen " << this << '\n'; }
};
int
main()
{
Hen h;
h.display();
return 0;
}
I'm printing the address (this) in each of the 3 methods. Here's what it looks like when I run it. Note that the address will be different on your computer:
creating Hen 0xffffcbff
this is Hen 0xffffcbff
destroy Hen 0xffffcbff
So far so good. Now add the Nest class inside class Hen. You can cut & paste and find/replace to do this. Along the way I re-read the directions and realized that the instances in main should be allocated with new, so I've fixed that.
#include <iostream>
using std::string;
using std::cout;
class Hen {
public:
Hen() { cout << "creating Hen " << this << '\n'; }
~Hen() {cout << "destroy Hen " << this << '\n'; }
void display() { cout << "this is Hen " << this << '\n'; }
class Nest {
public:
Nest() { cout << "creating Nest " << this << '\n'; }
~Nest() {cout << "destroy Nest " << this << '\n'; }
void display() { cout << "this is Nest " << this << '\n'; }
};
};
int
main()
{
Hen *h = new Hen;
h->display();
delete h;
Hen::Nest *n = new Hen::Nest;
n->display();
delete n;
return 0;
}
This raises a question: did the professor want you to define and instance of Nest inside Hen? I haven't done that. You might want to check the assignment or ask him/her.
For the Egg class you can follow the same procedure: nest it inside Hen::Nest.
Basically there's a class Hen inside hen there's a class Nest inside which there's a class Egg....
Much like a movie in which there's dream inside a dream (you guess the name )
Now regarding the deleting problem, the destructor will automatically delete it, you don't need to worry, but if you do you can always define your own desctructor....