0% found this document useful (0 votes)
10 views2 pages

MyString Constructors and Destructors Explained

ADS

Uploaded by

maitrungtuan2002
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

MyString Constructors and Destructors Explained

ADS

Uploaded by

maitrungtuan2002
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Answer the questions in Exercise A in the following table and post it into the D2L

Program output and its order Your explanation (why and where is the cause for this output)
constructor with int argument is It is called at line 12 in exAmain when: Mystring c = 3.
called.
Constructor MyString::Mystring (int n) is
called

default constructor is called. It is called at line 18 when Mystring x[2]. Constructor


default constructor is called.
MyString::Mystring() is called

constructor with char* argument It is called at line 22 when Mystring* z = new


is called.
Mystring(“4”). Constructor
MyString::Mystring(const char *s) is called

copy constructor is called. It is called at line 24 when x[0].append(*z).append(x[1]).


copy constructor is called.
The append function creates the Mystring object “other”
twice in its time. Constructor
Mystring::Mystring(const Mystring&
source)is called twice in this line as both calls to append
creates a new one

destructor is called. It is called once line 24 finishes. When the functions return,
destructor is called.
the newly created Mystring objects go out of scope and the
destructor is called for both times

copy constructor is called. It is called at line 26 when Mystring mars = x[0]. This is a
copy constructor instead of an assignment operator because
this is the declaration of the Mystring object mars.
Constructor Mystring::Mystring(const
Mystring& source)is called

assignment operator called. It is called at line 28 when x[1] = x[0]. This is an


assignment operator because x[1] has already been created
and is now being assigned. Function Mystring&
Mystring::operator =(const Mystring& S)
is called

constructor with char* argument It is called at line 30 when Mystring jupiter("White") and
is called.
constructor with char* argument
line 32 when ar[0] = new Mystring ("Yellow"). Constructor
is called. MyString::Mystring(const char *s) is called

destructor is called. It is called once line 33 finishes. When the block of code
destructor is called.
destructor is called.
ends, x[0], x[1], mars, and jupiter all get out of scope and
destructor is called. the destructor is called to destroy them. The Mystring
destructor is called. object that z points too is also destroyed because after z
goes out of scope, nothing points to it and it’s destroyed
constructor with char* argument It is called at line 39 when Mystring d = "Green".
is called.
Constructor MyString::Mystring(const char
*s) is called

Program terminated successfully. It is printed from line 41 when cout << "\nProgram
terminated successfully." <<endl;
destructor is called. It is called once line 42 finishes. When the scope ends,
destructor is called
Mystring objects ar[2] and d are destroyed

You might also like