Chapter4 Inheritanceandpolymorphism
Chapter4 Inheritanceandpolymorphism
Chapter 4 Inheritance
Inheritance & Polymorphism
1
Terminology
C++ inheritance mechanism is used to build new
classes from an existing class Base class = generic class/parent class/superclass
Classes are allowed to inherits commonly used Derived class = specific class/child class/subclass
state and behavior from other classes.
Inheritance
Single Inheritance Multiple Inheritance
Single Inheritance Multiple Inheritance
SuperClass SuperClass SuperClass
subclass may inherit instance subclass may inherit instance
variables and methods of a variables and methods from
single parent class, possibly multiple parent classes.
adding some methods and
instance variables of its own. SubClass SubClass SubClass
the new class subclasses of the SubClass
other class, it does not need to
re-implement the basic
functionality of its
superclasses but needs only to
add its own specialized
behavior and state
2
Single Inheritance
Student
SuperClass
3
Single Inheritance Single Inheritance
Suppose we want to model postgraduate and
undergraduate students.
Student
For both types of student we keep their names, test score
and final course grade.
4
Single Inheritance Single Inheritance
Example 4
Introduce class Circle using inheritance from Class Point
class Vehicle class Point{
{ int model; private: class Circle : public Point {
int year;}; int x, y; // point coordinate private :
public: float Result;
class Car : public Vehicle void setX(int val); public:
void setY(int val); Circle(); //constructor declaration
{ string carNo;}
int getX(); void SetPoint();
int getY(); void GetResult();
class MPV : public Vehicle }; // class Point };
{ int backDoor;}
Multiple Inheritance
// Base class Rect.setWidth(5);
class Shape { Rect.setHeight(7);
public:
void setWidth(int w) { // Print the area of the object.
width = w; cout << "Total area: “;
} cout << Rect.getArea() << endl;
void setHeight(int h) {
height = h; return 0;
SuperClass SuperClass
} }
protected:
int width;
int height;
};
// Derived class
class Rectangle: public Shape {
public:
int getArea() { SubClass
return (width * height);
}
};
5
Base class access
specifier
Multiple Inheritance Multiple Inheritance
TeachingAssistant
6
Polymorphism
Polymorphism Concept:
Polymorphism means having many forms.
Provides the ability to use a single message to invoke
many different kinds of behavior.
Methods with the same name with different meaning.
The methods must belong to different kinds of
objects
Polymorphism also allows significant code sharing
and code reuse
Polymorphism
Polymorphism
#include<iostream> int main()
using namespace std; {
7
Polymorphism Polymorphism
Example 2
Virtual Function (page 98 text book)
Shape
• Virtual functions are used in C++ to ensure that the correct function is
called from the appropriate derived class.
Rectangle Circle
• This is done at run time where dynamic binding occurs. +width +radius
+height
Rectangle(double w, double h) Circle(double r)
+area_calc() : double +area_calc() ; double
Polymorphism
#include <iostream> class Circle : public Shape{
#define PI 3.14159 public:
using namespace std; double radius;
Circle(double r = 2) { radius = r; }
class Shape //Base class{ double area_calc(){
public: return PI*radius*radius;
Shape() {} }
virtual double area_calc() { };
return 0; } ’.
}; int main(){ Rosziati Ibrahim and Sapiee Jamel (2006), “ Object-Oriented
Circle circ; Programming Using C++: An Introduction”,McGrawHill Sdn.
class Rectangle : public Shape{ Rectangle rect; Bhd, Malaysia.
public: Deitel, H.M and Deitel P.J( 2005),” C++ How To Program
double width, height; Shape* ptr = ˆ Fifth Edition”, Pearson Education, Inc., New Jersey.
Rectangle(double w = 2, double cout << "circ area: " << ptr- Koffman, Elliot B. and Wolfgang, Paul A.T (2006),”Objects, Abstraction,
h = 2){ >area_calc() << endl;
Data Structures and Design Using C++”, John Wiley and Sons,US.
width = w; height = h;
Gaddis, T. et al (2008), “Starting Out with C++ Early Objects”, Pearson
} ptr = ▭ //ptr -> rect
double area_calc(){ cout << "rect area: " << ptr-
Education, Inc.
return width*height; >area_calc() << endl;
}
}; cout << "circ area: " <<
circ.area_calc() << endl;
return 0;
}