Unformatted text preview:

Slide 1ObjectivesSlide 3Inheritance and CompositionInheritanceInheritance (continued)Slide 7Slide 8Slide 9Redefining (Overriding) Member Functions of the Base ClassRedefining (Overriding) Member Functions of the Base Class (continued)Constructors of Derived and Base ClassesHeader File of a Derived ClassMultiple InclusionsC++ Stream ClassesSlide 16C++ Stream Classes (continued)Slide 18Protected Members of a ClassPublic InheritanceProtected InheritancePrivate InheritanceOOD and OOPOOD and OOP (continued)Slide 25Slide 26Slide 27Slide 28Classes, Objects, & OperationsClasses, Objects, & Operations (continued)Slide 31Slide 32Slide 33Slide 34SummarySlide 36Slide 37Slide 38Chapter 13: Inheritance2ObjectivesIn this chapter you will:•Learn about inheritance•Learn about derived and base classes•Explore how to redefine the member functions of a base class•Examine how the constructors of base and derived classes work•Learn how to construct the header file of a derived class3Objectives•Explore three types of inheritance: public, protected, and private•Become familiar with the three basic principles of object-oriented design4Inheritance and Composition•The two common ways to relate two classes in a meaningful way are:1. Inheritance (“is-a” relationship)2. Composition (“has-a” relationship)5Inheritance•Inheritance is an “is-a” relationship•For instance,“every employee is a person”•Inheritance lets us create new classes from existing classes•New classes are called the derived classes•Existing classes are called the base classes•Derived classes inherit the properties of the base classes6Inheritance (continued)•Single inheritance: derived class has a single base class•Multiple inheritance: derived class has more than one base class•Can be viewed as a tree (hierarchy) where a base class is shown with its derived classes•Public inheritance: all public members of base class are inherited as public members by derived class8Inheritance (continued)•Private members of the base class are private to the base class–Members of the derived class cannot directly access them•Public members of a base class can be inherited either as public members or as private members by the derived class•The derived class can include additional data and/or function members9Inheritance (continued)•Derived class can redefine public member functions of base class•Redefinition applies only to objects of the derived class, not to the base class•All data/function members of the base class are also data/function members of the derived class10Redefining (Overriding) Member Functions of the Base Class•To redefine a public member function of a base class–Corresponding function in the derived class must have the same name, number, and types of parameters11Redefining (Overriding) Member Functions of the Base Class (continued)•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 with the appropriate parameter list12Constructors of Derived and Base Classes•Derived class constructor cannot directly access private members of the base class•Derived class can initialize private data members of the derived class•When a derived object is declared–It must execute one of the base class constructors•Call to the base class constructor is specified in the heading of derived class constructor definition13Header File of a Derived Class•To define new classes–Create new header files•To create new classes based on previously defined classes–Header files of the new classes contain commands that specify where to look for the definitions of the base classes•The definitions of the member functions can be placed in a separate file14Multiple Inclusions•Use the preprocessor command (#include) to include a header file in a program•The preprocessor processes the program before it is compiled•To avoid multiple inclusion of a file in a program–Use certain preprocessor commands in the header file (“file guard”)15C++ Stream Classes•ios is the base class for all stream classes•istream and ostream are derived from ios•ifstream is derived from istream•ofstream is derived from the ostream•ios contains formatting flags and member functions to access/modify the flag settings17C++ Stream Classes (continued)•istream and ostream provide operations for data transfer between memory and devices•istream defines the extraction operator (>>) and functions such as get and ignore•ostream defines the insertion operator (<<), which is used by cout18C++ Stream Classes (continued)•ifstream is derived from istream for file input•ofstream is derived from ostream for file output•Objects of type ifstream are for file input•Objects of type ofstream are for file output•Header file fstream contains the definitions of ifstream and ofstream19Protected Members of a Class•Private members of a class cannot be directly accessed outside the class•For a base class to give derived class access to a private member–Declare that member as protected•The accessibility of a protected member of a class is in between public and private•A derived class can directly access the protected member of the base class20Public InheritanceIf the memberAccessSpecifier is public, then•Public members of A (base) are public members of B (derived) and can be directly accessed in class B•Protected members of A are protected members of B and can be directly accessed by the member functions (and friend functions) of B•Private members of A are hidden in B and can be accessed by member functions (and friend functions) of B through public or protected members of A21Protected InheritanceIf the memberAccessSpecifier is protected, then•Public members of A are protected members of B and can be accessed by the member functions (and friend functions) of B•Protected members of A are protected members of B and can be accessed by the member functions (and friend functions) of B•Private members of A are hidden in B and can be accessed by member functions (and friend functions) of B through public or protected members of A22Private InheritanceIf the memberAccessSpecifier is private, then•Public members of A are private members of B and can be accessed by member functions (and friend functions) of B•Protected members of A are private members of B and can


View Full Document

Oneonta CSCI 116 - Lecture Notes

Documents in this Course
Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?