CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Oop

Hybrid View

  1. #1
    Join Date
    Jan 2013
    Posts
    5

    Oop

    Hello , first post here

    I have been studying for my programming exam lately and I have encountered some problems that I hope you could explain to me. I have been doing exams from the last years to practise and here is the portion of the code that I would love some help with:

    Code:
    class Sentence
    {
       string sent;
    
    public:
       Sentence(const string &s):s(sent)
       {
         cout << "Constructor Sentence: " << sent << endl;
       }
    
       void print()
      {
        cout << sent ;
      }
    
       ~Sentece()
      {
        cout << "Destructor Sentence: " << sent << endl;
      }
    };
    Code:
    class Document
    {
      Sentence *sentences;
      int n;
      int max;
    
    public:
      Document(int max)
      {
        this->max=max;
        n=0;
        cout << "Constructor Document: " << max << endl;
      }
    
      void NewSentence(const string &c)
      {
        if(n<max)
        {
          Sentence s(c);
          sentences[n++]=s;
        }
      }
    
      void print()
      {
        for(int i=0;i<n;i++)
        {
          sentences[i].print();
        }
        cout << endl;
      }
    
      ~Document()
      {
        cout << "Destructor Document: " << n << endl;
        delete []sentences;
      }
    };
    Code:
      void main()
      {
        Document doc(10);
        doc.NewSentence("abc. ");
        doc.NewSentence("def. ");
        doc.NewSentence("ghi. ");
        doc.print();
      }
    In the exam, they clearly state that there is an important problem with the Document(int max) constructor and to solve the problem by doing necessary changes to it. I have no idea what's wrong with it, in fact, I compiled the program and the output was this:

    Constructor Document: 10
    Constructor Sentence: abc

    Then, the programs shuts down for some reason. Is this related to the constructor?

    Thanks for your time,
    Pancake

  2. #2
    Join Date
    May 2009
    Posts
    2,413

    Re: Oop

    Quote Originally Posted by Pancake View Post
    Is this related to the constructor?
    The Document class acts as if there exists an array of Sentence (with length max) assigned to the sentences variable. This array could be allocated in the constructor but isn't.

    So the sentences variable holds garbage and when its used in NewSentence some arbitrary part of memory will be overwritten and the program may crash.

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

    Re: Oop

    In addition to nuzzle's point about sentences being uninitialized, I'll point out that just having a Sentence pointer instead of a proper container class such as a list, and a constructor that arbitrarily constrains the number of sentences is a pretty goofy design.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Oop

    Quote Originally Posted by Pancake View Post
    Code:
      void main()
      {
        Document doc(10);
        doc.NewSentence("abc. ");
        doc.NewSentence("def. ");
        doc.NewSentence("ghi. ");
        doc.print();
      }
    In the exam, they clearly state that there is an important problem with the Document(int max) constructor and to solve the problem by doing necessary changes to it.
    Your teacher taught you that main() returns void? Get another teacher. The main() function returns an int.

    I can break your class easily with a two-line main() program.
    Code:
    int main()
    {
       Document doc(10);
       Document doc2 = doc;
    }
    You note that at the end, you're deleting the same pointer value twice, once for doc and another for doc2. This leads to undefined behaviour, possibly a crash or some sort of diagnostic. How you fix the problem -- that is for later.

    Regards,

    Paul McKenzie

Tags for this Thread

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