CS2308 C++ Inheritance
composition (aggregation)
"has-a" relation - one or more member(s) of a class are objects of another class type
inheritance
"is-a" relationship ex: "every employee is a person" - allows creation of new classes from existing classes - helps reduce software complexity
member-object construction
- Arguments to the constructor of a member-object are specified in the heading part of the definition of the constructor - Member-objects of a class are constructed in the order they are declared - Not in the order listed in the constructor's member initialization list - They are constructed before the containing class objects are constructed
constructors
- derived class constructor cannot directly access private members of the base class - it can directly initialize only public member variables of the base class - when a derived object is declared, it must execute one of the base class constructors - call to base class constructor is specified in heading of derived class constructor definition
two common ways to relate two classes in a meaningful way are:
- inheritance ("is-a" relationship) - composition, or aggregation: ("has-a" relationship)
specifying base class constructor
- it is possible to specify base class constructor and pass the parameters to it - you specify the constructor by passing arguments ex: Square::Square(int side) : Rectangle(side, side)
if derived class overrides a public member function of the base class, then to call the base class function, specify:
- name of the base class - scope resolution operator (::) - function name w/ appropriate parameter list
steps to writing program
- pick class name from list of nouns - pick nouns that are characteristics of class noun - determine 3 pieces of info about class --> operations that an object can perform --> operations that can be performed on an object --> info that an object must maintain - from verbs, list possible operations that an object of that class can perform, or have performed, on itself
public inheritance
- public members of base class become public members of derived class - protected members of base class become protected members of derived class - base class's private members never accessible directly from derived class --> can be accessed through calls to the public and protected members of base class
inheritance rules
- public members of base class can be inherited as public or private members - derived class can include additional members (data and/or functions) - derived class can redefine public member functions of the base class - applies only to the objects of the derived class - all member variables of the base class are also member variables of the derived class
destructors
- when a derived class object goes out of scope - automatically invokes its destructor - when the destructor of the derived class executes - automatically invokes the destructor of the base class
public inheritance
all public members of base class are inherited as public members by derived class
to find classes:
begin w/ a problem description and identify all nouns and verbs --> from the list of nouns choose the classes --> from the list of verbs choose the operations
syntax of a derived class
class className: memberAccessSpecifier baseClassName { member list }; - memberAccessSpecifier is public, protected, or private (default)
encapsulation
combines data and operations on data in a single unit
to redefine a public member:
corresponding function in derived class must have same name/number/types of parameters
what are an objects internal and external state?
private members form the internal state public members form the external state
istream and ostream
provide operations for data transfer between memory and devices
private inheritance
public an protected members of base class become private members of derived class --> derived class cannot directly access private members of its base class --> to give it direct access, declare that member as protected
protected inheritance
public and protected members of base class become protected members of derived class
polymorphism
the ability to use the same expression to denote diff operations
ios
the base class for all stream classes - contains formatting flags and member functions to access/modify the flag settings
base class
the original class
ifstream/ofstream objects
used for file I/O
istream
defines the extraction operator (>>) and functions get and ignore
ostream
defines the insertion operator (<<) which is used by cout
single inheritance
derived class has a single base class
multiple inheritance
derived class has more than one base class
the fundamental principles of object-oriented design (OOD) are:
encapsulation, inheritance, polymorphism
polymorphic function or operator
has many forms ex: division w/ floating point and division w/ integer operands
derived classes
new classes created from the existing class - inherits the properties of its base classes
can derived classes access private members of a base class?
no, private members of a base class are private to the base class