Content Frame
Note for screen reader users: There is text between the form elements on this page. To be sure that you do not miss any text, use item by item navigation methods, rather than tabbing from form element to form element.
Skip Breadcrumb Navigation
Home  arrow Student Resources  arrow Quizzes  arrow Chapter 14

Chapter 14

Inheritance

This activity contains 10 questions.

Question 1.
If class C1 inherits from class C2, which of the following is not a term that can be used to describe class C2?

 
End of Question 1


Question 2.

Given the following class definitions:

// Animal class
class Animal
{
private:
	string type;
	int age;
	string intToString(int i);
public:
	Animal();
	Animal(string newType, int newAge);
	string getDescription();
};

// Cow class
class Cow : public Animal
{
private:
	string breed;
public:
	Cow();
	Cow(int newAge, string newBreed);
	string getBreed();
};

What variables/methods are available for an object of type Cow? e.g. if we declare:

Cow bessie(3,"Holstein");   

What can we access after the dot? E.g. can we access bessie.getBreed(), etc?
 
End of Question 2


Question 3.

Given the following class definitions:

// Animal class
class Animal
{
private:
	string type;
	int age;
	string intToString(int i);
public:
	Animal();
	Animal(string newType, int newAge);
	string getDescription();
};

// Cow class
class Cow : public Animal
{
private:
	string breed;
public:
	Cow();
	Cow(int newAge, string newBreed);
	void printDescription();
};

What will the following implementation output?

void Cow:: printDescription ()
{
	cout << "Type: " << type << " Breed " <<
		breed << " Age: " << age << endl;
}
 
End of Question 3


Question 4.

Given the following class definitions:

// Animal class
class Animal
{
protected:
	string type;
	int age;
	string intToString(int i);
public:
	Animal();
	Animal(string newType, int newAge);
	string getDescription();
};

// Cow class
class Cow : public Animal
{
private:
	string breed;
public:
	Cow();
	Cow(int newAge, string newBreed);
	void printDescription();
};

What will the following implementation output?

void Cow:: printDescription ()
{
	cout << "Type: " << type << " Breed " <<
		breed << " Age: " << age << endl;
}
 
End of Question 4


Question 5.

Given the following class definitions:

// Animal class
class Animal
{
protected:
	string type;
	int age;
private:
	string intToString(int i);
public:
	Animal();
	Animal(string newType, int newAge);
	string getDescription();
};

// Cow class
class Cow : public Animal
{
private:
	string breed;
public:
	Cow();
	Cow(int newAge, string newBreed);
	void printDescription();
};

What variables and methods are inherited from the Animal class that we can directly access from code written inside the Cow class? (e.g. inside the implementation of printDescription()).
 
End of Question 5


Question 6.

Given the following definition and implementation of the Cow and Animal classes, what will be the output of the program?

// Animal class
class Animal
{
private:
	string type;
	int age;
	string intToString(int i);
public:
	Animal();
	Animal(string newType, int newAge);
	string getDescription();
};

Animal::Animal()
{
	age=0; type="Unknown";
}

Animal::Animal(string newType, int newAge)
{
	age= newAge; type = newType;
}

// Converts an integer to a string
string Animal::intToString(int i)
{
	stringstream ss;
	ss << i;
	return (ss.str());
}

string Animal::getDescription()
{
	string sAge = intToString(age);
	return string("Animal type: ") + type +
		string(". Age: ") + sAge;
}

// Cow class
class Cow : public Animal
{
private:
	string breed;
public:
	Cow();
	Cow(int newAge, string newBreed);
	string getBreed();
	string getDescription();
};

Cow::Cow() : Animal(), breed("")
{
}

Cow::Cow(int newAge, string newBreed)
  :Animal("cow", newAge), breed(newBreed) 
{
}

string Cow::getBreed()
{
	return breed;
}

string Cow::getDescription()
{
	return Animal::getDescription() + 
		". Breed: " + breed;
}

