Download presentation
Presentation is loading. Please wait.
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 classes (types) which share code and interface.
2
2 Inheritance Examples
3
3 Animal class hierarchy Wild Animals Domestic TigerLionBearDogCathorse
4
4 Credit cards logo american express hologram card owner’s name inherits from (is a) visa card master card pin category
5
5 Implementing Inheritance in C++ Develop a base class called student Use it to define a derived class called grad_student
6
6 The Student Class Hierarchy student print() grad_student inherits (is a) student_id, year, name dept, thesis Inherits data and methods from base class
7
7 The Student Class Hierarchy student print() grad_student print() inherits (is a) student_id, year, name dept, thesis Override print()
8
8 Student Class class student { public: student(string nm, int id, int y); void print(); private: int student_id; int year; string name; };
9
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
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
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
12 Examples of Use n grad_student * g = new grad_student(…); n g->print(); // grad student print n student * s = new student(…); n s->print(); // student print n No concern about which print is called
13
13 Examples of Use n student * joe; n if (…) n joe = new grad_student(); n else n joe = new student(); n joe->print(); n Dilemma - which print do you want? n joe->print(); //student print if NOT virtual n joe->print(); //correct print if virtual n “virtual” means “Make a run time decision” Base class pointer can store any descendant class
14
14 Examples of Use n student * all[SIZE]; n all[0] = new student(…); n all[1] = new grad_student(); n all[2] = new student(…); n all[3] = new grad_student(); n … n for (i=0; I < SIZE;i++) n all[i]->print(); n Dilemma - which print do you want? Base class pointer can store any descendant class
15
15 Two Types of Binding n Static Binding (the default in C++) –y->print() uses y ’s print –this is known at compile time n 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
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
17 Representing Shapes: abstract base class shape rectangle square triangle circle inherits (is a) shape is only a category and cannot be instantiated
18
Why do we want an abstract base class? n Helpful for organization n Can use a pointer to a base class to store actual instantiations of derived classes 18
19
19 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; };
20
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
21 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 area();
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.