10 Quick Questions. NO EXPLAINATION NECESSARY just the answer only. Will rate if
ID: 3607717 • 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
31. T or F? The single statement shown below relies on the left-to-right associativity of comma operator.
if ( showDetails ) cout << INDENTION << "Construction of ", Print(), cout << endl;
32. Consider the statement below (taken from the PARENT overloaded assignment operator): Under what circumstances is ( this != &RHS ) false? Why “worry about” that particular circumstance? Answer T or F? Self-assignment is a problem whenever data members of the objects being assigned are pointers to dynamically allocated memory.
if ( this != &RHS )
33. The following statement (an explicit reference to the PARENT destructor) was removed from the overloaded assignment operator in the PARENT class to resolve an un-diagnosable run-time issue. T or F? An explicit reference to the destructor would never be appropriate. Hint How would you avoid a memory leak if the PARENT class had one or more a dynamically-allocated data members?
this->~PARENT();
34. The assignment operator for the COMPONENT class is not explicitly provided by Dr. Hanna, but is instead the “free” one provided by default by the compiler. The default assignment operator and the corresponding “free”/default copy constructor both implement memberwise assignment/copy semantics (also known as shallow-copy semantics). T or F? The “free” assignment operator “works” for this Problem because memberwise copying is sufficient for making copies of COMPONENT objects.
35. The formal parameter RHS in the prototype shown below (taken from the COMPONENT class) is passed-by-reference and T or F? it always must be.
friend ostream &operator<<(ostream &OUT,const COMPONENT &RHS);
36. What is the value of the CHILD1 data member type of the object child1 defined in the statement show below?
A: CHILD1TYPE B: PARENTTYPE C: NOTYPE D: SUBCHILD1TYPE E: none of these
CHILD1 child1;
37. T or F? The COMPONENT copy constructor function definition (shown below as Code Segment #1) could be rewritten more simply using member initializers as shown below as Code Segment #2.
Code Segment #1
//---------------------------------------------------
COMPONENT::COMPONENT(const COMPONENT &RHS)
//---------------------------------------------------
{
x = RHS.x;
y = RHS.y;
if ( showDetails ) cout << INDENTION << "Copy construction of " << *this << endl;
}
Code Segment #2
//---------------------------------------------------
COMPONENT::COMPONENT(const COMPONENT &RHS): y(y),x(x)
//---------------------------------------------------
{
if ( showDetails ) cout << INDENTION << "Copy construction of " << *this << endl;
}
38. Temporarily change the function header for the function template DisplayObject(). Doing so, changes the formal parameter object from a by-value parameter to a by-reference parameter. Now, rebuild and execute Problem.cpp. T or F? Some of the copy construction and destruction you can see in the Problem.cpp Expected Output have disappeared because the copy constructor is no longer being implicitly referenced.
//---------------------------------------------------
template<class TYPE>
void DisplayObject(TYPE &object)
//---------------------------------------------------
39. When you add the utility function shown below as a private member function of the PARENT class, T or F? all PARENT member functions can legally reference Utility().
//---------------------------------------------------
void Utility()
//---------------------------------------------------
{
cout << "Utility called by ", Print(), cout << endl;
}
40. (Continuing 39) T or F? Both the CHILD1 class and CHILD2 class will inherit Utility() but neither can access it.
//---------------------------------------------------
// 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
ANSWERS
31) false
32) false
33) false
34) true
35)true
36) option e (non of the above)
37) false
38) false
39) true
40) false
As mentioned no need of explanation i have provided only answers
All questions are answered
Thank you for the oppurtunity
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.