OOS Chapter 10 exam 3
Fact: Multiple inheritance is never
Multiple inheritance is never required to solve a programming problem; the same results always can be achieved through single inheritance.
Fact: Multiple inheritance occurs when
Multiple inheritance occurs when a child class derives from more than one base class
NOTE: No matter which access specifier
No matter which access specifier you use when creating a child class, access to parent class members never becomes more lenient than originally coded.
DO: One possible PetStoreAnimal constructor implementation:
One possible PetStoreAnimal constructor implementation: PetStoreAnimal::PetStoreAnimal(int age) : PetStoreItem(1234, 69.95) { petAge=age; }
Fact: Single inheritance occurs
Single inheritance occurs when a child class derives from a single parent.
Fact: Some programmers are
Some programmers are vehemently opposed to using multiple inheritance
The class definition for StudentEmployee would begin as follows:
The class definition for StudentEmployee would begin as follows: class StudentEmployee : public Student, public Employee
Fact: The values passed to a base class
The values passed to a base class constructor from a child class can be constant or variable.
Note: The version of the Employee::setFields()
The version of the Employee::setFields() function in Figure10-15 provides an additional advantage over the one in Figure 10-14—it could be used even if the data fields in the Person class were declared to be private rather than protected.
Fact: The word virtual indicates
The word virtual indicates that the base class should be used only once even though it appears more than once in a class's ancestry.
DONT DO IT: This constructor
This constructor wont work because the parent class constructor requires arguments: PetStoreAnimal::PetStoreAnimal(int age) { petAge=age; }
Fact: To create a derived class,
To create a derived class, you follow the derived class name with a colon, followed by an access specifier and the base class name
Note: When a child class function overrides
When a child class function overrides a parent class function, you can say you have redefined the function
Fact: A child class contains..
all the data members and all the functions of its parent class
protected access specifier
allows members to be used by class member functions and by derived classes, but not by other parts of a program.
parent class
an original class that has been extended, or inherited from
A superclass is an..
ancestor to a subclass
ancestor
any predecessor in an inheritance hierarchy
When a class serves as a...
base class to other classes, all of its members are included when you create an object from a derived class
Inheritance
based on the principle that knowledge of a general category can be applied to more specific objects; means you can create classes that derive their attributes from existing classes; in other words, a newly created class can absorb all the data fields and functions of a class already being used; saves you time because you don't have to start from scratch every time you want to create a class
A child class function..
can override the parent's version
derived
class- a child class, subclass, or descendant type- a data type that is not built into the programming language, but is created by a programmer
virtual
indicates that the base class should be used only once ex: class Student : virtual public Person and class Employee : virtual public Person
OOP say that...
inheritance supports generalization and specialization. That is, base classes are more general, and derived classes are more specialized cases of their base classes.
Class members that are never inherited:
Class members that are never inherited: -constructors -destructors -friend functions -overloaded new operators -overloaded = operators
Fact: A base class is the same
A base class is the same as a superclass, parent class, or ancestor class. A derived class is the same as a subclass, child class, or descendant
Fact: A child class function
A child class function can override the parent's version
Fact: A child class must provide
A child class must provide arguments for all its parents' constructors only if the parents have only non-default constructors
Fact: A child class object can use
A child class object can use its own functions or its parent's protected functions, but a parent object cannot use its child's functions protected functions.
Advantages of deriving new classes from existing ones
-You save time, most code already written -Save more time, bc that code has been tested, its reliable -Save even more time, bc you know how base class works, and you can concentrate on the complexity added as a result of your extensions to the class -In a derived class, you can extend and revise a parent class without corrupting the existing parent class feature (no modification of parent class needed, leave original class alone) -If other classes have been derived from the parent class, the parent class is even more reliable
Problems with Multiple inheritance:
-if 2 parent classes contain members with the same name, then you must use the resolution operator when working with those members -it can get lengthy, more room for error
The access specifiers follow these rules:
-private data and functions can be accessed only within a class -protected data and functions can be accessed only by functions in the same class, or by functions in child classes -public data and functions can be accessed anywhere the class is in scope
When any class member function is called, here are the following steps:
1. The compiler looks for a matching function (name and argument list) in the class of the object using the function name (also called the class of the object invoking the method) 2. If no match is found in the class, the compiler looks for a matching function in the parent class 3. If no match is found in the parents class, the compiler continues up the inheritance hierarchy, looking at the parent of the parent, until the base class is reached 4. If no match is found in any class, an error message is issued
Note: A derived class can have
A derived class can have both virtual and nonvirtual base classes
Note: A derived class object can be assigned
A derived class object can be assigned to a base class object, as in aPerson=worker;. The assignment causes each data member to be copied from worker to aPerson, and leaves off any data for which the base class doesn't have members. The reverse assignment cannot take place without writing a specialized function.
Fact: A derived class with more
A derived class with more than one parent must provide for the constructors of all parents.
Fact: A superclass is an
A superclass is an ancestor to a subclass
Note: Although it is not required
Although it is not required, if you list child class constructor arguments in the same order as you list the parent class constructor arguments, your programs will be easier to follow and maintain.
Note: Although many programmers oppose..
Although many programmers oppose creating protected and public data fields in a base class, they might approve of creating protected functions. A protected function is appropriate for a function that provides a service to a class and its descendants, but not to outside methods
Fact: Any child class function with the
Any child class function with the same name and argument list as the parent overrides the parent function.
Fact: Any child class function with the same
Any child class function with the same name as the parent, yet with an argument list that differs from the parent's, overload the parent function
Note:
As proof that multiple inheritance is never required, consider that the widely used object- oriented program- ming languages Java, C#, and SmallTalk allow only single inheritance, and manage quite nicely.
Fact: Base class members that are private,
Base class members that are private, protected, or public can be inherited with private, protected, or public access; therefore there are nine possible combinations.
Note: Because each child is a more specific
Because each child is a more specific instance of a parent, you can create an array of parent class objects, and store either parent or child class objects in each element of the array. If you do this, the child class objects become members of the parent class, losing any more specific child class data.
___ access specifier for inheritance:
C++ programmers usually use the public access specifier for inheritance.
True Fact: Constructors, destructors, friend functions
Constructors, destructors, friend functions, overloaded new operators, and overloaded = operators cannot be inherited.
True Fact: Data and functions that are private
Data and functions that are private can be accessed only within a class; protected data and functions can be accessed only by functions in the same class, or by functions in child classes; public data and functions can be accessed anywhere.
Note: Many of the constructor complication
Many of the constructor complications that occur when inheriting from multiple classes are minimized if you always include a default constructor for every class.
Remember: For most C++ classes...
For most C++ classes, data is private or protected, and most functions are public. -- Most inheritance is activated with public access so that child class members retain the same access that's available in the parent class.
Note: If a base class contains a function
If a base class contains a function that the derived class should not have, you can create a dummy,or empty, function with the same name in the derived class. If a derived class object uses this function name, no statements are executed.
Note: If a base class contains a function...
If a base class contains a function through which a field is modified (such as a typical set function), it generally is considered good practice to use that function from within a derived class, even though the derived class has direct access to the field. That way, if the base class function enforces restrictions on a field's values, the same restrictions will automatically be enforced in the derived class.
Fact: If a base class does not contain a default
If a base class does not contain a default constructor, then you must provide a constructor for every derived class, even if the derived class does not need a constructor for any other reason.
Note: If a default base class constructor exists
If a default base class constructor exists, then no compiler error arises if you omit the call to the base class constructor when deriving a class. It is perfectly okay to use the default base class constructor with a derived class if that suits your purpose. For example, if PetStoreItem had a default constructor that initialized its fields to zeros, and if you wanted PetStoreAnimal to have those values, then the child class constructor would not have to explicitly call the parent constructor
When private access specifier is used for inheritance
If a derived class uses the private access specifier for inheritance, then the following statements are true: -Base class members that are public become private in the derived class. -Base class members that are protected become private in the derived class. -Base class members that are private are inaccessible in the derived class.
When public access specifier is used for inheritance
If a derived class uses the public access specifier for inheritance, then the following statements are true: -Base class members that are public remain public in the derived class. -Base class members that are protected remain protected in the derived class. -Base class members that are private are inaccessible in the derived class.
Note: If you do not use
If you do not use an access specifier when you create a derived class, access is private by default.
Note: If you fail to call a needed base class constructor
If you fail to call a needed base class constructor for a derived class object, you will receive an error message such as, "No appropriate default constructor available" to initialize the base class
In C#, Java, and SmallTalk...
In C#, Java, and SmallTalk, every new class must inherit from an existing class that is built into the language. In all 3 of these, the ultimate base class is named Object. C++ is sometimes called a hybrid object-oriented programming language because you can create original base classes without deriving them from some other class.
Note: In Chapter 8, you learned
In Chapter 8, you learned to use an initialization list when you create a class that contains a class object as one of its members, and the contained class has a nondefault constructor.
Fact: In a derived class, you can
In a derived class, you can extend and revise a parent class without corrupting the existing parent class features. In other words, you dont have to modify a parent class to get it to work correctly with your new category of objects, you leave the original class alone
Fact: When a child class object uses a function
When a child class object uses a function, C++ first looks for the function in the child class. If it does not find a match, then C++ looks in the parent class. If it doesn't match there, then it checks the parent's parent, if there is one. If it does not find a match all the way up the inheritance hierarchy, then an error would occur
Fact: When a class inherits from two parents
When a class inherits from two parents, both classes are listed after the colon in the class header, separated by a comma.
Note: When you construct a derived
When you construct a derived class object, the base class constructor is called first. When a derived class object is destroyed, the opposite order prevails: The child class destructor is called first and the base class destructor is called last.
Fact: When you instantiate a class object
When you instantiate a class object that has been derived from another class, a constructor for the base class is called first, followed by the derived class constructor
Fact: With a protected inheritance
With a protected inheritance specifier, all public and protected members of the base class become protected in the child class, and all private members of the base class remain inaccessible to the child class
Note: Within the AutomobileInsurancePolicy class, the protected access specifier is overridden
Within the AutomobileInsurancePolicy class, the protected access specifier is overridden for the showPolicy()function. In this example, showPolicy()is allowed more liberal access than are other inherited class members, but the function does not have more liberal access than it originally has in InsurancePolicy. You can never override a function's original access specifier in a parent class to make it more liberal in the child. However, you can override the inherited access to make an individual member's access more conservative
Note: You already use multiple inheritance
You already use multiple inheritance each time you include iostream in a program. Each cin and cout object is derived from other classes.
Fact: you can override the class access
You can override the class access specifier for any specific class members; when you do so for functions, you do not use parentheses after the function name.
You can write any of the following to indicate that a Customer is a Person:
You can write any of the following to indicate that a Customer is a Person: class Customer : public Person class Customer : protected Person class Customer : private Person
Note: You do not have to pass a variable
You do not have to pass a variable to a base class constructor that requires an argument; you can pass a constant of the same type. For example, if you want all PetStoreAnimal objects to cost $99.95, then you might create a PetStoreAnimal constructor such as the following: PetStoreAnimal(int stock, int age) : PetStoreItem(stock, 99.95), petAge(age) {};
when override class access specifier?
You override the class access specifier when it does not suit your needs for some members of a class.
Fact: You use the keyword
You use the keyword virtual when you define child classes to avoid duplicate inheritance.
superclass
a base or parent class
derived class
a child class, subclass, or descendant derived type- a data type that is not built into the programming language, but is created by a programmer
descendant
a class extended from an existing class
base class
a class from which another is derived. Also called a parent class, superclass, or ancestor
child class
a derived class
subclass
a derived or child class
generalization and specialization
a principle, which inheritance supports, that base classes are more general, and derived classes are more specialized cases of their base classes
A base class is the same as..
a superclass, parent class, or ancestor class. A derived class is the same as subclass, child class, or descendant
Multiple inheritance is never..
needed to solve a programming problem
NOTE: not only are friend functions
not only are friend functions not inherited; class friendship also is not inherited
multiple inheritance
when a child class also can derive from more than one base class
single inheritance
when a child class derives from a single parent
How to create a derived class:
you include the following elements in the order listed: -keyword class -derived class name -colon -class access specifier, either public, private, or protected -base class name -opening brace -class definition statements -closing brace -semicolon
override
you substitute one version of the function for another