CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jul 2013
    Posts
    3

    Need help in This Code plzz

    >Write a program in which you create a Hen class. Inside this class, nest a Nest class. Inside Nest, place an Egg class. Each class should have a display() member function. For each class, create a constructor and a destructor that prints an appropriate message when it is called. In main(), create an instance of each class using new and call the display() function for each one. After calling display(), free the storage using delete.
    And i came up with this code can anyone help me if im missing something its realy comfusing question. and also the last line about Delete function im confusing about as well plzz help.

    #include<iostream>
    #include <string>
    using namespace std;

    // Create an abstract data type that represents a hen.
    class Hen
    {
    string name;
    public:
    Hen (void);
    ~Hen (void);
    class Nest{
    int size;
    public:
    class Egg{
    int age;
    public:
    void display();
    };
    void display();
    };
    void display();
    };

    //display the age of the egg.
    void Hen::Nest::Egg:isplay()
    {
    cout<<"Egg display called... "<<endl<<"Egg's age is : "<<age<<endl;
    }
    // display the size of the nest.
    void Hen::Nest:isplay()
    {
    cout<<"Nest display called... "<<endl<<"Size of nest is : "<<size<<endl;
    }
    // display the name of the hen
    void Hen:isplay()
    {
    cout<<"Hen display called... "<<endl<<"Hen's name is: "<<name<<endl;
    }

    Hen::Hen()
    {
    cout << "A new hen has appeared!" << endl;
    }

    Hen::~Hen()
    {
    cout << "The hen died " << endl;
    }


    int main()
    {
    Hen h;
    Hen::Nest n;
    Hen::Nest::Egg e;
    h.display();
    n.display();
    e.display();

    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Need help in This Code plzz

    Please, when you post code format your code usng indents and use code tags. Go advanced, select code and then click '#'
    Last edited by 2kaud; July 21st, 2013 at 07:49 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Need help in This Code plzz

    You created a constructor for the hen class, but it does not initialise its members. You have not created a constructor/destructor for nest or egg. You also have no way of setting the class members. The assignment asks for instances of each class to be created using new which you don't do so have no storage to release using delete. To show some of what I mean, the below is some suggested changes to the hen class

    Code:
    class Hen
    {
    	string name;
    
    public:
    	Hen (string na = "default hen");
    Code:
    Hen::Hen(string na) : name(na)
    {
    	cout << "A new hen has appeared!" << endl;
    	cout << "called " << name << endl;
    }
    Code:
    int main()
    {
    Hen h("guru");
    
    Hen *p = new Hen;
    delete p;
    ...
    Last edited by 2kaud; July 21st, 2013 at 07:49 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Need help in This Code plzz

    Quote Originally Posted by s_zar View Post
    Write a program in which you create a Hen class. Inside this class, nest a Nest class. Inside Nest, place an Egg class. Each class should have a display() member function. For each class, create a constructor and a destructor that prints an appropriate message when it is called. In main(), create an instance of each class using new and call the display() function for each one. After calling display(), free the storage using delete.
    Assignments like this make me question the sanity of the teachers giving them...

    From a pure reality POV... So there is an egg in a nest... and this nest is in a hen. Wait, what, the hen ate the nest (along with the egg in it) ?


    What is the point of this ?
    Who designs code like this ? Why are these unrelated classes defined inside eachother ? I could understand if you have the 3 separate classes and a relation between them, but the definition of this assignment is a joke.

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Need help in This Code plzz

    as to the OP...

    The assignment says nothing about a member "name" or a nest size or an egg age.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Need help in This Code plzz

    Quote Originally Posted by OReubens View Post
    Assignments like this make me question the sanity of the teachers giving them...

    From a pure reality POV... So there is an egg in a nest... and this nest is in a hen. Wait, what, the hen ate the nest (along with the egg in it) ?


    What is the point of this ?
    Who designs code like this ? Why are these unrelated classes defined inside eachother ? I could understand if you have the 3 separate classes and a relation between them, but the definition of this assignment is a joke.
    I was thinking the same thing. I've been doing this for decades and never written anything so goofy. A student's going to come out of class thinking that's how you design programs. It's nuts.

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Need help in This Code plzz

    Quote Originally Posted by OReubens View Post
    Assignments like this make me question the sanity of the teachers giving them...

    From a pure reality POV... So there is an egg in a nest... and this nest is in a hen. Wait, what, the hen ate the nest (along with the egg in it) ?


    What is the point of this ?
    Who designs code like this ? Why are these unrelated classes defined inside eachother ? I could understand if you have the 3 separate classes and a relation between them, but the definition of this assignment is a joke.
    As an ex c++ teacher, I have to agree with you. The 'point?' of these assignments is to test the students understanding of the syntax of the language - it has nothing to do with what would be encountered in 'real' c++ programming. The teacher may have no choice in what/how they teach if they are hired to teach an already written course - in which case I have some sympathy for the teacher. When I was teaching, I was supposed to teach just this sort of 'rubbish'. I tried to move my teaching examples more towards 'real' programming and introduce strings/vectors etc early on but was reprimanded by higher authority for not following 'the course'. I left.

    This brings us back to that old chestnut - qui docet doctores - and the design of c++ teaching material. Can't the c++ professional community produce some teaching aides as to how modern c++ should be taught?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Need help in This Code plzz

    Quote Originally Posted by 2kaud
    This brings us back to that old chestnut - qui docet doctores - and the design of c++ teaching material. Can't the c++ professional community produce some teaching aides as to how modern c++ should be taught?
    We already have at least two: Stroustrup's essay on Learning Standard C++ as a New Language and Koenig and Moo's Accelerated C++.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Need help in This Code plzz

    Quote Originally Posted by 2kaud View Post
    The 'point?' of these assignments is to test the students understanding of the syntax of the language
    I know what the point of "an" assignment is. THe point in this particular assignment isn't obvious.


    In part becasue the formulation of the assignment is "silly" rather than something you'd actually be asked to write from a half decent analyst.
    And in part because the formulation makes no sense "in the real world".

    it has nothing to do with what would be encountered in 'real' c++ programming.
    Then what is the point of teaching if it is not to prepare a student for "the real" C++ programming jobs?

    Is it a good idea to fill the heads of our youth with silliness that upon embarking on a real job, the first few weeks (months) is spent doing nothing but trying to weed out hard ingrained bad programming practices, design principles and ideas ?

    The teacher may have no choice in what/how they teach if they are hired to teach an already written course - in which case I have some sympathy for the teacher. When I was teaching, I was supposed to teach just this sort of 'rubbish'. I tried to move my teaching examples more towards 'real' programming and introduce strings/vectors etc early on but was reprimanded by higher authority for not following 'the course'. I left.
    I can understand they're following a book, in which case, I redirect my sanity check towards the author of said book, and towards the body of educational authority that decided such nonsense book was "a good idea" in the first place.
    Even then, I still blame the teacher in part for following the book without proper personal input.

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Need help in This Code plzz

    Agreed When I was teaching and I tried to give 'personal input' of 20 years of programming it was 'frowned upon' - so I left as I couldn't agree with what was being demanded of me. I was supposed to follow a course written by a so-called 'expert' programmer who in practice wasn't. He was a 'language lawyer' who knew every last little detail of the syntax of the language as it then was but when challenged hadn't a clue how to write a decent program for the 'real world'. He got into teaching because it was the only job he could get and tried to fool everybody into thinking he was an 'expert' because of his syntax knowledge. Its guys like him that give the teaching profession a bad reputation. I wouldn't have hired him as a junior - never mind an expert - programmer. IMO we need some kind of accreditation for those that teach programming.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured