0% found this document useful (0 votes)
3 views

Lecture19

Uploaded by

Aurangzaib Gill
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture19

Uploaded by

Aurangzaib Gill
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Abstract Classes, Multiple

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

Mertech Mertech Mertech

Every implementation was a separate code base from


scratch
Recap
Base class

Virtual
functions

Oracle MS SQL MySQL

All function calls are bound at runtime


Pure Virtual Functions
• Functions that:
• Are declared as virtual
• Have no implementation
• “= 0” at the end of the function signature
• Corresponding class can’t be instantiated
• Incomplete implementation
• Known as an abstract class
• The opposite is known as a concrete class
• A derived class or may not provide an implementation
• Can’t instantiate unless it implements all inherited pure 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

ConsoleLogger FileLogger DBLogger

TextFileLogger CSVFileLogger

Which class(es) should be abstract? 8


Other Examples
• GUI Frameworks:
• Drawable interface: methods like draw(), resize()
• Payment systems:
• PaymentProcessor interface: methods like authorize(), refund()
• Database Management System library:
• Connection interface: methods like connect(), query(), disconnect()
• Multimedia libraries:
• Playable interface: methods like play(), pause(), stop()

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

Multiple • A Teaching Assistant is:


• A Student
Inheritance • An Employee
• A Hero object in a game is:
• Movable
• Drawable
Coding example
Refrigerator InternetDevice
# capacity: float # ip_address: int
# temp_setting: float
+ updateThermostat(double): + send(int, char*): bool
void + receive(int): Packet*
+ getPresetTemperature(): float
+ on(): void
+ off(): void

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

A teaching assistant is simultaneously a student and an


employee
Let’s code
Summary
• C++ supports inheriting from multiple base classes
• Multiple base classes:
• Are separated with a comma
• Each has the inheritance type (public/private/protected) mentioned
• The diamond problem:
• multiple base classes have a shared parent
• Solution:
• Use virtual inheritance (on the base classes with common parent)
A Common Application
• Implementing multiple “interfaces”
• Different kinds of behavior is declared in abstract base classes
• Derived class inherits from each, implementing each
Practice Problems
• Vehicle, LandVehicle, WaterVehicle, AmphibiousVehicle
• Device, Sensor, SmartDevice
Include Guards
• Multiple includes cause compilation errors
• Include guards prevent multiple includes
• Let’s code

You might also like