Download presentation
Presentation is loading. Please wait.
1
C ++ Inheritance
2
Inheritance A derived class is an addition to a base class.
The attributes and functions of the base class are available in the derived class, Almost private protected public
3
A base class X and a derived class Y
4
TTYP –derived class for book
class catalog_item { char* title; char* catalog_number; public: catalog_item(){title = new char[MAX]; catalog_number = new char[MAX];} void addtitle (char* newtitle); void addcatnum(char* new catnum); void display(); }; TTYP1 – write declaration of attributes and functions TTYP2 – write definition of addbook
5
Library Object model
6
Storing base and derived instances
A pointer to a base type can point to instances of derived classes Virtual functions will figure “right” function to apply type casting can let the compiler know when you know that the type is really something else.
7
TTYP3 - addbook Write an addbook function for the library class
Assume that library has an array of pointers to catalog-items catalog-item *itemlist[MAX];
8
Redefining functions Must have the same return type Virtual functions
puts indirection in object space
9
TTYP4 Write a display function for book
10
TTYP5 Write a virtual display function for cat-item
11
TTYP6 Write a display function for library
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.