Classes within classes

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>
using namespace std;

class Hen {
public:
    class Nest {}
    int a;
    friend class Hen;

public:
    class Egg {}
    int b;
    friend class Nest;
Last edited on
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.
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
#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 also raises a question. I've created the nested class Nest, but Hen doesn't contain an instance of that class. Is that what the prof wanted?

Okay, for the Egg class, you can follow the same procedure.

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.
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
#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.
Ah no worries actually! I just figured it out! Thank you!
Hi,

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....
Yup! No worries Shadder! Thanks for your help, but I literally just got it myself! Thanks anyways! :)
Topic archived. No new replies allowed.