Question: C++ with comments Question 1: Read the complete assignment before beginning. This is an exercise to get practice using the debugging tool. #include #include using

C++ with comments

Question 1:

Read the complete assignment before beginning. This is an exercise to get practice using the debugging tool. #include #include using namespace std; class Data { public: Data() { _values.push_back( 1 ); _values.push_back( 2 ); } int getSum() { int total = -1; for (vector::iterator itr = _values.begin(), end = _values.end(); itr != end; ++itr ) { total += *itr; } return total; } int getCount() { return _values.size(); } private: vector _values; }; double compute_average( int sum, int count ) { return sum / count; } int main() { Data d; cout << compute_average( d.getSum(), d.getCount() ) << endl; return 0; } 1) Enter the above program without changes, compile, and execute. Does it work correctly? 2) Set a breakpoint on the line where function compute_average is called. In this simple program the function is only called once so putting the breakpoint inside the function itself would make more sense. But what if the function were called many times, from different locations? In this example, better to isolate the call that has the issue and trace the function with those specific values. a) Use the snipping tool to capture an image showing the breakpoint is set and the line is about to be executed. b) Run (F5) the program. c) When the debugger stops, step into (F11) function getCount. d) Rather than hit step over (F10) multiple times to walk through the function, step out (shift+F11) to return to the function call location. e) Repeat steps b & c for function getSum. f) Finish (F5) running the program 3) Sometimes, such as with the parameters to function compute_average, we dont store function results but we still wish to see the values. Repeat step 2, but after 2c and 2d, look at tab Autos for the function name to see the value returned by the function. a) This technique is better than (re-)writing your code as avg = compute_average( d.getSum(), d.getCount() ); cout << avg << endl; just so you can see the value returned by compute_average(). b) Use the snipping tool to capture images of tab Autos showing the values returned by each of the three functions 4) Sometimes we realize during debugging/testing that a value is initialized incorrectly. Variable values can be modified during execution. a) Repeat step 2 through 2c b) In autos, click the arrow to left of d to expand the class c) Click the arrow to left of _values to expand the vector d) Use the snipping tool to capture images showing the value of variable _values before and after the next step e) Click on [1], type 4 to change its value from 2 to 4 and hit enter f) Continue with step 2d and finish g) This trick can also be used when a wrong value has been identified (possibly as the return from a function). The value can be changed and then continue with program execution to verify remaining code works correctly. 5) Code can be modified during the debugging process. Warning: be sure to save your source code before debugging in case the change doesnt work. If it does work, save it again immediately so you dont lose the change you just made! a) Repeat step 2 through 2c, then step into function getSum. b) Use the snipping tool to capture images showing the code before and after the next step c) Change the initialization value of total to zero d) Step out of the function and step into function compute_average e) Change the code to cast count as a double in the return statement f) Step out of the function g) Execute one statement h) Verify the output is correct and use the snipping tool to capture an image of the output 6) As you are stepping through your code (F10), you may find youve gone too far and missed seeing a value. Rather than starting over, right-click on the line youd like to execute next and select Set Next Statement. Execution will continue from that point. CAUTION: program execution point will change but values of variables will remain as they currently are so perform this operation very carefully and selectively! a) Execute your program up to the return statement in function getSum b) Use snipping tool to capture an image showing local variable values c) Set next statement to be first line of for loop and continue execution up to the return statement d) Use snipping tool to capture an image showing local variable values

Question 2: Here we are getting practice creating classes and using inheritance. No test plan is necessary. Each class must have its own .cpp and .h files. 1st class: has one private attribute, color, which is a string, and another which is a pointer to an int. One constructor, with a string parameter with default parameter value Purple. The constructor will create a new int and assign it to the pointer variable, then give it value -13. The constructor displays message Class created with color ccc, replacing ccc with the color variable. Destructor, which displays message Class destroyed with color ccc and number nnn, replacing ccc with the color variable and nnn with the value the pointer points to, and deleting the pointer. Also create appropriate get and set methods for color. Youll have to write a copy constructor for the class as well as overloading operator=. Have the copy constructor increase the value the new pointer points to by 1 (only the copy will increase, not the original) and display message Class created with color ccc and number nnn, replacing ccc with the color variable and nnn with the value the pointer points to. 2nd class: inherits public from 1st class. It has private attributes of length and width, both int. One constructor, with int parameters with default parameter values of 3 and 5. The constructor displays message Class created with color ccc, length lll, and width www, replacing ccc, lll, and www with the appropriate variable values. Destructor, which displays message Class destroyed with color ccc, length lll, and width www, replacing ccc, lll, and www with the appropriate variable values. Create appropriate get and set methods for the two attributes. Create a PrintMe method that prints ccc object with lll x www (area = aaa) replacing ccc, lll, and www with the appropriate variable values and aaa with length multiplied by width. 3rd class: inherits public from 2nd class. It has private int attribute of height. One constructor, with an int parameter with default parameter value of 2. The constructor displays message Class created with color ccc, length lll, width www, and height hhh, replacing ccc, lll, www, and hhh with the appropriate variable values. Destructor, which displays message Class destroyed with color ccc, length lll, width www, and height hhh, replacing ccc, lll, www, and hhh with the appropriate variable values. Create appropriate get and set methods for the attribute. Create a PrintMe method that prints ccc object with lll x www x hhh (volume = vvv) replacing ccc, lll, www, and hhh with the appropriate variable values and vvv with length multiplied by width multiplied by height. Create a function called Test that accepts a parameter to the 3rd class. Inside the function, declare an instance of 3rd class and assign the parameter to it. Call the PrintMe method of both the parameter and local variable. Assignment: declare multiple instances of each object (1st class, 2nd class, and 3rd class), with and without initialization values (to show default parameter values work). Declare two more instances of 3rd class, called thing1 and thing2, using default values. Call PrintMe method of every object declared of type 2nd class and 3rd class. Call Test, passing thing2 as parameter. Call PrintMe of thing1 and thing2. Put a breakpoint on the last } of your program so you can see final destructor messages. Capture a snippet of output and put it in your report along with all source code. Go back to all classes and change private attributes to protected. What inheritance type should be used now in 2nd class and 3rd class so they keep working? Put a comment in your report as question 2A describing whether you need to change inheritance from public to something else. Modify your classes to directly access inherited attributes where possible. Re-run instructions listed in paragraph Assignment, above, using the new versions of your classes.

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!