You are on page 1of 6

CS 2432 Introduction to Object Oriented

Programming
Lab 6 ~ week 11

Polymorphism

Objectives:

In this lab, students will:

● Revise polymorphism theory

● Analyse given code samples that employ polymorphism

● Use operator overloading concepts to extend class implementations to support

suitable operators
A bit of biology :)

A story of chameleons and how they assume many forms...

Scientists at the University of Cambridge call chameleons "just phenomenal," but say
it's a "myth" that they change color to camouflage themselves. Their skin is covered with
several layers of very special cells called chromatophores that respond to chemicals
from the nervous system and bloodstream. Inside these cells are tiny sacs containing
color and when the signal comes, this color is released and spreads throughout the
chromatophores. They only have four shades to work with -- yellow, red, blue and
brown -- but like artists, chameleons mix colors to produce other colors, like red and
yellow to produce orange….

Polymorphism in nature? Food for thought...what other examples c


Exercises:

1. State whether each of the following is true or false. If false, explain why.

a. The + operator cannot be defined as a non member

b. All virtual functions in an abstract base class must be declared as pure

virtual functions.

c. An object of an abstract class can be used to support dynamic

polymorphism

d. Referring to a derived-class object with a base-class handle is dangerous.

e. A class is made abstract by declaring that class virtual .

f. If a base class declares a pure virtual function, a derived class must

implement that function to become a concrete class.

g. Polymorphic programming can eliminate the need for switch logic.

2. Both string and vector define an overloaded == that can be used to compare

objects of those types. Assuming svec1 and svec2 are vectors that hold strings,

identify and motivate which version of == is applied in each of the following

expressions:

a. "cobble" == "stone"

b. svec1[0] == svec2[0]

c. svec1 == svec2

d. "svec1[0] == "stone"

3. Copy each of the following code snippets and answer the questions that follow.

Code Snippet A:
Code Snippet B:

a. What type of polymorphism is demonstrated?


b. Is m() bound at compile-time or run-time? Discuss.

c. What is the output of the code? Discuss.

4. In C++, operators can be implemented as either members or non-class

members. Members are defined within a class scope. Non-members can either

be implemented as friends or as non-friend standalone functions in the class

scope. Consider the following three declarations of the + operator inside the

XYPoint class for an illustration.

Download the XYPoint class source on Thuto and for each of the

declarations marked (a), (b) and (c) in the code above:

a. Implement the + operator

b. Test the implementation in the main

c. Discuss one advantage and disadvantage of the form

5. Discuss whether each of the following operators can be implemented as class

members only, non-class members only or both. Motivate clearly.

a. %

b. ++ (post and pre-increment)

c. <<
d. &&

e. ==

f. ()

6. Declare and implement and test the following operators for the XYPoint class.

a. Assignment operator

b. Additive assignment operator

c. Unary + operator e.g usage XY p1(4,-5); XYPoint p2= -p1; //(-4,5)

d. Insertion operator

e. Extraction operator

7. In the previous labs and class exercises, you wrote interfaces and/or

implementations for the following classes. Motivate, what, if any, overloaded

operators that each of the classes should support.

a. Person

b. Matrix

c. Shape

d. MobileContact

e. Time

8. Declare and implement and test at-least three operators for one of the classes

analysed in 6 above.

You might also like