DOC PREVIEW
UT Dallas CS 4337 - Chapter15 Class3 Inheritance & Polymorphism

This preview shows page 1-2-3-4-26-27-28-54-55-56-57 out of 57 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

PowerPoint Presentation15.1What Is Inheritance?Example: InsectsThe "is a" RelationshipInheritance – Terminology and NotationBack to the ‘is a’ RelationshipWhat Does a Child Have?15.2Protected Members and Class AccessClass Access SpecifiersInheritance vs. AccessMore Inheritance vs. AccessMore Inheritance vs. Access (2)More Inheritance vs. Access (3)15.3Constructors and Destructors in Base and Derived ClassesSlide 18Slide 19Slide 20Passing Arguments to Base Class ConstructorSlide 2215.4Redefining Base Class FunctionsSlide 25Base ClassSlide 27Slide 28Problem with RedefiningSlide 3015.5Class HierarchiesSlide 3315.6Polymorphism and Virtual Member FunctionsSlide 36Slide 37Slide 38Static BindingVirtual FunctionsSlide 41Slide 42Slide 43Slide 44Slide 45Polymorphism Requires References or PointersBase Class PointersSlide 48Redefining vs. OverridingVirtual Destructors15.7Abstract Base Classes and Pure Virtual FunctionsAbstract Base Classes and Pure Virtual Functions15.8Multiple InheritanceSlide 56Slide 57Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Chapter 15:Inheritance, Polymorphism, and Virtual FunctionsCopyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.15.1What Is Inheritance?Copyright © 2012 Pearson Education, Inc.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 classCopyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Example: InsectsCopyright © 2012 Pearson Education, Inc.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 athleteCopyright © 2012 Pearson Education, Inc.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.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 classCopyright © 2012 Pearson Education, Inc.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 classAn object of the derived class can use:•all public members defined in child class•all public members defined in parent class•See GradeActivity Version 1Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.15.2Protected Members and Class AccessCopyright © 2012 Pearson Education, Inc.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 classCopyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Class Access Specifiers1) 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 parents3) private – prevents objects of derived class from being treated as objects of base class.See GradeActivity Version 2Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Inheritance vs. Access private: xprotected: ypublic: zprivate: xprotected: ypublic: zprivate: xprotected: ypublic: zBase class membersx is inaccessibleprivate: yprivate: zx is inaccessibleprotected: yprotected: zx is inaccessibleprotected: ypublic: zHow inherited base class membersappear in derived classprivatebase classprotectedbase classpublicbase classCopyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.More Inheritance vs. Accessprivate members: char letter; float score; void calcGrade();public members: void setScore(float); float getScore(); char getLetter();class Gradeprivate members: int numQuestions; float pointsEach; int numMissed;public members: Test(int, int);class Test : public GradeWhen Test class inheritsfrom Grade class using public class access, it looks like this:private members: int numQuestions: float pointsEach; int numMissed;public members: Test(int, int); void setScore(float); float getScore(); float getLetter();Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.More Inheritance vs. Access (2)private members: char letter; float score; void calcGrade();public members: void setScore(float); float getScore(); char getLetter();class Gradeprivate members: int numQuestions; float pointsEach; int numMissed;public members: Test(int, int);When Test class inheritsfrom Grade class using protected class access, it looks like this:private members: int numQuestions: float pointsEach; int numMissed;public members: Test(int, int);protected members: void setScore(float); float getScore(); float getLetter();class Test : protected GradeCopyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.More Inheritance vs. Access (3)private members: int numQuestions: float pointsEach; int numMissed; void setScore(float); float getScore(); float getLetter();public members: Test(int, int); private members: char letter; float score; void calcGrade();public members: void setScore(float); float getScore(); char getLetter();class Gradeprivate members: int numQuestions; float pointsEach; int numMissed;public members: Test(int, int);When Test class inheritsfrom Grade class using private class access, it looks like this:class Test : private GradeCopyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.15.3Constructors and Destructors in Base and Derived ClassesCopyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Constructors and Destructors in Base and Derived Classes•Derived


View Full Document

UT Dallas CS 4337 - Chapter15 Class3 Inheritance & Polymorphism

Download Chapter15 Class3 Inheritance & Polymorphism
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 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 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?