Understanding Inheritance

Slides:



Advertisements
Similar presentations
More on Classes Inheritance and Polymorphism
Advertisements

Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Chapter 10: Introduction to Inheritance
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
Understanding Inheritance Object-Oriented Programming Using C++ Second Edition 9.
Using Templates Object-Oriented Programming Using C++ Second Edition 11.
2 Objectives You should be able to describe: Operator Functions Two Useful Alternatives – operator() and operator[] Data Type Conversions Class Inheritance.
Chapter 13: Object-Oriented Programming
Chapter 10 Classes Continued
1 Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Chapter 12: Adding Functionality to Your Classes.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Chapter 8 More Object Concepts
An Object-Oriented Approach to Programming Logic and Design
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
1 Using Templates COSC 1567 C++ Programming Lecture 10.
Programming Fundamentals1 Chapter 8 OBJECT MANIPULATION - INHERITANCE.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
CITA 342 Section 1 Object Oriented Programming (OOP)
CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.4 Constructors, Overloading,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Classes, Interfaces and Packages
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Object-Oriented Programming: Inheritance and Polymorphism.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CSCE 240 – Intro to Software Engineering Lecture 3.
A First Book of C++ Chapter 12 Extending Your Classes.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Inheritance and Polymorphism
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Object-Oriented Programming: Classes and Objects
Inheritance and Polymorphism
03/10/14 Inheritance-2.
Chapter 3: Using Methods, Classes, and Objects
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Nested class.
Inheritance, Polymorphism, and Interfaces. Oh My
Chapter 14 Inheritance Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Inheritance Basics Programming with Inheritance
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Lecture 22 Inheritance Richard Gesick.
Chapter 9: Polymorphism and Inheritance
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Object-Oriented Programming Using C++ Second Edition
Object-Oriented Programming Using C++ Second Edition
Classes and Objects.
Computer Programming with JAVA
Java – Inheritance.
Chapter 6 Control Statements: Part 2
Java Programming, Second Edition
Object-Oriented Programming: Inheritance and Polymorphism
Object-Oriented Programming Using C++ Second Edition
Object-Oriented Programming: Inheritance
Fundaments of Game Design
Inheritance.
Chapter 8: Class Relationships
Chapter 9 Inheritance.
Using C++ Arithmetic Operators and Control Structures
Presentation transcript:

Understanding Inheritance 9 Understanding Inheritance Object-Oriented Programming Using C++ Second Edition

9 Objectives In this chapter, you will learn: About inheritance and its benefits How to create a derived class About restrictions imposed with inheritance How to choose a class access specifier How to override and overload parent class functions within a child class How to use a constructor initialization list

9 Objectives In this chapter, you will learn: How to provide for a base class constructor within a derived class How to override the class access specifier How to use multiple inheritance About the special problems posed by multiple inheritance How to use virtual inheritance

Understanding Inheritance 9 Understanding Inheritance Objects used in computer programs also are easier to understand if you can place them within a hierarchy of inheritance Suppose you have written several programs with a class named Student The FirstYearStudent class inherits from the Student class, or is derived from it The Student class is called a parent class, base class, superclass, or ancestor; the FirstYearStudent class is called a child class, derived class, subclass, or descendant When you override a function, you substitute one version of the function for another

Understanding the Advantages Provided by Inheritance 9 Understanding the Advantages Provided by Inheritance Inheritance is considered a basic building block of object-oriented programming Programs in which you derive new classes from existing classes offer several advantages: You save time because much of the code needed for your class is already written You save additional time because the existing code has already been tested—that is, you know it works correctly; it is reliable

Understanding the Advantages Provided by Inheritance 9 Understanding the Advantages Provided by Inheritance Advantages of using derived classes: You save even more time because you already understand how the 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 features If other classes have been derived from the parent class, the parent class is even more reliable—the more its code has been used, the more likely it is that logical errors have already been found and fixed

Creating a Derived Class 9 Creating a Derived Class The class, named Person, is shown in Figure 9-1 It contains three fields, and two member functions that set and display the data field values

Creating a Derived Class 9 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

Creating a Derived Class 9 The Customer class shown in Figure 9-2 contains all the members of Person because it inherits them

The Customer Class 9

Creating a Derived Class 9 Creating a Derived Class Ex9-1

