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

10 Quick Questions. NO EXPLAINATION NECESSARY just the answer only. Will rate if

ID: 3607720 • Letter: 1

Question

10 Quick Questions. NO EXPLAINATION NECESSARY just the answer only. Will rate if fully complete.

Unless explicitly stated otherwise, every one of the following questions concern only the code found in the files Problem.cpp, Problem.h, Parent.h, Parent.cpp, Child1.h, Child1.cpp, Child2.h, Child2.cpp, Component.h, and Component.cpp

11. The assignment statement used by the function DisplayObject() (shown below) T or F? exercises the overloaded assignment operators for each of the classes PARENT, CHILD1, and CHILD2.

copyOfObject = object;

12. The reference to the Print() function in the function DisplayObject() (shown below) T or F? exercises the Print() member functions for each of the classes PARENT, CHILD1, and CHILD2.

copyOfObject.Print(); cout << endl;

13. The reference to the GetShowDetails() function in the function DisplayObject() (shown below) T or F? exercises the GetShowDetails() function defined in the class PARENT and inherited by the PARENT subclasses CHILD1 and CHILD2.

if ( object.GetShowDetails() ) cout << "------------------------ "

                                    << "End of DisplayObject() ";

14. T or F? The two references to the function SetShowDetails() shown below are references to exactly the same function.

PARENT::SetShowDetails(true);

COMPONENT::SetShowDetails(false);

15. The static data member showDetails defined in the PARENT class is inherited by both subclasses CHILD1 and CHILD2: Which of the following is true about showDetails? Hint Study the effect that the statement shown below has on the detailed output produced by Problem.cpp.

A: Every PARENT, CHILD1, or CHILD2 object “owns” a distinct showDetails data member.

B: Each PARENT, CHILD1, or CHILD2 class “owns” a distinct showDetails data member.

C: There is only one showDetails data member “co-owned” by the classes PARENT, CHILD1, and CHILD2.

D: The showDetails data member “owned” by the classes PARENT, CHILD1, and CHILD2 classes are “shared” with the class COMPONENT.

E: none of these

pchild2A->SetShowDetails(false);

16. The static data type of objects[2] is (PARENT *). What is its dynamic data type?

A: PARENT B: PARENT * C: CHILD1 * D: CHILD2 E: CHILD2 *

PARENT *objects[] =

{

   &parent1,

   &child1A,

   pchild2A

};

17. T or F? The Print() function referenced by the Problem.cpp statement shown below is always the Print() function defined in the PARENT class. Hint Study the detailed output produced by Problem.cpp.

cout << "Correct? ", objects[i]->Print(), cout << endl;

18. T or F? The GetType() function referenced by the Problem.cpp statement shown below is always the GetType() function defined in the PARENT class.

switch ( objects[i]->GetType() )

19. (Continuing 18) GetType() is a member function of the PARENT class T or F? that is inherited by both classes CHILD1 and CHILD2.

20. T or F? The Print() function references in the Problem.cpp statements shown below are always references to the Print() function defined in the PARENT class. Hint Study the detailed output produced by Problem.cpp.

case PARENTTYPE:

   objects[i]->PARENT::Print();

   break;

case CHILD1TYPE:

   (*((CHILD1 *) objects[i])).CHILD1::Print();

   break;

case CHILD2TYPE:

   ((CHILD2 *) objects[i])->CHILD2::Print();

   // *OR* ((CHILD2 *) objects[i])->Print();, right?

   break;

//---------------------------------------------------

// Dr. Art Hanna

// Chapter #11 Problem

// Problem.h

//---------------------------------------------------

#ifndef PROBLEM_H

#define PROBLEM_H

enum TYPE

{

   NOTYPE=0,

   PARENTTYPE=1,

   CHILD1TYPE=2,

   CHILD2TYPE=3,

   CHILD3TYPE=4,

   SUBCHILD1TYPE=5

};

#define DEFAULTNAME "[Default]"

#define DEFAULTCOMPONENT COMPONENT(0,0)

