Chapter 7 Polymorphism_231119_152610 (3)
Chapter 7 Polymorphism_231119_152610 (3)
Polymorphism
1
Polymorphism
• Polymorphism—the same word or phrase
can be mean different things in different
contexts
• Greek term polymorphos means “having many
forms.”
• For example:
– In OOP, two or more classes could each have a
method called output
– Each output method would do the right thing
for the class that it was in.
– One output might display a number whereas a 2
different one might display a name.
Overriding vs. Overloading
• Overloading refers to methods of the
same class with the same name but
different signatures or return types.
• Overriding refers to methods of different
classes with the same name, signatures
and return types. The overriden method
appears in a subclass.
3
Overloading Functions
4
Overloaded Functions
• The operation of one function depends on the argument
passed to it.
– Example: Fly(), Fly(low), Fly(150)
• Some languages, like C++, permit the programmer to
overload function names
• For Example:
int square(int x) {
return x*x;
}
double square(double x) {
return x*x;
}
5
Overriding Functions
6
Virtual Functions
• A virtual function is a member function
– declared within a base class
– redefined by a derived class (i.e. overriding)
• A virtual member function in a base class
expects to be overridden in a derived class
• The keyword virtual is used to designate a
member function as virtual.
• It can be used to support run-time
polymorphism.
• Used to support polymorphism with pointers and
references
7
Virtual Functions (cont’d)
• In derived class – redefined and the
function signature must match the
original function (in the base class).
• keyword virtual not needed in
derived class (but can be specified if
the programmer wants).
• The “virtual”-ity of the member
function continues along the
inheritance chain.
• polymorphic class - class that
contains a virtual function
8
Virtual Functions – Example 1
class base { void main() {
public: base b1;
virtual void show() { b1.show(); // base - (s.b.)
cout << “base\n”; derived d1;
} d1.show(); // derived – (s.b.)
};
base *pb = &b1;
class derived : public base { pb->show(); // base - (d.b.)
public: pb = &d1;
void show() { pb->show(); // derived (d.b.)
cout << “derived\n”; }
}
}; Here,
s.b. = static binding
d.b. = dynamic binding
9
Virtual Functions – Example 2
class base { class d2 : public base {
public: public:
virtual void show() { void show() {
cout << “base\n”; cout << “derived-2\n”;
} }
}; };
Shape
Circle Triangle
public void draw() public void draw()
19
Example 2 (cont’d)
• A pure virtual function not defined in the
derived class remains a pure virtual function.
• Hence derived class also becomes abstract
class Circle : public Shape { //No draw() - Abstract
public :
void print(){
cout << “I am a circle” << endl;
}
class Rectangle : public Shape {
public :
void draw(){ // Override Shape::draw()
cout << “Drawing Rectangle” << endl;
}
Rectangle r; // Valid
Circle c; // error : variable of an abstract class
Abstract Classes and Methods
• Purpose of an abstract class
– Declare common attributes …
– Declare common behaviors of classes in a
class hierarchy
• Contains one or more abstract methods
– Subclasses must override
• Instance variables, concrete methods of
abstract class
– subject to normal rules of inheritance
21
Abstract Classes
• Classes that are too general to create
real objects
• Used only as abstract superclasses for
concrete subclasses and to declare
reference variables
• Many inheritance hierarchies have
abstract superclasses occupying the top
few levels
22