// Main
int main()
{
	Animal dog("dog",3);
	Cow bessie(4, "holstein");
	cout << dog.getDescription() << endl;
	cout << bessie.getDescription() << endl;
	return 0;
}
 
End of Question 6


Question 7.

Given the following class definitions:

// Animal class
class Animal
{
private:
	string type;
	int age;
	string intToString(int i);
public:
	Animal();
	Animal(string newType, int newAge);
	string getDescription();
};

// Cow class
class Cow : public Animal
{
private:
	string breed;
public:
	Cow();
	Cow(int newAge, string newBreed);
	string getDescription();
};

If we define a variable of type Cow, as:  

Cow bessie(3,"Holstein");   

What can we access the getDescription method defined in Animal, not in Cow?
 
End of Question 7


Question 8.
Out of constructors, destructors, and the overloaded assignment operator, which are inherited in a derived class?

 
End of Question 8


Question 9.

Given the following class definitions:

// Animal class
class Animal
{
private:
	string type;
	int age;
	string intToString(int i);
public:
	Animal();
	Animal(string newType, int newAge);
	virtual string getDescription();
};

Animal::Animal()
{
	age=0; type="Unknown";
}

Animal::Animal(string newType, int newAge)
{
	age= newAge; type = newType;
}

// Converts an integer to a string
string Animal::intToString(int i)
{
	stringstream ss;
	ss << i;
	return (ss.str());
}

/*
string Animal::getDescription()
{
	string sAge = intToString(age);
	return string("Animal type: ") + type +
		string(". Age: ") + sAge;
}
*/

// Cow class
class Cow : public Animal
{
private:
	string breed;
public:
	Cow();
	Cow(int newAge, string newBreed);
	string getBreed();
	string getDescription();
};

Cow::Cow() : Animal(), breed("")
{
}

Cow::Cow(int newAge, string newBreed)
  :Animal("cow", newAge), breed(newBreed) 
{
}

string Cow::getBreed()
{
	return breed;
}

string Cow::getDescription()
{
	return Animal::getDescription() + 
		". Breed: " + breed;
}

What will the following code output?

	Animal a;
	Cow bessie(4, "holstein");
	a = bessie;
	cout << a.getDescription() << endl;
 
End of Question 9


Question 10.

Given the following class definitions:

// Animal class
class Animal
{
private:
	string type;
	int age;
	string intToString(int i);
public:
	Animal();
	Animal(string newType, int newAge);
	virtual string getDescription();
};

Animal::Animal()
{
	age=0; type="Unknown";
}

Animal::Animal(string newType, int newAge)
{
	age= newAge; type = newType;
}

// Converts an integer to a string
string Animal::intToString(int i)
{
	stringstream ss;
	ss << i;
	return (ss.str());
}

/*
string Animal::getDescription()
{
	string sAge = intToString(age);
	return string("Animal type: ") + type +
		string(". Age: ") + sAge;
}
*/

// Cow class
class Cow : public Animal
{
private:
	string breed;
public:
	Cow();
	Cow(int newAge, string newBreed);
	string getBreed();
	string getDescription();
};

Cow::Cow() : Animal(), breed("")
{
}

Cow::Cow(int newAge, string newBreed)
  :Animal("cow", newAge), breed(newBreed) 
{
}

string Cow::getBreed()
{
	return breed;
}

string Cow::getDescription()
{
	return Animal::getDescription() + 
		". Breed: " + breed;
}

What will the following code output?

	Animal mr_ed("cow",4);
	Cow cow5;
	cow5 = mr_ed;
	cout << cow5.getDescription() << endl;
 
End of Question 10





Pearson Copyright © 1995 - 2010 Pearson Education . All rights reserved. Pearson Addison Wesley is an imprint of Pearson .
Legal Notice | Privacy Policy | Permissions

Return to the Top of this Page