Inheritance, Polymorphism, and Virtual Functions

advertisement
Inheritance Concepts

Derive a new class (subclass) from an
existing class (base class or superclass).

Inheritance creates a hierarchy of related
classes (types) which share code and
interface.
1
Inheritance Examples
B ase C lass
D eriv ed C lasses
S tu d en t
C o m m u terS tu d en t
R esid en tS tu d en t
S h ap e
C ircle
T rian g le
R ectan g le
L o an
C arL o an
H o m eIm p ro v em en tL o an
M o rtg ag eL o an
2
Animal class hierarchy
Animals
Domestic
Wild
Tiger
Lion
Bear
Dog
Cat
horse
3
Credit cards
logo
card
owner’s name
inherits
from (is a)
visa
card
hologram
master
card
pin
american
express
category
4
Implementing Inheritance in C++

Develop a base class called student

Use it to define a derived class called
grad_student
5
The Student Class Hierarchy
student
student_id,
year, name
print()
inherits (is a)
grad_student
dept,
thesis
Inherits data and
methods from base
class
6
The Student Class Hierarchy
student
student_id,
year, name
print()
inherits (is a)
grad_student
dept,
thesis
print()
Override print()
7
Student Class
class student {
public:
student(string nm, int id, int y);
void print();
private:
int student_id;
int year;
string name;
};
8
Member functions
student::student(string nm, int id, int y)
{
student_id = id;
year = y;
name = nm;
}
void student::print()
{
cout << "\n STUDENT " << name;
}
9
Graduate Student Class grad_student.h
class grad_student: public student {
public:
grad_student(string nm, int id,
int y, string d, string th);
void print();
private:
string dept;
string thesis;
};
10
grad_student.cpp
grad_student::grad_student(string nm,
int id, int y, string d, string th)
:student(nm, id, y)
Calls base class constructor
{
dept = d;
thesis = th;
}
void grad_student::print()
{
cout << “GRAD STUDENT “ << name <<
dept << ", " << thesis << endl;
}
11
Examples of Use

grad_student * g = new grad_student(…);
g->print(); // grad student print
student * s = new student(…);
s->print(); // student print

No concern about which print is called



12
Examples of Use










Base class pointer can store any
descendant class
student * joe;
if (…)
joe = new grad_student();
else
joe = new student();
joe->print();
Dilemma - which print do you want?
joe->print(); //student print if NOT virtual
joe->print(); //correct print if virtual
“virtual” means “Make a run time decision”
13
Examples of Use









Base class pointer can store any
student * all[SIZE];
descendant class
all[0] = new student(…);
all[1] = new grad_student();
all[2] = new student(…);
all[3] = new grad_student();
…
for (i=0; I < SIZE;i++)
all[i]->print();
Dilemma - which print do you want?
14
Two Types of Binding

Static Binding (the default in C++)
– y->print() uses y’s print
– this is known at compile time

Dynamic Binding
– all[i]->print() uses the print() in the
object pointed at
– this is only known at run time
– coded in C++ with virtual functions
15
Student Class
class student {
public:
student(string nm, int id, int y);
void virtual print();
private:
int student_id;
int year;
string name;
};
16
Representing Shapes: abstract base class
shape
inherits (is a)
shape is only a category
and cannot be instantiated
rectangle
triangle
circle
••••
square
17
Why do we want an abstract base class?


Helpful for organization
Can use a pointer to a base class to store actual
instantiations of derived classes
18
C++ Shape Classes
class shape {
public:
virtual double area() = 0; //abstract
// shape will NOT define area
// but forces all derived classes to define area
};
class rectangle: public shape {
public:
double area() const
{return (height*width);}
:
private:
double height, width;
};
19
Every descendant of shape MUST have
an area method.
class circle: public shape {
public:
double area()
{return (PI*radius*radius);}
:
private:
double radius;
};
// etc
20
Use:
shape* p[N];
circle c1,...;
rectangle r1,...;
:
// fill in p with pointers to
// circles, squares, etc
p[0] = &c1; p[1] = &r1; ...
:
:
// calculate total area
for (i = 0; i < N; ++i)
tot_area = tot_area + p[i]->area();
21
Download