lecture 8 Flashcards
what is polymorphism?
polymorphism is an oop concept that allows an (object-reference variable) or a (pointer) to
1 - reference objects of different types
2- call the correct member functions, depending on the type of object being referenced
its the ability of an object to have multiple forms or behaviors
what are virtual member functions?
- function in base class that expects to be redefined in derived class , its defined with the keyword “virtual”
- supports “dynamic binding” ie: functions bound at run time to the function that they call
- When a virtual function is called on an object, the actual function that is executed depends on the type of the object at runtime, rather than its type at compile time.
- Without virtual member functions, C++ uses static
(compile time) binding
- virtual member function is a function in …………that expects to be redefined in derived class , its defined with the keyword “virtual”
- supports ………………ie: functions bound at run time to the function that they call
- Without virtual member functions, C++ uses ……………………binding
base class
“dynamic binding”
static
(compile time)
A …………… is dynamically bound to calls at runtime.
- At runtime, C++ determines the ………………………, and binds the function to the appropriate version of the function.
virtual function
type of object making the call
To make a function virtual, place the “virtual” key word before the return type in the……………….:
virtual char getLetterGrade() const;
* The compiler will not bind the function to calls. Instead, the program
………………………….
* virtual is not needed for the………………. , but is often included
* virtual is NOT added to the ………………..
* Virtual functions require considerable overhead so excessive use reduces program efficiency
*The function also becomes
virtual in all derived classes
automatically!
base class’s declaration
will bind them at runtime.
function declaration in the derived class
function definition
Polymorphism Requires ……………………
Polymorphic behavior is ONLY possible when an object is referenced by a ………….. or a …………
References or Pointers
reference variable or a pointer,
- redefining a virtual function in a derived class is called …………….
- …………… functions are …………… and………….functions are …………….
- virtual function is ………., and a non-virtual function is ………….
-overriding a function.
-redefined functions ,statically bound
-overridden functions, dynamically bound.
-overridden,redefined
polymorphism types?
compile time polymorphism :- function overloading , operator overloading
runtime polymorphism :- virtual function , pure virtual function
- …………….: is a virtual member function that MUST be overridden (redefined) in a derived class that has objects & it Must have NO function definition in the base class
- ………… contains AT LEAST one pure virtual function
virtual void Y() = 0;
* The = 0 indicates a ………….
Pure virtual function
Abstract base class
pure virtual function
what is an abstract base class??
Abstract base class: is a class that canNOT be used to create objects.
Serves as a basis for derived classes that may/will have objects
A class becomes an abstract base class when one or more of its member functions is a pure virtual function
what is multiple inheritance?
- its where A derived class can have more than one base class
- Each base class can have its own access specification in derived class’s definition
example:
class cube : public square,
public rectSolid; - Arguments can be passed to both base classes’ constructors
example:
cube :: cube(int side) : square(side), rectSolid(side, side, side); - Base class constructors are called in order given in class declaration, NOT in order used in class constructor
Problem: what if base classes have member variables/functions with the same
name?
Solutions:
* Derived class redefines the multiple-defined function
* Derived class invokes member function in a particular base class using scope resolution operator
::
- Problem: t multiple inheritance opens the opportunity for a derived class
to have ambiguous members. That is, two base classes may have member variables
or functions of the same name
Solutions:
the derived class should
always redefine or override the member functions.
*Calls to the member functions of the appropriate base class can be performed within
the derived class using ………………
- The derived class can also access the ambiguously named member variables of the correct base class using the scope resolution operator. If these steps aren’t taken, the compiler will generate an error when it can’t tell which member is being accessed
the scope resolution operator (::)
- A derived class inherits the __________ of its base class.
- When both a base class and a derived class have constructors, the base class’s constructor
is called __________ (first/last). - When both a base class and a derived class have destructors, the base class’s constructor
is called __________ (first/last). - An overridden base class function may be called by a function in a derived class by
using the __________ operator. - When a derived class redefines a function in a base class, which version of the function
do objects that are defined of the base class call? __________ - A(n) __________ member function in a base class expects to be overridden in a derived
class. - _________ binding is when the compiler binds member function calls at compile time.
- _________ binding is when a function call is bound at runtime.
- _________ is when member functions in a class hierarchy behave differently, depending
upon which object performs the call. - A(n) __________ class cannot be instantiated.
- A(n) __________ function has no body, or definition, in the class in which it is declared.
- A(n) __________ of inheritance is where one class is derived from a second class, which in turn is
derived from a third class. - _________ is where a derived class has two or more base classes.
- In multiple inheritance, the derived class should always __________ a function that has the same
name in more than one base class.
A derived class inherits the attributes and behavior of its base class.
When both a base class and a derived class have constructors, the base class’s constructor is called first.
When both a base class and a derived class have destructors, the base class’s destructor is called last.
An overridden base class function may be called by a function in a derived class by using the scope resolution operator (::).
When a derived class redefines a function in a base class, objects that are defined of the base class call the version of the function in the base class.
A virtual member function in a base class expects to be overridden in a derived class.
Early binding is when the compiler binds member function calls at compile time.
Late binding (also called dynamic binding or runtime polymorphism) is when a function call is bound at runtime.
Polymorphism is when member functions in a class hierarchy behave differently, depending upon which object performs the call.
An abstract class cannot be instantiated.
A pure virtual function has no body, or definition, in the class in which it is declared.
Hierarchical inheritance is where one class is derived from a second class, which in turn is derived from a third class.
Multiple inheritance is where a derived class has two or more base classes.
In multiple inheritance, the derived class should always explicitly specify which function to use when a function has the same name in more than one base class. This can be done using the scope resolution operator (::).