#define DEFAULTDESCRIPTION "Default description"

#define DEFAULTPATTERN 0

#define DEFAULTWEIGHT 0.00

#define INDENTION "   "

#endif

//---------------------------------------------------

// Dr. Art Hanna

// Chapter #11 Problem

// Problem.cpp

//---------------------------------------------------

#include <iostream>

#include <iomanip>

#include <cstdlib>

#include <string>

using namespace std;

#include ".Problem.h"

#include ".Parent.h"

#include ".Child1.h"

#include ".Component.h"

#include ".Child2.h"

//---------------------------------------------------

template<class TYPE>

void DisplayObject(TYPE object)

//---------------------------------------------------

{

   if ( object.GetShowDetails() ) cout << " Start of DisplayObject() "

                                       << "------------------------ ";

   TYPE copyOfObject;

   copyOfObject = object;

   copyOfObject.Print(); cout << endl;

   if ( object.GetShowDetails() ) cout << "------------------------ "

                                       << "End of DisplayObject() ";

}

//---------------------------------------------------

int main()

//---------------------------------------------------

{

   cout << "Start of main() ";

   PARENT::SetShowDetails(true);

   PARENT parent1(PARENTTYPE,"parent1");

   DisplayObject(parent1);

   CHILD1 child1A(CHILD1TYPE,"child1A",COMPONENT(1,1));

   CHILD2 *pchild2A = new CHILD2(CHILD2TYPE,"child2A","a child2 object");

   COMPONENT::SetShowDetails(false);

   pchild2A->SetShowDetails(false);

   DisplayObject(child1A);

   DisplayObject(*pchild2A);

   child1A.SetShowDetails(true);

   PARENT *objects[] =

   {

      &parent1,

      &child1A,

      pchild2A

   };

   cout << " Start of for (int i = 0; i <= 2; i++) loop ";

   cout << "------------------------------------------ ";

   for (int i = 0; i <= 2; i++)

   {

      cout << "Correct? ", objects[i]->Print(), cout << endl;

      cout << "         ";

      switch ( objects[i]->GetType() )

      {

         case NOTYPE:

            cout << "Should not happen!!! ";

            break;

         case PARENTTYPE:

            objects[i]->PARENT::Print();

            break;

         case CHILD1TYPE:

            (*((CHILD1 *) objects[i])).CHILD1::Print();

            break;

         case CHILD2TYPE:

            ((CHILD2 *) objects[i])->CHILD2::Print();

            // *OR* ((CHILD2 *) objects[i])->Print();, right?

            break;

         default:

            cout << "Should not happen!!! ";

            break;

      }

      cout << endl;

   }

   cout << "------------------------------------------ ";

   cout << "End of for (int i = 0; i <= 2; i++) loop ";

   delete pchild2A;

   cout << " End of main() ";

   system("PAUSE");

   return( 0 );

}

//---------------------------------------------------

// Dr. Art Hanna

// Chapter #11 Problem

// Parent.h

//---------------------------------------------------

#ifndef PARENT_H

#define PARENT_H

#include ".Problem.h"

//---------------------------------------------------

class PARENT

//---------------------------------------------------

{

protected:

   static bool showDetails;

public:

   static bool GetShowDetails()          { return( showDetails ); }

   static void SetShowDetails(bool TOrF) { showDetails = TOrF;    }

protected:

   TYPE type;

   string name;

public:

   PARENT(TYPE type = NOTYPE,string name = DEFAULTNAME);

   PARENT(const PARENT &RHS);

   ~PARENT();

   PARENT &operator=(PARENT &RHS);

   TYPE GetType() { return( type ); }

   void Print();

};

#endif

//---------------------------------------------------

// Dr. Art Hanna

// Chapter #11 Problem

// Parent.cpp

//---------------------------------------------------

#include <iostream>

#include <iomanip>

#include <string>

using namespace std;

#include ".Problem.h"

#include ".Parent.h"

bool PARENT::showDetails = false;

