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; } };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:Code:void main() { Document doc(10); doc.NewSentence("abc. "); doc.NewSentence("def. "); doc.NewSentence("ghi. "); doc.print(); }
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





Reply With Quote
