21
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.

1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related

  • View
    231

  • Download
    1

Embed Size (px)

Citation preview

Page 1: 1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related

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.

Page 2: 1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related

2

Inheritance Examples

Base Class Derived Classes

Student CommuterStudentResidentStudent

Shape CircleTriangleRectangle

Loan CarLoanHomeImprovementLoanMortgageLoan

Page 3: 1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related

3

Animal class hierarchy

Wild

Animals

Domestic

Tiger Lion Bear Dog Cat horse

Page 4: 1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related

4

Credit cardslogo

americanexpress

hologram

cardowner’s name

inheritsfrom (is a)

visacard

mastercard

pin category

Page 5: 1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related

5

Implementing Inheritance in C++

Develop a base class called student

Use it to define a derived class called grad_student

Page 6: 1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related

6

The Student Class Hierarchy

studentprint()

grad_student

inherits (is a)

student_id,year, name

dept,thesis

Inherits data and methods from base class

Page 7: 1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related

7

The Student Class Hierarchy

studentprint()

grad_studentprint()

inherits (is a)

student_id,year, name

dept,thesis Override print()

Page 8: 1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related

8

Student Class

class student {public: student(string nm, int id, int y); void print();private: int student_id; int year; string name;};

Page 9: 1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related

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;}

Page 10: 1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related

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;};

Page 11: 1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related

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

Page 12: 1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related

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

Page 13: 1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related

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

Page 14: 1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related

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

Page 15: 1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related

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

Page 16: 1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related

16

Student Class

class student {public: student(string nm, int id, int y); void virtual print();private: int student_id; int year; string name;};

Page 17: 1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related

17

Representing Shapes: abstract base class

shape

rectangle

square

triangle circle • • • •

inherits (is a)

shape is only a categoryand cannot be instantiated

Page 18: 1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related

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

Page 19: 1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related

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;};

Page 20: 1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related

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

Page 21: 1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related

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();