1
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.
2
Inheritance Examples
Base Class Derived Classes
Student CommuterStudentResidentStudent
Shape CircleTriangleRectangle
Loan CarLoanHomeImprovementLoanMortgageLoan
3
Animal class hierarchy
Wild
Animals
Domestic
Tiger Lion Bear Dog Cat horse
4
Credit cardslogo
americanexpress
hologram
cardowner’s name
inheritsfrom (is a)
visacard
mastercard
pin category
5
Implementing Inheritance in C++
Develop a base class called student
Use it to define a derived class called grad_student
6
The Student Class Hierarchy
studentprint()
grad_student
inherits (is a)
student_id,year, name
dept,thesis
Inherits data and methods from base class
7
The Student Class Hierarchy
studentprint()
grad_studentprint()
inherits (is a)
student_id,year, name
dept,thesis Override print()
8
Student Class
class student {public: student(string nm, int id, int y); void print();private: int student_id; int year; string name;};
9
Member functions
student::student(string nm, int id, int y){ student_id = id; year = y; name = nm;}
void student::print(){ cout << "\n STUDENT " << name;}
10
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;};
11
grad_student.cpp
grad_student::grad_student(string nm, int id, int y, string d, string th)
:student(nm, id, y){ dept = d; thesis = th;}
void grad_student::print(){ cout << “GRAD STUDENT “ << name << dept << ", " << thesis << endl;}
Calls base class constructor
12
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
13
Examples of Use
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”
Base class pointer can store any descendant class
14
Examples of Use
student * all[SIZE]; 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?
Base class pointer can store any descendant class
15
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
16
Student Class
class student {public: student(string nm, int id, int y); void virtual print();private: int student_id; int year; string name;};
17
Representing Shapes: abstract base class
shape
rectangle
square
triangle circle • • • •
inherits (is a)
shape is only a categoryand cannot be instantiated
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
19
C++ Shape Classesclass 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;};
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
21
Use:shape* p[N];circle c1,...;rectangle r1,...; :// fill in p with pointers to // circles, squares, etcp[0] = &c1; p[1] = &r1; ... : :// calculate total areafor (i = 0; i < N; ++i) tot_area = tot_area + p[i]->area();
Top Related