Lecture19
Lecture19
Inheritance
CS 200
Learning Objectives
• Abstract classes
• Multiple inheritance
• Diamond problem
• Include guards
Recap
Dataflex
Function calls Data store/retrieve
Dataflex
Mertech
Function calls
Oracle
Recap
Virtual
functions
6
Purpose Of An Abstract Class
• What good is an abstract class?
• Creates an “interface” – a contract with a programmer
• There are usually multiple ways to do the same thing
• Any class can implement a set of pure virtual functions differently
• Examples:
• Comparable: isEqual(), isLessThan()
• Printable: toString()
• Savable: saveToFile(), loadFromFile()
• PaymentProcessor: charge()
• Concrete shared members only defined in the base class
7
Example
Logger
TextFileLogger CSVFileLogger
9
Summary
• A pure virtual function (PVF) is one that has no implementation
• A class with one or more PVFs is an abstract class
• An abstract class may not be instantiated
10
• Sometimes objects inherit from multiple base
classes
• A bat is a mammal as well as a bird
SmartRefrigerator
# temp_history: float[]
# power_consumption: float
+ sendPowerConsumtion(int): bool
Let’s code!
Another scenario
Person
Where do we put common
attributes like name?
Student Employee
Teaching Assistant