//---------------------------------------------------

PARENT::PARENT(TYPE type,string name):

   type(type),name(name)

//---------------------------------------------------

{

   if ( showDetails ) cout << INDENTION << "Construction of ", Print(), cout << endl;

}

//---------------------------------------------------

PARENT::PARENT(const PARENT &RHS):

   type(RHS.type),name(RHS.name)

//---------------------------------------------------

{

   if ( showDetails ) cout << INDENTION << "Copy construction of ", Print(), cout << endl;

}

//---------------------------------------------------

PARENT::~PARENT()

//---------------------------------------------------

{

   if ( showDetails ) cout << INDENTION << "Destruction of ", Print(), cout << endl;

}

//---------------------------------------------------

PARENT &PARENT::operator=(PARENT &RHS)

//---------------------------------------------------

{

   if ( this != &RHS )

   {

      type = RHS.type;

      name = RHS.name;

      if ( showDetails ) cout << INDENTION << "Assignment of ", Print(), cout << endl;

   }

   return( *this );

}

//---------------------------------------------------

void PARENT::Print()

//---------------------------------------------------

{

   cout << "PARENT(" << type << "," << name << ")";

}

//---------------------------------------------------

// Dr. Art Hanna

// Chapter #11 Problem

// Component.h

//---------------------------------------------------

#ifndef COMPONENT_H

#define COMPONENT_H

#include <iostream>

using namespace std;

#include ".Problem.h"

//---------------------------------------------------

class COMPONENT

//---------------------------------------------------

{

protected:

   int x;

   int y;

protected:

   static bool showDetails;

public:

   static bool GetShowDetails()          { return( showDetails ); }

   static void SetShowDetails(bool TOrF) { showDetails = TOrF;    }

public:

   COMPONENT(int x,int y);

   COMPONENT(const COMPONENT &RHS);

   ~COMPONENT();

//friend:

   friend ostream &operator<<(ostream &OUT,const COMPONENT &RHS);

};

#endif

//---------------------------------------------------

// Dr. Art Hanna

// Chapter #11 Problem

// Component.cpp

//---------------------------------------------------

#include <iostream>

#include <iomanip>

#include <string>

using namespace std;

#include ".Problem.h"

#include ".Component.h"

bool COMPONENT::showDetails = true;

//---------------------------------------------------

COMPONENT::COMPONENT(int x,int y):

   x(x),y(y)

//---------------------------------------------------

{

   if ( showDetails ) cout << INDENTION << "Construction of " << *this << endl;

}

//---------------------------------------------------

COMPONENT::COMPONENT(const COMPONENT &RHS)

//---------------------------------------------------

{

   x = RHS.x;

   y = RHS.y;

   if ( showDetails ) cout << INDENTION << "Copy construction of " << *this << endl;

}

//---------------------------------------------------

COMPONENT::~COMPONENT()

//---------------------------------------------------

{

   if ( showDetails ) cout << INDENTION << "Destruction of " << *this << endl;

}

//---------------------------------------------------

/*friend*/ostream &operator<<(ostream &OUT,const COMPONENT &RHS)

//---------------------------------------------------

{

   OUT << "COMPONENT(" << RHS.x << "," << RHS.y << ")";

   return( OUT );

}

//---------------------------------------------------

// Dr. Art Hanna

// Chapter #11 Problem

// Child1.h

//---------------------------------------------------

#ifndef CHILD1_H

#define CHILD1_H

#include ".Problem.h"

#include ".Parent.h"

#include ".Component.h"

//---------------------------------------------------

class CHILD1: public PARENT

//---------------------------------------------------

{

protected:

   COMPONENT component;

public:

   CHILD1(TYPE type = NOTYPE,string name = DEFAULTNAME,COMPONENT component = DEFAULTCOMPONENT);

   CHILD1(const CHILD1 &RHS);

   ~CHILD1();

   CHILD1 &operator=(CHILD1 &RHS);

   void Print();

};

#endif

//---------------------------------------------------

// Dr. Art Hanna

