MOD 5_ZYBOOKS 6_OOP Flashcards

(32 cards)

1
Q

What is a derived class? Base class? Inheritance?

A

A derived class (or subclass) is a class that is derived from another class, called a base class (or superclass). Any class may serve as a base class.

The derived class is said to inherit the properties of the base class, a concept called inheritance. An object declared of a derived class type has access to all the public members of the derived class as well as the public members of the base class.

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

How is a derived class declared?

A

A derived class is declared by placing a colon “:” after the derived class name, followed by a member access specifier like public and a base class name.

Ex: class DerivedClass: public BaseClass { … };

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

VIEW: Class ProduceItem is derived from class GenericItem

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

What are the possible inheritance scenarios?

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

VIEW: Inheritance example: Business and Restaurant classes

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

Describe how members of a derived class may be accessed.

VIEW: Member functions of a derived class cannot access private members of the base class

A

The members of a derived class have access to the public members of the base class, but not to the private members of the base class. This is logical—allowing access to all private members of a class merely by creating a derived class would circumvent the idea of private members. Thus, adding the following member function to the Restaurant class yields a compiler error.

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

In a derived class, how is member access protected?

A

Recall that members of a class may have their access specified as public or private. A third access specifier is protected, which provides access to derived classes but not by anyone else.

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

VIEW: Access specifiers – Protected allows access by derived classes but not by others

A

In the following example, the member called name is specified as protected and is accessible anywhere in the derived class. Note however that the name member is not accessible in main() – the protected specifier only applies to derived classes; protected members are private to everyone else.

— > To make Restaurant’s DisplayRestaurant() function work, we merely need to change the private members to protected members in class Business. Business’s data members name and address thus become accessible to a derived class like Restaurant, but not elsewhere. A programmer may often want to make some members protected in a base class to allow access by derived classes, while making other members private to the base class.

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

VIEW: Access specifiers

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

Describe the different (derived) class definitions.

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

What does it mean for a member function to override?

A

When a derived class defines a member function that has the same name and parameters as a base class’s function, the member function is said to override the base class’s function.

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

What is the difference between overriding and overloading?

A

Overriding differs from overloading. In overloading, functions with the same name must have different parameter types, number of parameters, or return values. In overriding, a derived class member function must have the same parameter types, number of parameters, and return value as the base class member function with the same name. Overloading is performed if derived and base member functions have different parameter types; the member function of the derived class does not hide the member function of the base class.

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

An overriding function can call the overridden function by ____ the base class name.

VIEW: Function calling overridden function of base class

A

Prepending

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

What is polymorphism?

A

Polymorphism refers to determining which program behavior to execute depending on data types.

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

What are the 2 main types of polymorphism?

A

Function overloading is an example of compile-time polymorphism where the compiler determines which of several identically-named functions to call based on the function’s arguments.

One scenario requiring runtime polymorphism involves derived classes. Programmers commonly create a collection of objects of both base and derived class types.

Ex: The statement vector<Business*> businessList; creates a vector that can contain pointers to objects of type Business or Restaurant, since Restaurant is derived from Business. Similarly, polymorphism is also used for references to objects. Ex: Business& primaryBusiness declares a reference that can refer to Business or Restaurant objects.

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

VIEW: Compile-time polymorphism vs. runtime polymorphism

17
Q

What is derived/base class pointer conversion?

A

When a pointer to a derived class is converted to a pointer to the base class without explicit casting.

18
Q

What is a virtual function?

A

Runtime polymorphism only works when an overridden member function in a base class is virtual. A virtual function is a member function that may be overridden in a derived class and is used for runtime polymorphism. A virtual function is declared by prepending the keyword “virtual”.

Ex: virtual string GetDescription() const

At runtime, when a virtual function is called using a pointer, the correct function to call is dynamically determined based on the actual object type to which the pointer or reference refers.

19
Q

What is the override keyword and what is considered good practice of it?

A

The override keyword is an optional keyword used to indicate that a virtual function is overridden in a derived class.

Good practice is to use the override keyword when overriding a virtual function to avoid accidentally misspelling the function name or typing the wrong parameters.

20
Q

What is a virtual table?

A

To implement virtual functions, the compiler creates a virtual table that allows the computer to quickly lookup which function to call at runtime. The virtual table contains an entry for each virtual function with a function pointer that points to the most-derived function that is accessible to each class. Looking up which function to call makes runtime polymorphism slower than compile-time polymorphism.

21
Q

VIEW: Runtime polymorphism via a virtual function

22
Q

What is a pure virtual function?

A

Sometimes a base class should not provide a definition for a member function, but all derived classes must provide a definition.
Ex: A Business may require all derived classes to define a GetHours() function, but the Business class does not provide a default GetHours() function.

A pure virtual function is a virtual function that provides no definition in the base class, and all derived classes must override the function. A pure virtual function is declared like a virtual function with the “virtual” keyword but is assigned with 0.
Ex: virtual string GetHours() const = 0; declares a pure virtual function GetHours().

23
Q

What is an abstract base class?

A

A class that has at least one pure virtual function is known as an abstract base class. An abstract base class object cannot be declared.

Ex: The variable declaration Business someBusiness; generates a syntax error if Business is an abstract base class.

24
Q

VIEW: Business is an abstract base class

A

In the above example, the programmer may intend to create several classes derived from Business, such as Restaurant, LawnService, and CoffeeShop. The abstract base class Business implements functionality common to all derived classes, thus avoiding redundant code in all derived classes, and supporting uniform treatment of a collection (e.g., vector) of objects of derived classes via polymorphism. Not overriding the pure virtual function in a derived class makes the derived class an abstract base class too.

25
VIEW: Possible warning messages when using virtual functions
26
What are the 3 key features of abstract classes?
27
Provide definitions for: 1) Pure virtual function 2) Abstract class 3) Concrete class
28
VIEW: Shape is an abstract class. Circle and Rectangle are concrete classes that extend the Shape class
(VIEW IN ZYBOOKS)
29
What is composition? How does it differ from inheritance? (Is-a VS has-a)
The concept of inheritance is commonly confused with the idea of composition. Composition is the idea that one object may be made up of other objects, such as a MotherInfo class being made up of objects like firstName (which may be a string object), childrenData (which may be a vector of ChildInfo objects), etc. Defining that MotherInfo class does not involve inheritance, but rather just composing the sub-objects in the class.
30
VIEW: Composition
31
VIEW: Inheritance
32
What are UML diagrams?
Captions: 1) A class diagram depicts a class' name, data members, and functions. 2) A solid line with a closed, unfilled arrowhead indicates a class is derived from another class. 3) The derived class only shows additional members.