• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/29

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

29 Cards in this Set

  • Front
  • Back
-Associating many meanings to one function
-Virtual functions provide this capability
-Fundamental principle of object-oriented programming!
Polymorphism
Can be "used" before it’s "defined"
Virtual Function
Want inherited function center() to use function Triangle::draw() NOT function Figure::draw()
But class Triangle wasn’t even WRITTEN when Figure::center() was! Doesn’t know "triangles"!
Example use of virtual function
implement late binding
Virtual functions
"Don’t know how function is implemented"
"Wait until used in program"
"Then get implementation from object instance"
Virtual functions
Program must:
Compute daily gross sales
Calculate largest/smallest sales of day
Perhaps average sale for day
All come from individual bills
But many functions for computing bills will be added "later"!
When different types of sales added!
So function for "computing a bill" will be virtual!
Impact: Later, derived classes of Sale can define THEIR versions of function bill
Other member functions of Sale will use version based on object of derived class!
They won’t automatically use Sale’s version!
Notice reserved word "virtual" in declaration of member function bill
Virtual functions changed
overridden
Non-virtual functions changed
redefined
One major disadvantage of virtual functions
overhead
virtual void draw() = 0;
pure virtual function
require no definition
Forces all derived classes to define "their own" version
Pure virtual functions
Class with one or more pure virtual functions
Can only be used as base class
No objects can ever be created from it
Since it doesn’t have complete "definitions" of all it’s members!
abstract base class
Given: Derived is derived class of Base
Derived objects can be assigned to objects of type Base
But NOT the other way!
Consider previous example:
A DiscountSale "is a" Sale, but reverse not true
Extended Type Compatibility
a nuisance of virtual functions that can be solved with pointers to dynamic variables
slicing problem
needed to de-allocate dynamically allocated data
Good policy for all _____ to be virtual
destructors
Base *pBase = new Derived; … delete pBase;
Would call base class destructor even though pointing to Derived class object!
Making destructor virtual fixes this
From descendant type to ancestor type
Upcasting
Casting from ancestor type to descended type
dangerous
Assumes information is "added"
Downcasting
delays decision of which member function is called until runtime
Late binding
functions have no definition
Pure virtual functions
Classes with at least one are abstract
No objects can be created from abstract class
Used strictly as base for others to derive
Pure virtual functions
_____ class objects can be assigned to _____ class objects
Derived, base
Allow very "general" definitions for functions and classes
Type names are "parameters" instead of actual types
Precise definition determined at run-time
templates
template<class T>
Tells compiler what’s coming is "template"
And that T is a type parameter
template prefix
C++ allows keyword _____ in place of keyword "class" in templates
But most use "class" anyway
typename
Typically we have them separate
For templates separate not supported on most compilers!
Function declarations and definitions
template<class T1, class T2>
Not typical
Usually only need one "replaceable" type
Cannot have "unused" template parameters
Each must be "used" in definition
Error otherwise!
Multiple Type Parameters
Refers to implementing templates
Express algorithms in "general" way:
Algorithm applies to variables of any type
Ignore incidental detail
Concentrate on substantive parts of algorithm
Function templates are one way C++ supports _____
Algorithm Abstraction