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: 3607721 • 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

41. (Continuing 39) T or F? No application program will be able to access the private access function Utility().

42. (Continuing 39) When you change the access modifier for Utility() from private to protected, T or F? both the CHILD1 class and CHILD2 class will inherit Utility() and both can access it.

43. (Continuing 39 and 42) T or F? No application program can access the protected access function Utility().

44. (Continuing 39) When you change the access modifier for Utility() from private to public, T or F? both the CHILD1 class and the CHILD2 class will inherit Utility() and now both can access it.

45. (Continuing 39 and 44) T or F? No application program can access the public access function Utility().

46. T or F? The COMPONENT class is a member of the class hierarchy rooted by the PARENT class.

47. T or F? The subclass CHILD1 publically inherits from its superclass PARENT.

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

class CHILD1: public PARENT

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

48. Fact The CHILD1 constructor function header (shown below) explicitly references its parent class constructor PARENT() in the header’s member initializer list. T or F? When the code segment PARENT(type,name) is removed from the member initializer list, the PARENT default constructor will be implicitly referenced with the default values NOTTYPE and DEFAULTNAME for the type and name data members, respectively.

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

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

   PARENT(type,name),component(component)

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

49. The code segment Print() shown in the statement below (taken from the CHILD1 class overloaded operator=() member function) T or F? can legally be rewritten this->CHILD1::Print().

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

50. Why does the CHILD2 class overloaded operator=() member function include the statement shown below? T or F? The answer is found on page 425 of the text book, “Another use of the this pointer is to enable cascaded member-function calls—that is, invoking multiple functions in the same statement...”

return( *this );

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

// 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

41. True.
42. True.
43. True
44. True.
45. False
46. False
47. True
48. True
49. True
50. True