• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/20

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

20 Cards in this Set

  • Front
  • Back
1. Is the following program legal (assuming appropriate #include and using directives
are added)?
void showEmployeeData(const Employee object);
int main( )
{
HourlyEmployee joe("Mighty Joe",
"123-45-6789", 20.50, 40);
SalariedEmployee boss("Mr. Big Shot",
"987-65-4321", 10500.50);
showEmployeeData(joe);
showEmployeeData(boss);
return 0;
}
void showEmployeeData(const Employee object)
{
cout << "Name: " << object.getName( ) << endl;
cout << "Social Security Number: "
<< object.getSsn( ) << endl;
}
1. Yes. You can plug in an object of a derived class for a parameter of the base class type.
An HourlyEmployee is an Employee. A SalariedEmployee is an Employee.
2. Give a definition for a class SmartBut that is a derived class of the base class Smart
given below. Do not bother with #include directives or namespace details.
class Smart
{
public:
Smart( );
void printAnswer( ) const;
protected:
int a;
int b;
};
This class should have an additional data field, crazy, of type bool; one additional
member function that takes no arguments and returns a value of type bool; and
suitable constructors. The new function is named isCrazy. You do not need to give
any implementations, just the class definition.
2. class SmartBut : public Smart
{
public:
SmartBut( );
SmartBut(int newA, int newB, bool newCrazy);
bool isCrazy( ) const;
private:
bool crazy;
};
3. Is the following a legal definition of the member function isCrazy in the derived class
SmartBut discussed in Self-Test Exercise 2? Explain your answer. (Remember, the
question asks if it is legal, not if it is a sensible definition.)
bool SmartBut::isCrazy( ) const
{
if (a > b)
return crazy;
else
return true;
}
3. It is legal because a and b are marked protected in the base class Smart and so they can
be accessed by name in a derived class. If a and b had instead been marked private,
then this would be illegal.
4. The class SalariedEmployee inherits both of the functions getName and print-
Check (among other things) from the base class Employee, yet only the function declaration
for the function printCheck is given in the definition of the class
SalariedEmployee. Why isn’t the function declaration for the function getName
given in the definition of SalariedEmployee?
4. The declaration for the function getName is not given in the definition of
SalariedEmployee because it is not redefined in the class SalariedEmployee. It is
inherited unchanged from the base class Employee.
5. Give a definition for a class TitledEmployee that is a derived class of the base class
SalariedEmployee given in Display 14.4. The class TitledEmployee has one
additional member variable of type string called title. It also has two additional
member functions: getTitle, which takes no arguments and returns a string, and
setTitle, which is a void function that takes one argument of type string. It also
redefines the member function setName. You do not need to give any implementations,
just the class definition. However, do give all needed #include directives and
all using namespace directives. Place the class TitledEmployee in the namespace
SavitchEmployees.
5. #include <iostream>
#include "salariedemployee.h"
using namespace std;
namespace SavitchEmployees
{
class TitledEmployee : public SalariedEmployee
{
public:
TitledEmployee( );
TitledEmployee(string theName, string theTitle,
string theSsn, double theSalary);
string getTitle( ) const;
void setTitle(string theTitle);
void setName(string theName);
private:
string title;
};
}//SavitchEmployees
6. Give the definitions of the constructors for the class TitledEmployee that you gave
as the answer to Self-Test Exercise 5. Also, give the redefinition of the member function
setName. The function setName should insert the title into the name. Do not
bother with #include directives or namespace details.
6. namespace SavitchEmployees
{
TitledEmployee::TitledEmployee( )
: SalariedEmployee( ), title("No title yet")
{
//deliberately empty
}
TitledEmployee::TitledEmployee(string theName,
string theTitle,
string theSsn, double theSalary)
: SalariedEmployee(theName, theSsn, theSalary),
title(theTitle)
{
//deliberately empty
}
void TitledEmployee::setName(string theName)
{
Employee::setName(title + theName);
}
}//SavitchEmployees
7. You know that an overloaded assignment operator and a copy constructor are not
inherited. Does this mean that if you do not define an overloaded assignment operator
or a copy constructor for a derived class, then that derived class will have no
assignment operator and no copy constructor?
7. No. If you do not define an overloaded assignment operator or a copy constructor for a
derived class, then a default assignment operator and a default copy constructor will be
defined for the derived class. However, if the class involves pointers, dynamic arrays, or
other dynamic data, then it is almost certain that neither the default assignment operator
nor the default copy constructor will behave as you want them to.
8. Suppose
Child
is a class derived from the class
Parent
and that the class
Grandchild
is a class derived from the class
Child
. This question is concerned
with the constructors and destructors for the three classes
Parent
,
Child
, and
Grandchild
. When a constructor for the class
Grandchild
is invoked, what
constructors are invoked and in what order? When the destructor for the class
Grandchild
is invoked, what destructors are invoked and in what order?
8. The constructors are called in the following order: first Parent, then Child, and finally
Grandchild. The destructors are called in the reverse order: first Grandchild, then
Child, and finally Parent.
9. Is the following alternative definition of the default constructor for the class
PFArrayDBak
(Displays 14.10 and 14.11) legal? (The invocation of the constructor
from the base class has been omitted.) Explain your answer.
PFArrayDBak::PFArrayDBak( ) : usedB(0)
{
b = new double[capacity];
}
9. Yes, it is legal and has the same meaning as the definition given in Display 14.11. If no
base class constructor is called, then the default constructor for the base class is called
automatically.
10. Suppose you define a function with a parameter of type PFArrayD. Can you plug in
an object of the class PFArrayDBak as an argument for this function?
10. Yes. An object of a derived class is also an object of its base class. A PFArrayDBak is a
PFArrayD.
11. Would the following be legal for the definition of a member function to add to the
class Employee (Display 14.1)? (Remember, the question is whether it is legal, not
whether it is sensible.)
void Employee::doStuff( )
{
Employee object("Joe", "123-45-6789");
cout << "Hello " << object.name << endl;
}
11. Yes, it is legal. One reason you might think it illegal is that name is a private member
variable. However, object is in the class Employee, which is the class that is being
defined, so we have access to all member variables of all objects of the class Employee.
provides a tool for code reuse by deriving one class from another, adding
features to the derived class.
Inheritance
inherit the members of the base class, and may add members.
Derived class objects
If a member variable is private in a base class,
then it cannot be accessed by name in
a derived class.
member functions are not inherited.
Private
A member function may be redefined in a derived class so that it performs differently
from how it performs in the base class.
The declaration for a redefined member
function must be given in the class definition of the derived class, even though
it is the same as in the base class.
member in the base class can be accessed by name in the definition of a
member function of a publicly derived class.
protected
is not inherited. However, the assignment operator
of a base class can be used in the definition of an overloaded assignment operator
of a derived class.
An overloaded assignment operator
are not inherited. However, a constructor of a base class can be used
in the definition of a constructor for a derived class.
Constructors
you cannot access a private member variable or private member
function in the definition or implementation of a derived class. There is a classification
of member variables and functions that allows them to be accessed by name in a
derived class, but not anyplace else, such as in some class that is not a derived class. If
you use the qualifier ___, rather than private or public, before a member variable
or member function of a class, then for any class or function other than a derived
class the effect is the same as if the member variable were labeled private; however, in
a derived class the variable can be accessed by name.
protected