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

#include <iostream> using namespace std; // Here are class and function definiti

ID: 3904645 • Letter: #

Question

#include <iostream>

using namespace std;

// Here are class and function definitions used in program:

// This program does run! Indicate output (if any) for each numbered line.

// A class definition

class G // base class, thinking of Geometric objects

{

public:

     G() { cout << "makeG ";}

     ~G() { cout << "Ggone " << endl;}

     virtual void fun() const = 0;

protected:

     int data;

};

// another class, thinking like a Box (rectangle)

class B : public G

{

public:

     B(int i) : data(i) { cout << "makeB " << endl;}

     ~B() { cout << "Bgone ";}

     void fun() const { cout << data * data << endl;}

     // so the B fun does a square (data*data)

private:

     int data;

};

// and another class, Circular reasoning

class C : public G

{

public:

     C() { cout << "makeC " << endl; data = 8;}

     ~C() { cout << "Cgone ";}

     void fun() const { cout << data << endl;}

};

// some functions using above classes

void gee(G& one) {one.fun();}

void bee(B& two) {cout << "bee works" << endl;}

void sea(C& the)

{

     C *see = new C();

     cout << "sea works ";

     delete see;

}

// The main driver: Indicate output (if any) for each numbered line.

void main()

{

     G *g;               // 1.

     B *b = new B(2);    // 2.

     C *c = new C();     // 3.

     b->fun();      // 4.

     c->fun();      // 5.

     g = b;         // 6.

     g->fun();      // 7.

     b->fun();      // 8.

     c->fun();      // 9.

     gee(*b);       // 10.

     bee(*b);       // 11.

     sea(*c);       // 12.

// one of these casts could halt execution, thus the try-catch

// your answers below must clearly indicate where the exception is thrown:

     try

     {

          static_cast<B*>(g)->fun();   // 13.

          static_cast<C*>(g)->fun();   // 14.

          dynamic_cast<B*>(g)->fun();   // 15.

          dynamic_cast<C*>(g)->fun();   // 16.

     }

     catch (...) // catches any thrown exception in Visual Studio

     {

          cout << "can't do that" << endl;

     }

     // Some pointer math (start HEX with AAAAAAAA)

     cout << g << endl; // 17.

     cout << b << endl; // 18.

     cout << c << endl; // 19.

     c++;

     cout << c << endl; // 20.

    

}

Explanation / Answer

G *g; // 1.

Explaination:

g is base pointer to class G

B *b = new B(2); // 2.

Explaination:

b is pointer to object B(2)

display to screeen:

makeG makeB   

[Constructor of G and B are called]

C *c = new C(); // 3.

Explaination:

c is pointer to object C(2)

display to screeen:

makeG makeC   

[Constructor of G and B are called]

b->fun(); // 4.

Explaination:

call fun to class B (b points to B(2) )

display to screeen:

4

[function is data* data = 2 *2 =4 ]

c->fun(); // 5.

Explaination:

call fun to class C (c points to C() )

display to screeen:

8

[function is data= 8 ]

g = b; // 6.

Explaination:

g points to object B(2)

g->fun(); // 7.

Explaination:

the function under class B is run

call fun to class B (b points to B(2) )

display to screeen:

4

[function is data* data = 2 *2 =4 ]

b->fun(); // 8.

Explaination:

b points the function under class B is run

call fun to class B (b points to B(2) )

display to screeen:

4

[function is data* data = 2 *2 =4 ]

c->fun(); // 9.

Explaination:

call fun to class C (c points to C() )

call fun to class C (c points to C() )

display to screeen:

8

function is data= 8 ]

  

gee(*b); // 10.

Explaination:   

the fun under class B is run

call fun to class B (b points to B(2) )

display to screeen:

4

[function is data* data = 2 *2 =4 ]

bee(*b); // 11.

Explaination:

display to screeen:

"bee works"

sea(*c); // 12.

Explaination:  

[new object for C() is created, so constructuor will be called fo G and C

also once scope completes destructor is called

]

display to screeen:

makeG makeC

sea works Cgone Ggone

// one of these casts could halt execution, thus the try-catch

// your answers below must clearly indicate where the exception is thrown:

try

{

static_cast<B*>(g)->fun(); // 13.

Explaination:

g points to object b

the function under object b of Class B is run

display to screeen:

4

  

static_cast<C*>(g)->fun(); // 14.

Explaination:   

g points to object b

the function under object b of Class B is run

display to screeen:

4

dynamic_cast<B*>(g)->fun(); // 15.

Explaination:

g points to object b

the function under object b of Class B is run

display to screeen:

4

dynamic_cast<C*>(g)->fun(); // 16.

Explaination:

g points to object b and dynamic_cast results in exception

Use dynamic_cast when casting from a base class type to a derived class type.

It checks that the object being cast is actually of the derived class type

and returns a null pointer if the object is not of the desired type

(unless you're casting to a reference type -- then it throws a bad_cast exception).

  

}

catch (...) // catches any thrown exception in Visual Studio

{

cout << "can't do that" << endl;

}

// Some pointer math (start HEX with AAAAAAAA)

cout << g << endl; // 17.

0x55ac0ea8ce80

cout << b << endl; // 18.

0x55ac0ea8ce80

Explaination:

both b and g point to same object ie object under B(2)

cout << c << endl; // 19.

0x55ac0ea8ce80  

c to same object under C()

c++;

cout << c << endl; // 20.

0x55ac0ea8ceb0

Explaination:

incerement pointer to next object of C