UT Dallas CS 4337 - Chapter15 Class3 Inheritance & Polymorphism (57 pages)

Previewing pages 1, 2, 3, 4, 26, 27, 28, 54, 55, 56, 57 of 57 page document View the full content.
View Full Document

Chapter15 Class3 Inheritance & Polymorphism



Previewing pages 1, 2, 3, 4, 26, 27, 28, 54, 55, 56, 57 of actual document.

View the full content.
View Full Document
View Full Document

Chapter15 Class3 Inheritance & Polymorphism

36 views


Pages:
57
School:
University of Texas at Dallas
Course:
Cs 4337 - Organization of Programming Languages
Unformatted text preview:

Chapter 15 Inheritance Polymorphism and Virtual Functions Copyright 2012 Pearson Education Inc 15 1 What Is Inheritance Copyright 2012 Pearson Education Inc What Is Inheritance Provides a way to create a new class from an existing class The new class is a specialized version of the existing class Copyright 2012 Pearson Education Inc Example Insects Copyright 2012 Pearson Education Inc The is a Relationship Inheritance establishes an is a relationship between classes A poodle is a dog A car is a vehicle A flower is a plant A football player is an athlete Copyright 2012 Pearson Education Inc Inheritance Terminology and Notation Base class or parent inherited from Derived class or child inherits from the base class Notation class Student base class class UnderGrad public student derived class Copyright 2012 Pearson Education Inc Back to the is a Relationship An object of a derived class is a n object of the base class Example an UnderGrad is a Student a Mammal is an Animal A derived object has all of the characteristics of the base class Copyright 2012 Pearson Education Inc What Does a Child Have An object of the derived class has all members defined in child class all members declared in parent class An object of the derived class can use all public members defined in child class all public members defined in parent class See GradeActivity Version 1 Copyright 2012 Pearson Education Inc 15 2 Protected Members and Class Access Copyright 2012 Pearson Education Inc Protected Members and Class Access protected member access specification like private but accessible by functions in a derived class inaccessible to the rest of program Class access specification determines how private protected and public members of base class are inherited by the derived class Copyright 2012 Pearson Education Inc Class Access Specifiers 1 public object of derived class can be treated as object of base class not vice versa 2 protected more restrictive than public but allows derived classes to know details of parents 3 private prevents objects of derived class from being treated as objects of base class See GradeActivity Version 2 Copyright 2012 Pearson Education Inc Inheritance vs Access Base class members How inherited base class members appear in derived class x is inaccessible private y private z private x protected y public z private base class private x protected y public z protecte d base class x is inaccessible protected y protected z public base class x is inaccessible protected y public z private x protected y public z Copyright 2012 Pearson Education Inc More Inheritance vs Access class Grade private members char letter float score void calcGrade public members void setScore float float getScore char getLetter When Test class inherits from Grade class using public class access it looks like this Copyright 2012 Pearson Education Inc class Test public Grade private members int numQuestions float pointsEach int numMissed public members Test int int private members int numQuestions float pointsEach int numMissed public members Test int int void setScore float float getScore float getLetter More Inheritance vs Access 2 class Grade private members char letter float score void calcGrade public members void setScore float float getScore char getLetter When Test class inherits from Grade class using protected class access it looks like this Copyright 2012 Pearson Education Inc class Test protected Grade private members int numQuestions float pointsEach int numMissed public members Test int int private members int numQuestions float pointsEach int numMissed public members Test int int protected members void setScore float float getScore float getLetter More Inheritance vs Access 3 class Grade private members char letter float score void calcGrade public members void setScore float float getScore char getLetter When Test class inherits from Grade class using private class access it looks like this Copyright 2012 Pearson Education Inc class Test private Grade private members int numQuestions float pointsEach int numMissed public members Test int int private members int numQuestions float pointsEach int numMissed void setScore float float getScore float getLetter public members Test int int 15 3 Constructors and Destructors in Base and Derived Classes Copyright 2012 Pearson Education Inc Constructors and Destructors in Base and Derived Classes Derived classes can have their own constructors and destructors When an object of a derived class is created the base class s constructor is executed first followed by the derived class s constructor When an object of a derived class is destroyed its destructor is called first then that of the base class Copyright 2012 Pearson Education Inc Constructors and Destructors in Base and Derived Classes Copyright 2012 Pearson Education Inc Copyright 2012 Pearson Education Inc Program 5 14 Continued Copyright 2012 Pearson Education Inc Passing Arguments to Base Class Constructor Allows selection between multiple base class constructors Specify arguments to base constructor on derived constructor heading Square Square int side Rectangle side side Can also be done with inline constructors Must be done if base class has no default constructor Copyright 2012 Pearson Education Inc Passing Arguments to Base Class Constructor derived class constructor base class constructor Square Square int side Rectangle side side derived constructor parameter See Cube h and Pr15 5 cpp for example Copyright 2012 Pearson Education Inc base constructor parameters 15 4 Redefining Base Class Functions Copyright 2012 Pearson Education Inc Redefining Base Class Functions Redefining function function in a derived class that has the same name and parameter list as a function in the base class Typically used to replace a function in base class with different actions in derived class Copyright 2012 Pearson Education Inc Redefining Base Class Functions Not the same as overloading with overloading parameter lists must be different Objects of base class use base class version of function objects of derived class use derived class version of function See Pr15 8 cpp Copyright 2012 Pearson Education Inc Base Class Note setScore function 15 26 Copyright 2012 Pearson Education Inc Derived Class Redefined setScore function 15 27 Copyright 2012 Pearson Education Inc From Program 15 7 Copyright 2012 Pearson Education Inc Problem with Redefining Consider this situation Class BaseClass defines functions x and y x calls y


View Full Document

Access the best Study Guides, Lecture Notes and Practice Exams

Loading Unlocking...
Login

Join to view Chapter15 Class3 Inheritance & Polymorphism 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 Chapter15 Class3 Inheritance & Polymorphism 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?