Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. What is the output of the following program? why? #include <iostream> #includ

ID: 3868107 • Letter: 1

Question

1. What is the output of the following program? why?

#include <iostream>

#include <string>

using namespace std;

class Other {

int value;

public: Other(int n): value(n) { cout << "O" << value; }

~Other() {

cout << "~O" << value;

}

}; class Base { public: Base(int i):o(i) { cout << " B "; value = i; } virtual ~Base() { cout << " ~B "; } virtual void output_value() { cout << value ;

}

private: int value; Other o;

};

class Derived : public Base {

public: Derived(int i): Base(i+1),value(i),o(i) { cout << " D "; }

~Derived() {

cout << " ~D ";

}

virtual void output_value() {

cout << value ;

}

private: int value; Other o;

};

void processAll(Base *bp) { bp->output_value(); delete bp; }

int main() {

processAll( new Base(2) );

cout << " ";

processAll( new Derived(3) );

cout << endl;

}

2.

You have had enough of this weird rainforest and decided to go back to the civilized world. Unfortunately, since you solved the water jug problem last time (see Problem #4 in Practice Midterm II), the cannibals believe that eating you will give them the wisdom you have. Thus, you are now being chased by 300 cannibals. The only way to return to the civilized world is to go through a maze. Fortunately, you stole a map of this maze from one of the cannibals, but you found that this maze is not a common maze. There are many rocks blocking the road here and there. Although you can use a bomb in your bag to smash the rocks to get through, you only have 2 bombs left. Thus, you decided to write a program to figure out whether you are able to use 2 bombs to smash 2 rocks and get to the exit. If so, then print out the moves you need to take in order to get to the exit. Given the following map (S is the start, Q is the exit, R is a Rock, W is a wall)

"SWWWWWWWWW",

"..WWWWWWWW",

"W...WWWWWW",

"W.WRWWWWWW",

"..W......W",

.WWWRW.WRW",

".WWWRW.W.W",

"RWW..W.RWW",

"WWWWWW.WRQ',

"WWWWWW...W"

Your program should output: (Directions S/E/N/W indicate South/East/North/West)

SESEES Bomb!

SEEESSSSSEEN

Bomb! E Found Exit!

#include <iostream> #include <string> using namespace std;

int direction[4][2] = { {-1,0}, {0,-1}, {1,0}, {0,1} }; string dir_str[4] = {"N","W","S","E"};

void find_path(char maze[10][11], int bx, int by, int x,int y, string path, int numBomb, bool &found );

int main() { char maze[10][11] = { "SWWWWWWWWW", "..WWWWWWWW", "W...WWWWWW", "W.WRWWWWWW", "..W......W", ".WWWRW.WRW", ".WWWRW.W.W", "RWW..W.RWW", "WWWWWW.WRQ", "WWWWWW...W" };

bool found = false; string path; find_path(maze, 10,11, 0, 0, path, 2, found); }

void find_path(char maze[10][11], int bx, int by, int x,int y, string path, int numBomb, bool &found ) {

if(found) return ;
for(int i=0;i<4;i++) {

int next_x = x + direction[i][0];

int next_y = y + direction[i][1];

if( next_x >= 0 && next_y >= 0 && next_x < bx && next_y < by) {

if( maze[ next_x ][ next_y ] == '.' ) {

maze[ next_x ][ next_y ] = 'V';

find_path(maze, bx , by , next_x, next_y, path + dir_str[i] , numBomb, found );

if ( ! found)

maze[ next_x ][ next_y ] = '.';

}

else

if( maze[ next_x ][ next_y ] == 'R' && numBomb > 0 ) {

// Smash the rocks with your bomb if you have any.

maze[ next_x ][ next_y ] = 'V';

find_path( maze, bx, by, next_x, next_y, path + dir_str[i] + " Bomb! ", numBomb-1, found );

if ( ! found) maze[ next_x ][ next_y ] = 'R';

}

else if(maze[ next_x ][ next_y ] == 'Q') {

path += dir_str[i] + " Found Exit! ";

found = true;

for(int j=0;j<path.size();j++)

cout << path[j];

}

}

}

}

Explanation / Answer

Am sorry but answer to question 1 only is provided since Question 2 is not a subpart of Question 1 (Chegg protocol).

output is:
O2 B 2 ~B ~O2 O4 B O3 D 3 ~D ~O3 ~B ~O4

processAll( new Base(2) ); will execute new Base(2) which in turn first INITIALIZES object other using constructor hence printing O2. Once Other is initialized it moves into the body of Base constructor and prints B

Now the control is transfered to the processAll function which calls the virtual function of Base class and it prints 2, and then it calls Delete B which prints ~B and it then kills Other object too printing ~O2


Now the call id processAll(new Derived(3)), and the constructor first calls Base(4) which prints O4 B (as we have seen previously) and then it calls o(3) which prints O3 and finally it moves inside the body of constructor and prints D

Now the control is with processAll() function which prints 3, and it then moves into the Destructor of Derived class which prints ~D. It then kills other objects exactly the opposite way it was created, hence printing ~O3 ~B ~O4