Creating a Derived Class 9 In the set of steps outlined on pages 317 to 319 of the textbook, you create two classes, Car and Convertible The Car class serves as a base class and includes features common to all cars

Creating a Derived Class 9 Creating a Derived Class Ex9-2 You create the Convertible class and a demonstration program in the steps referred to on pages 319 and 320 of the textbook

Understanding Inheritance Restrictions 9 Member accessibilities of the members of the base class in the derived classes

Understanding Inheritance Restrictions 9 Understanding Inheritance Restrictions The protected specifier allows members to be used by class member functions and by derived classes, but not by other parts of a program

Choosing the Class Access Specifier 9 C++ programmers usually use the public access specifier for inheritance If a derived class uses the public access specifier, 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

Choosing the Class Access Specifier 9 If a derived class uses the protected access specifier, then the following statements are true: Base class members that are public become protected 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

Choosing the Class Access Specifier 9 If a derived class uses the private access specifier, 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

Choosing the Class Access Specifier 9 Choosing the Class Access Specifier

Choosing the Class Access Specifier 9 Choosing the Class Access Specifier

Choosing the Class Access Specifier 9 Choosing the Class Access Specifier A main() program that instantiates an Introvert object cannot access any of the four data items directly; all four fields are private to the Introvert class

Choosing the Class Access Specifier 9 Choosing the Class Access Specifier

Choosing the Class Access Specifier 9 To summarize the use of three class access specifiers with class data members: If a class has private data members, they can be used only by member functions of that class If a class has protected data members, they can be used by member functions of that class and by member functions of derived classes If a class has public data members, they can be used by member functions of that class, by member functions of derived classes, and by any other functions’ including the main() function of a program

Choosing the Class Access Specifier 9 Choosing the Class Access Specifier With one generation of inheritance, there are nine possible combinations, as shown in Table 9-1 Remember the following: Private data can be accessed only by a class’s member functions (or friend functions), and not by any functions in derived classes If a class serves as a base class, most often its data members are protected, and its member functions are public The access specifier in derived classes is most often public, so that the derived class can refer to all non-private data and functions of the base class When a class is derived from another derived class, the newly derived class never has any more liberal access to a base class member than does its immediate predecessor

Summary of Child Class Member Access Rules 9 Ex9-3 check errors

Overriding and Overloading Parent Class Functions 9 Overriding and Overloading Parent Class Functions When a new class is derived from an existing class, the derived class has access to non-private member functions in the base class The new class also can have its own member functions Those functions can have names that are identical to the function names in the base class Any child class function with the same name and argument list as the parent overrides the parent function; No overloading across scopes.

Overriding and Overloading Parent Class Functions 9 Overriding and Overloading Parent Class Functions

Overriding and Overloading Parent Class Functions 9 Overriding and Overloading Parent Class Functions

Overriding and Overloading Parent Class Functions 9 Overriding and Overloading Parent Class Functions Ex9-4

Overriding and Overloading Parent Class Functions 9 Overriding and Overloading Parent Class Functions The output in Figure 9-21 shows that even though you set fields for a Person and an Employee by using separate functions that require separate argument lists, you use the same outputData() function that exists within the parent class for both a Person and an Employee

Overriding and Overloading Parent Class Functions 9 Overriding and Overloading Parent Class Functions Ex9-4

Overriding and Overloading Parent Class Functions 9 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 When any class member function is called, the steps shown on page 333 of the textbook take place

Overriding and Overloading Parent Class Functions 9 Overriding and Overloading Parent Class Functions Ex9-5 In the set of steps outlined on pages 333 and 334 of the textbook, you create a RaceCar class as a child of the Car class you created earlier A RaceCar has all the attributes of a Car, but its maximum allowed speed is higher Overriding a base class member function with a derived member function demonstrates the concept of polymorphism

Using Constructor Initialization Lists 9 Many classes use a constructor function to provide initial values for fields when a class object is created; that is, many functions simply contain a series of assignment statements As an alternative to the separate prototype and implementation of the constructor shown in Figure 9-25, you can implement the constructor within the declaration section for the class as an inline function

The Item Class 9

Using Constructor Initialization Lists 9 As another alternative, you can replace the assignment statements within a constructor body for Item with a constructor initialization list A constructor initialization list provides values for class fields in the same statement that contains the constructor definition

