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
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
1. inheritancea form of software reuse in which you create a class that absorbs an existing classs capabilities, then customizes or enhances them. Which one of the following is un-reused software?
A: PARENT class implementation
B: CHILD1 class definition
C: CHILD2 class implementation
D: PARENT::GetType() function
E: PARENT::SetShowDetail() function
2. T or F? The PARENT class is a base class (also known as a superclass).
3. T or F? The CHILD1 class is a derived class (also known as a subclass).
4. A derived class represents a more specialized group of objects. Whats so special about the CHILD1 class?
A: type B: name C: component D: weight E: none of these
5. T or F? The PARENT /CHILD1 is-a relationship is based on public inheritance.
6. (Continuing 5) Fact Every CHILD1 object is-a PARENT object. T or F? Every PARENT object is-a CHILD1 object.
7. What is the nature of the class relationship between the classes PARENT and COMPONENT?
A: is-a B: has-a C: is-not-a D: has-not-a E: C and D
8. T or F? The type definition shown below is an example of an enumerated type.
enum TYPE
{
NOTYPE=0,
PARENTTYPE=1,
CHILD1TYPE=2,
CHILD2TYPE=3,
CHILD3TYPE=4,
SUBCHILD1TYPE=5
};
9. Fact The Problem.cpp function DisplayObject() function prototyped below is a function template that is instantiated T or F? to create exactly three function-template specializations.
//---------------------------------------------------
template
void DisplayObject(TYPE object)
//---------------------------------------------------
10. (Continuing 9) The passed-by-value parameter, TYPE object, used by the function DisplayObject() T or F? exercises the copy constructors for each of the classes PARENT, CHILD1, and CHILD2.
//---------------------------------------------------
// 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
//---------------------------------------------------
// Chapter #11 Problem
// Problem.cpp
//---------------------------------------------------
#include
#include
#include
#include
using namespace std;
#include ".\Problem.h"
#include ".\Parent.h"
#include ".\Child1.h"
#include ".\Component.h"
#include ".\Child2.h"
//---------------------------------------------------
template
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 );
}
//---------------------------------------------------
// 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
#include
#include
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
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
#include
#include
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
#include
#include
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
#include
#include
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 << ")";
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