// Chapter #11 Problem

// Child1.cpp

//---------------------------------------------------

#include <iostream>

#include <iomanip>

#include <string>

using namespace std;

#include ".Problem.h"

#include ".Parent.h"

#include ".Child1.h"

#include ".Component.h"

//---------------------------------------------------

CHILD1::CHILD1(TYPE type,string name,COMPONENT component):

   PARENT(type,name),component(component)

//---------------------------------------------------

{

   if ( showDetails ) cout << INDENTION << "Construction of ", Print(), cout << endl;

}

//---------------------------------------------------

CHILD1::CHILD1(const CHILD1 &RHS):

   PARENT(RHS.type,RHS.name),component(RHS.component)

//---------------------------------------------------

{

   if ( showDetails ) cout << INDENTION << "Copy construction of ", Print(), cout << endl;

}

//---------------------------------------------------

CHILD1::~CHILD1()

//---------------------------------------------------

{

   if ( showDetails ) cout << INDENTION << "Destruction of ", Print(), cout << endl;

}

//---------------------------------------------------

CHILD1 &CHILD1::operator=(CHILD1 &RHS)

//---------------------------------------------------

{

   if ( this != &RHS )

   {

      type = RHS.type;

      name = RHS.name;

      component = RHS.component;

      if ( showDetails ) cout << INDENTION << "Assignment of ", Print(), cout << endl;

   }

   return( *this );

}

//---------------------------------------------------

void CHILD1::Print()

//---------------------------------------------------

{

   cout << "CHILD1("; PARENT::Print(); cout << "," << component << ")";

}

//---------------------------------------------------

// Dr. Art Hanna

// Chapter #11 Problem

// Child2.h

//---------------------------------------------------

#ifndef CHILD2_H

#define CHILD2_H

#include ".Problem.h"

#include ".Parent.h"

//---------------------------------------------------

class CHILD2: public PARENT

//---------------------------------------------------

{

protected:

   string description;

public:

   CHILD2(TYPE type = NOTYPE,string name = DEFAULTNAME,string description = DEFAULTDESCRIPTION);

   CHILD2(const CHILD2 &RHS);

   ~CHILD2();

   CHILD2 &operator=(CHILD2 &RHS);

   void Print();

};

#endif

//---------------------------------------------------

// Dr. Art Hanna

// Chapter #11 Problem

// Child2.cpp

//---------------------------------------------------

#include <iostream>

#include <iomanip>

#include <string>

using namespace std;

#include ".Problem.h"

#include ".Parent.h"

#include ".Child2.h"

//---------------------------------------------------

CHILD2::CHILD2(TYPE type,string name,string description):

   PARENT(type,name),description(description)

//---------------------------------------------------

{

   if ( showDetails ) cout << INDENTION << "Construction of ", Print(), cout << endl;

}

//---------------------------------------------------

CHILD2::CHILD2(const CHILD2 &RHS):

   PARENT(RHS.type,RHS.name),description(RHS.description)

//---------------------------------------------------

{

   if ( showDetails ) cout << INDENTION << "Copy construction of ", Print(), cout << endl;

}

//---------------------------------------------------

CHILD2::~CHILD2()

//---------------------------------------------------

{

   if ( showDetails ) cout << INDENTION << "Destruction of ", Print(), cout << endl;

}

//---------------------------------------------------

CHILD2 &CHILD2::operator=(CHILD2 &RHS)

//---------------------------------------------------

{

   if ( this != &RHS )

   {

      type = RHS.type;

      name = RHS.name;

      description = RHS.description;

      if ( showDetails ) cout << INDENTION << "Assignment of ", Print(), cout << endl;

   }

   return( *this );

}

//---------------------------------------------------

void CHILD2::Print()

//---------------------------------------------------

{

   cout << "CHILD2("; PARENT::Print(); cout << "," << description << ")";

}

Explanation / Answer

11. True

12. False

13. False

14. False

15. C

16. E

17. True

18. False

19. True

20. False