lecture 8 Flashcards

1
Q

what is polymorphism?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what are virtual member functions?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  • 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
A

base class
“dynamic binding”
static
(compile time)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

A …………… is dynamically bound to calls at runtime.

  • At runtime, C++ determines the ………………………, and binds the function to the appropriate version of the function.
A

virtual function
type of object making the call

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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!

A

base class’s declaration
will bind them at runtime.
function declaration in the derived class
function definition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Polymorphism Requires ……………………

Polymorphic behavior is ONLY possible when an object is referenced by a ………….. or a …………

A

References or Pointers
reference variable or a pointer,

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  • redefining a virtual function in a derived class is called …………….
  • …………… functions are …………… and………….functions are …………….
  • virtual function is ………., and a non-virtual function is ………….
A

-overriding a function.
-redefined functions ,statically bound
-overridden functions, dynamically bound.
-overridden,redefined

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

polymorphism types?

A

compile time polymorphism :- function overloading , operator overloading

runtime polymorphism :- virtual function , pure virtual function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  • …………….: 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 ………….

A

Pure virtual function
Abstract base class
pure virtual function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what is an abstract base class??

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what is multiple inheritance?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Problem: what if base classes have member variables/functions with the same
name?

A

Solutions:
* Derived class redefines the multiple-defined function
* Derived class invokes member function in a particular base class using scope resolution operator
::

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  • 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
A

Solutions:
the derived class should
always redefine or override the member functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

*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
A

the scope resolution operator (::)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  1. A derived class inherits the __________ of its base class.
  2. When both a base class and a derived class have constructors, the base class’s constructor
    is called __________ (first/last).
  3. When both a base class and a derived class have destructors, the base class’s constructor
    is called __________ (first/last).
  4. An overridden base class function may be called by a function in a derived class by
    using the __________ operator.
  5. 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? __________
  6. A(n) __________ member function in a base class expects to be overridden in a derived
    class.
  7. _________ binding is when the compiler binds member function calls at compile time.
  8. _________ binding is when a function call is bound at runtime.
  9. _________ is when member functions in a class hierarchy behave differently, depending
    upon which object performs the call.
  10. A(n) __________ class cannot be instantiated.
  11. A(n) __________ function has no body, or definition, in the class in which it is declared.
  12. A(n) __________ of inheritance is where one class is derived from a second class, which in turn is
    derived from a third class.
  13. _________ is where a derived class has two or more base classes.
  14. In multiple inheritance, the derived class should always __________ a function that has the same
    name in more than one base class.
A

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 (::).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly