ITP 165x Lecture Notes - Lecture 23: Virtual Function, Calcareous Sponge

63 views1 pages
ITP 165: Intro to C++ - Lecture 23, Pure virtual functions, Abstract classes
Describing the Relationships
So conceptually, we could make a Shape class and say:
A Circle is-a type of Shape
A Rect is-a type of Shape
A Tri is-a type of Shape
We want to put all the common functionality into Shape
All shapes support a calcArea function
but each calcArea function is different
Virtual Function
A virtual function is one that child classes can override:
virtual double calcArea() = 0;
The = 0 means that all children of Shape are required to implement this function. No ifs,
ands or buts. The function is a pure virtual function
Abstract Functions
When you create a class that has a pure virtual function, it is called an abstract class
We call these classes abstract, because they have member functions that cannot be
implemented
You cannot create instances of these classes
Classes that have all their member functions defined are called concrete classes
Without virtual functions, we could not create a single collection class – like a Vector –
that contains objects of different classes that inherit from the same base class
We would have to have a collection of each derived type
This only works when the collection class is a collection of pointers to the objects.
Inheritance
Parent class pointers are allowed to point to instances of child classes
This is called polymorphism, and is absolutely key to this working
Unlock document

This preview shows half of the first page of the document.
Unlock all 1 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Itp 165: intro to c++ - lecture 23, pure virtual functions, abstract classes. So conceptually, we could make a shape class and say: We want to put all the common functionality into shape. A virtual function is one that child classes can override: The = 0 means that all children of shape are required to implement this function. When you create a class that has a pure virtual function, it is called an abstract class. We call these classes abstract, because they have member functions that cannot be implemented. You cannot create instances of these classes. Classes that have all their member functions defined are called concrete classes. Without virtual functions, we could not create a single collection class like a vector that contains objects of different classes that inherit from the same base class. We would have to have a collection of each derived type.

Get access

Grade+20% OFF
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers