C++ Chapter 11: Inheritance and Composition

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Consider the following statement: class B: protected A { ... }; How are the various members of A inherited by B?

1) Both public and protected members of A become protected members of B, and can be directly accessed. 2) private members of A become private members of B, and cannot be directly accessed by class B.

The 3 basic concepts of Object-Oriented Design

1) Encapsulation 2) Inheritance 3) Polymorphism

Two most common ways to relate classes

1) Inheritance ("is-a" relationship) 2) Composition ("has-a" relationship)

Consider the following statement: class B: public A { ... }; How are the various members of A inherited by B?

1) Public members of A become public members of B. 2) Protected members of A become protected members of B. 3) Private members of A become private members of B. B cannot directly access these private members.

To include a predefined header file in a program, it must be enclosed in ___1___. To include a user-defined header file, it must be enclosed in ___2___.

1) angular brackets (< >) 2) double quotes (" ")

Examples of polymorphism

1) function overloading 2) multiple constructors in a single class 3) operators (+,-,/,*,etc.) behave differently based on their operands.

Two parts of every object

1) internal state 2) external state

encapsulation

the ability to combine data, and operations on that data, in a single unit

inheritance

the ability to create new classes from existing classes; an is-a relationship

Inheritance

the ability to create new objects (classes) from existing objects.

polymorphism

the ability to use the same expression to denote different operations

If a derived class has no declared member variables...

then its member variables are those that were declared in the base class.

Parametric polymorphism

type of polymorphism in which the data type is left unspecified and then later instantiated.

multiple inheritance

when a derived class is derived from more than one base class

single inheritance

when an object is derived from a single class

How do objects interact with each other?

with function calls

Statements used to avoid preprocessor directive multiple-inclusion errors

#ifndef identifier #define identifier statements #endif

#ifndef

"if not defined." Used to determine whether a particular preprocessor directive was called previously or not.

When does a destructor execute?

A class automatically calls its destructor when the class goes out of scope.

derived class

A class that was developed from other classes

What happens when a preprocessor directive is included more than once in a program?

A compile-time error results.

dynamic memory space

A memory location that is allocated during execution time.

Consider the following statement: class B: private A { ... }; How are the various members of A inherited by B?

All members of A (public, protected, and private), become private members of B. B can access the public and protected members directly, but private member are hidden.

What makes up the internal state of a class?

All private members of the class

What makes up the external state of a class?

All public members of the class

When creating a class by composition, in what order must the class be built?

All smaller classes must be built individually, then the new class is built by putting together the smaller ones.

Aggregation

Another name for composition

Since private members of the base class cannot be accessed directly through the derived class, how can private base members be used?

By accessing them through the public member functions of the base class.

What are destructors for?

Destructors deallocate dynamic memory that had been allocated to the objects of a class.

What does a derived class destructor do?

It executes when the derived class goes out of scope, then calls the destructor of its base class.

What does it mean to "Redefine member functions of the base class"?

It means that the derived class can change member functions it inherits in such a way that the functions have the same name and parameters, but different body code.

How to identify a class from a problem statement

Look at all the nouns, and determine which one is most important, and which nouns are characteristics of the important one.

How to identify possible operations/functions for a class from a problem statement

Look at all the verbs. Determine which verbs perform actions or operations on the nouns.

What is the benefit of classes?

Once a class is developed and becomes error-free, it can be reused freely in other programs.

What is the difference between internal and external states of an object?

Only the object itself can manipulate its internal state. The object's external state can be accessed and manipulated by other objects.

Earliest developed OOP language

Simula in 1967

Encapsulation

The ability to combine data and operations on that data in a single unit.

Polymorphism

The ability to use the same expression to denote different operations in OOD.

If a base class function is NOT redefined in a derived class, how can it be accessed by the derived class?

The derived class can just call the function with its name and parameters.

Since derived classes cannot access private member variables of a base class, how can those member variables be initialized?

The derived class constructor must include code that will cause the base class constructors to execute.

derived classes

new classes that are created from base classes

composition (aggregation)

one or more members of a class are objects of another class type; a has-a relationship

base classes

predefined and user-defined classes that are used to define other classes.

If no member access specifier is included, then the program defaults to...

private inheritance

What is the purpose of "protected" members?

Unlike private members, protected members of a base class can be directly accessed by a derived class, but are still inaccessible outside of the class.

Possible values for "memberAccessSpecifier" when declaring a derived class

public private protected

single inheritance

When a class is derived from only one other class.

multiple inheritance

When a class is derived from two or more other classes

What is the difference between function overloading and function redefining in derived classes?

When a derived class defines a function with the same signature as a function in the base class, the function is said to be redefined. If the derived class defines a function with the same name, but a different signature as a function in the base class, the function is overloaded. Both redefining and overloading are legal in derived classes

What must you include at the beginning of a derived class header file?

a preprocessor directive for the header file of the base class used.

When a function is overloaded in a derived class, how does one call the original base function for use?

by using the resolution operator (::) and a correct parameter list

If a function is redefined in a derived class, how can you access the base class version of the function?

by using the scope resolution operator (::) Ex. rectangleType::area()

The basic unit of an OOD program is a...

class

syntax of a derived class

class className: memberAccessSpecifier baseClassName { member list };

base classes

classes from which you derive new classes

Composition

creating a derived class by putting together 2 or more base classes. Ex: creating class PersonalInfo from classes DateType and PersonType.

Syntax for triggering a base class constructor in a derived constructor

derivedClass::derivedClass(type Param1, type Param2,...) : baseClass( baseParam1, baseParam2, ...)

The basic unit of a structured program is a...

function

What is the difference between public and private inheritance?

in public inheritance, the public members of the base class become public members of the derived class. In private inheritance, all members of the base class become private members that cannot be directly accessed by the derived class.

Where must the triggering of the base function constructor occur?

in the heading of the derived class constructor


संबंधित स्टडी सेट्स

The Federalist Papers (1787 - 1789) (1)

View Set

Nonfiction/Reading for information-prose writing that is based on facts, real events, and real people, such as biography or history

View Set

POLI 107: Hannah Arendt: " Origins of Totalitarianism"

View Set

A/AS Level Business Studies - Chapter 2 - Business Structure (some A-Level material)

View Set