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 15

Chapter 15

Polymorphism and Virtual Functions

This activity contains 10 questions.

Question 1.
Which of the following best describes what a virtual function is?

 
End of Question 1


Question 2.
Which of the following is not another term that is used for the practice of using virtual functions?

 
End of Question 2


Question 3.

What will the following code output when run?

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

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 +
		string(". Noise made: ") + getNoise();
}

string Animal::getNoise()
{
	return(string("Unknown"));
}

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

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;
}

string Cow::getNoise()
{
	return (string("Moo"));
}

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


Question 4.

In the following class definitions what will the following code output when run?

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

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

Why is "virtual" used in the Cow class for getDescription() and getNoise()?
 
End of Question 4


Question 5.

What will the following code output when run?

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

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 +
		string(". Noise made: ") + getNoise();
}

string Animal::getNoise()
{
	return(string("Unknown"));
}

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

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;
}

string Cow::getNoise()
{
	return (string("Moo"));
}

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


Question 6.

What will the following program output when run?

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

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 +
		string(". Noise made: ") + getNoise();
}

string Animal::getNoise()
{
	return(string("Unknown"));
}

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

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;
}

string Cow::getNoise()
{
	return (string("Moo"));
}

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


Question 7.

What is the output of the following program?

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

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 +
		string(". Noise made: ") + getNoise();
}

string Animal::getNoise()
{
	return(string("Unknown"));
}

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

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;
}

string Cow::getNoise()
{
	return (string("Moo"));
}

// Main
int main()
{	
	Cow *pBessie = new Cow(4,"jersey");
	Animal *pAnimal = pBessie;
	cout << pAnimal->getDescription() << endl;
	delete pAnimal;	
	return 0;
}
 
End of Question 7


Question 8.

What will the following program output? Note the cout statements in the destructors of both classes.

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

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

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

Animal::~Animal()
{
	cout << "Animal destructor" << endl;
}

// 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 +
		string(". Noise made: ") + getNoise();
}

string Animal::getNoise()
{
	return(string("Unknown"));
}

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

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

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

Cow::~Cow()
{
	cout << "Cow destructor" << endl;
}

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

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

string Cow::getNoise()
{
	return (string("Moo"));
}

// Main
int main()
{	
	Cow *pBessie = new Cow(4,"jersey");	
	Animal *pAnimal = pBessie;
	delete pAnimal;	
	return 0;
}
 
End of Question 8


Question 9.

What will the following program output? Note the cout statements in the destructors of both classes.

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

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

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

Animal::~Animal()
{
	cout << "Animal destructor" << endl;
}

// 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 +
		string(". Noise made: ") + getNoise();
}

string Animal::getNoise()
{
	return(string("Unknown"));
}

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

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

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

Cow::~Cow()
{
	cout << "Cow destructor" << endl;
}

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

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

string Cow::getNoise()
{
	return (string("Moo"));
}

// Main
int main()
{	
	Cow *pBessie = new Cow(4,"jersey");	
	Animal *pAnimal = pBessie;
	delete pAnimal;	
	return 0;
}
 
End of Question 9


Question 10.

What will the following program output?

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

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 +
		string(". Noise made: ") + getNoise();
}


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

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;
}

string Cow::getNoise()
{
	return (string("Moo"));
}

// Main
int main()
{	
	Cow bessie(4,"jersey");	
	Animal dog("dalmatian", 5);
	
	cout << bessie.getDescription() << endl;
	cout << dog.getDescription() << endl;
	return 0;
}
 
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