Using Constructor Initialization Lists 9 Ex9-6 The constructor initialization list shown in Figure 9-27 initializes itemNum with the value of n and itemPrice with the value of p

Understanding the Difference Between Assignment and Initialization 9 Understanding the Difference Between Assignment and Initialization The difference between assignment and initialization is often very subtle, and programmers sometimes mingle the two terms rather casually When you declare a simple scalar variable and give it a value, you can declare and assign in two separate statements, for example: int z; z = 100; Alternatively, you can initialize a variable, declaring it and assigning it a value within a single statement, for example: int z = 100; OR int z(100);

Understanding the Difference Between Assignment and Initialization 9 Understanding the Difference Between Assignment and Initialization There are at least four reasons to understand the use of constructor initialization lists: Many C++ programmers prefer this method, so it is used in many programs Technically, a constructor should initialize rather than assign values Reference variables and constant class members cannot be assigned values; they must be initialized When you create a derived class and instantiate an object, a parent class object must be constructed first. You add a constructor initialization list to a derived class constructor to construct the parent class.

Providing for Base Class Construction 9 When you instantiate an object in a C++ program, you automatically call its constructor function This pattern holds true whether you write a custom constructor or use a default constructor When you instantiate a derived class object, a constructor for its base class is called first, followed by the derived class constructor This format is followed even if the base and derived classes both have only default constructors

Providing for Base Class Construction 9 Providing for Base Class Construction

Providing for Base Class Construction 9 If PetStoreAnimal were a simple base class, the PetStoreAnimal class constructor would require just an integer argument that would be assigned to the petAge field However, because PetStoreAnimal is derived from PetStoreItem, a PetStoreAnimal object is constructed, and the PetStoreItem class constructor also will be called The prototype for the PetStoreAnimal class constructor must provide values for all the arguments it needs as well as all the arguments its parent needs

The PetStoreAnimal Class 9

Providing for Base Class Construction 9 Ex9-7 Figure 9-31 shows one more option for coding the PetStoreAnimal class In this example, the constructor initialization list initializes both PetStoreItem and petAge

Overriding Inherited Access 9 Nine inheritance access specifier combinations are possible: base class members that are private, protected, or public can be inherited with private, protected, or public access In addition, you can override the class access specifier for any specific class members

The DemoBasePerson Class 9

Overriding Inherited Access 9 If a derived class, SomewhatShy, uses the protected access specifier when inheriting from DemoBasePerson, then the following statements hold true: The field named salary, which is private in DemoBasePerson, is inaccessible in the derived class The field named age, which is protected in the base class, remains protected in the derived class The field named initial, which is public in the base class, becomes protected in the derived class The function setPerson() and showPerson(), which are public in DemoBasePerson, become protected in the derived class

Overriding Inherited Access 9 Overriding Inherited Access For the showPerson() function to become public within SomewhatShy, the highlighted statement in Figure 9-33 must appear in the public section of the SomewhatShy class Additionally, the DemoBasePerson class name and scope resolution operator must appear before the showPerson() function name Finally, and most oddly, no parentheses appear after the showPerson() function name within the child class

Overriding Inherited Access 9 Overriding Inherited Access Ex9-8 However, you can override the inherited access to make an individual member’s access more conservative For most C++ classes, data is private or protected, and most functions are public

Summary 9 Inheritance allows you to create classes that derive most of their attributes from existing classes Programs in which you create classes that are derived from existing classes offer several advantages: you save time because much of the code needed for your class is already written and tested To create a derived class, you include the keyword class, the derived class name, a colon, a class access specifier, and the base class name in the class header; as with other classes, definition statements within a pair of curly braces follow

Summary 9 When a class serves as a base class to others, all of its members can be used within the child class functions, except for any private members When you define a derived class, you can insert one of the three class access specifiers (public, private, or protected) just prior to the base class name You can override parent class functions within a child class As an alternative to separate prototypes and implementation of a constructor, you can use a constructor initialization list

Summary 9 When you declare a variable without an initial value, it holds garbage until you make an assignment If a base class does not contain a default constructor, then you must provide a constructor for use by the derived class You can override the class access specifier when it does not suit your needs for some of the members of the class