Question: The skeleton code contains the same MyString implementation that we learned in class, with the following additions: - Makefile defines a macro called BASIC4TRACE. If

The skeleton code contains the same MyString implementation that we

learned in class, with the following additions:

- Makefile defines a macro called BASIC4TRACE. If you look at mystring.cpp, you will see that the basic 4 (i.e., constructor, destructor, copy constructor, and copy assignment) output a log message to stderr when that macro is defined.

- test4.cpp was added. It is a little test program that passes a couple of objects to a function and receives an object as a return value. Here is test4.cpp with line numbers:

1 // test4.cpp 2 3 #include "mystring.h" 4 5 static MyString add(MyString a, MyString b) 6 { 7 MyString t(" and "); 8 return a + t + b; 9 } 10 11 int main() 12 { 13 MyString x("one"); 14 MyString y("two"); 15 16 MyString z = add(x, y); 17 cout << z << endl; 18 return 0; 19 }

Your job is to understand the sequence of basic 4 calls during the execution of test4 program. When you build and run test4 using the Makefile provided, you will see the log output of the basic 4 that looks something like this:

BASIC4TRACE: (0x7f69ac0)->MyString(const char *) [1] 13,constructor,x BASIC4TRACE: (0x7f69ad0)->MyString(const char *) [2] 14,constructor,y BASIC4TRACE: (0x7f69b00)->MyString(const MyString&) [3] BASIC4TRACE: (0x7f69af0)->MyString(const MyString&) [4] BASIC4TRACE: (0x7f69a70)->MyString(const char *) [5] BASIC4TRACE: op+(const MyString&, const MyString&) [6] entering operator+ BASIC4TRACE: (0x7f69a20)->MyString() [7] BASIC4TRACE: (0x7f69a80)->MyString(const MyString&) [8] 8,copy constructor, u1 from return temp BASIC4TRACE: (0x7f69a20)->~MyString() [9] BASIC4TRACE: op+(const MyString&, const MyString&) [10] entering operator+ BASIC4TRACE: (0x7f69a20)->MyString() [11] BASIC4TRACE: (0x7f69a90)->MyString(const MyString&) [12] BASIC4TRACE: (0x7f69a20)->~MyString() [13] BASIC4TRACE: (0x7f69b10)->MyString(const MyString&) [14] BASIC4TRACE: (0x7f69a90)->~MyString() [15] BASIC4TRACE: (0x7f69a80)->~MyString() [16] BASIC4TRACE: (0x7f69a70)->~MyString() [17] BASIC4TRACE: (0x7f69ae0)->MyString(const MyString&) [18] BASIC4TRACE: (0x7f69b10)->~MyString() [19] BASIC4TRACE: (0x7f69af0)->~MyString() [20] BASIC4TRACE: (0x7f69b00)->~MyString() [21] one and two [22] cout << z << endl; BASIC4TRACE: (0x7f69ae0)->~MyString() [23] BASIC4TRACE: (0x7f69ad0)->~MyString() [24] BASIC4TRACE: (0x7f69ac0)->~MyString() [25]

Answer the following questions in your README.txt:

(a) For each line of the BASIC4TRACE output, write the following information.

- The line number in test4.cpp where the output is being produced.

- Which basic 4 call is producing the output line.

- The variable name (from either test4.cpp or mystring.cpp) for the object at the address in parenthesis. If the object is a unnamed temporary object, assign a name uN (i.e. u1,u2,u3,...) when the object is created and also write which expression (from test4.cpp or mystring.cpp code) the object was created from. (The line for u1 is done for you as an example.) For subsequent lines referring to the same object, you don't have to write the expression. Just write the uN name. As you can see, the answers for a few lines are already filled in for you as examples.

Please prefix your answers with "[N]", where [N] is the line number in the BASIC4TRACE output, as shown in the example above. You MUST follow this format in order to get credit for this part because it will be auto-graded by a script which will look for "[N]".

(b) Change the add() function in test4.cpp as follows:

static MyString add(const MyString& a, const MyString& b)

Explain the changes in the BASIC4TRACE output.

(c) The Makefile uses a compiler flag: -fno-elide-constructors. What does this flag do? (See g++ man page.) Rebuild test4 without the flag and examine the BASIC4TRACE output. Describe the changes from (b).

// test4.cpp

#include "mystring.h"

static MyString add(MyString a, MyString b)

{

MyString t(" and ");

return a + t + b;

}

int main()

{

MyString x("one");

MyString y("two");

MyString z = add(x, y);

cout << z << endl;

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!