DOC PREVIEW
IUPUI CS 265 - Inheritance

This preview shows page 1-2-3-20-21-22-41-42-43 out of 43 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 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 43 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 43 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 43 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 43 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 43 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 43 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 43 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 43 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Chapter 19 - Inheritance19.1 Introduction19.1 Introduction (II)19.2 Base and Derived Classes19.2 Base and Derived Classes (II)19.3 protected members19.4 Casting Base Class Pointers to Derived Class Pointers19.4 Casting Base-Class Pointers to Derived-Class Pointers (II)PowerPoint PresentationSlide 10Slide 11Slide 12Slide 13Slide 1419.5 Using Member Functions19.6 Overriding Base-Class Members in a Derived ClassSlide 17Slide 18Slide 19Slide 2019.7 public, private, and protected Inheritance19.8 Direct and Indirect Base Classes19.9 Using Constructors and Destructors in Derived Classes19.9 Using Constructors and Destructors in Derived Classes (II)Slide 25Slide 26Slide 27Slide 28Slide 2919.10 Implicit Derived-Class Object to Base-Class Object Conversion19.10 Implicit Derived-Class Object to Base-Class Object Conversion (II)19.11 Software Engineering With Inheritance9.12 Composition vs. Inheritance9.13 “Uses A” And “Knows A” Relationships9.14 Case Study: Point, Circle, CylinderSlide 36Slide 37Slide 38Slide 39Slide 40Slide 41Slide 42Slide 43 2000 Prentice Hall, Inc. All rights reserved.Chapter 19 - InheritanceOutline19.1 Introduction19.2 Inheritance: Base Classes and Derived Classes19.3 Protected Members19.4 Casting Base-Class Pointers to Derived-Class Pointers19.5 Using Member Functions19.6 Overriding Base-Class Members in a Derived Class19.7 Public, Protected and Private Inheritance19.8 Direct Base Classes and Indirect Base Classes19.9 Using Constructors and Destructors in Derived Classes19.10 Implicit Derived-Class Object to Base-Class Object Conversion19.11 Software Engineering with Inheritance19.12 Composition vs. Inheritance19.13 “Uses A” and “Knows A” Relationships19.14 Case Study: Point, Circle, Cylinder 2000 Prentice Hall, Inc. All rights reserved.19.1 Introduction•Inheritance–New classes created from existing classes–Absorb attributes and behaviors. •Polymorphism–Write programs in a general fashion –Handle a wide variety of existing (and unspecified) related classes•Derived class–Class that inherits data members and member functions from a previously defined base class 2000 Prentice Hall, Inc. All rights reserved.19.1 Introduction (II)•Inheritance–Single Inheritance •Class inherits from one base class–Multiple Inheritance •Class inherits from multiple base classes–Three types of inheritance:•public: Derived objects are accessible by the base class objects (focus of this chapter)•private: Derived objects are inaccessible by the base class•protected: Derived classes and friends can access protected members of the base class 2000 Prentice Hall, Inc. All rights reserved.19.2 Base and Derived Classes•Often an object from a derived class (subclass) “is an” object of a base class (superclass)Base class Derived classes Student GraduateStudent UndergraduateStudent Shape Circle Triangle Rectangle Loan CarLoan HomeImprovementLoan MortgageLoan Employee FacultyMember StaffMember Account CheckingAccount SavingsAccount 2000 Prentice Hall, Inc. All rights reserved.19.2 Base and Derived Classes (II)•Implementation of public inheritanceclass CommissionWorker : public Employee { ...}; Class CommissionWorker inherits from class Employee–friend functions not inherited–private members of base class not accessible from derived class 2000 Prentice Hall, Inc. All rights reserved.19.3 protected members•protected inheritance–Intermediate level of protection between public and private inheritance–Derived-class members can refer to public and protected members of the base class simply by using the member names–Note that protected data “breaks” encapsulation 2000 Prentice Hall, Inc. All rights reserved.19.4 Casting Base Class Pointers to Derived Class Pointers•Object of a derived class –Can be treated as an object of the base class–Reverse not true - base class objects not a derived-class object•Downcasting a pointer–Use an explicit cast to convert a base-class pointer to a derived-class pointer–Be sure that the type of the pointer matches the type of object to which the pointer pointsderivedPtr = static_cast< DerivedClass * > basePtr; 2000 Prentice Hall, Inc. All rights reserved.19.4 Casting Base-Class Pointers to Derived-Class Pointers (II)•Example–Circle class derived from the Point base class–We use pointer of type Point to reference a Circle object, and vice-versa 2000 Prentice Hall, Inc. All rights reserved.Outline1. Point class definition-------------1. Load header1.1 Function definitions1 // Fig. 19.4: point.h2 // Definition of class Point3 #ifndef POINT_H4 #define POINT_H56 #include <iostream>78 using std::ostream;910 class Point {11 friend ostream &operator<<( ostream &, const Point & );12 public:13 Point( int = 0, int = 0 ); // default constructor14 void setPoint( int, int ); // set coordinates15 int getX() const { return x; } // get x coordinate16 int getY() const { return y; } // get y coordinate17 protected: // accessible by derived classes18 int x, y; // x and y coordinates of the Point19 };2021 #endif22 // Fig. 19.4: point.cpp23 // Member functions for class Point24 #include <iostream>25 #include "point.h"2627 // Constructor for class Point28 Point::Point( int a, int b ) { setPoint( a, b ); }2930 // Set x and y coordinates of Point31 void Point::setPoint( int a, int b )32 {33 x = a; 2000 Prentice Hall, Inc. All rights reserved.Outline1.1 Function definitions---------------------1. Circle class definition34 y = b;35 }3637 // Output Point (with overloaded stream insertion operator)38 ostream &operator<<( ostream &output, const Point &p )39 {40 output << '[' << p.x << ", " << p.y << ']';4142 return output; // enables cascaded calls43 }44 // Fig. 19.4: circle.h 45 // Definition of class Circle46 #ifndef CIRCLE_H47 #define CIRCLE_H4849 #include <iostream>5051 using std::ostream;5253 #include <iomanip>5455 using std::ios;56 using std::setiosflags;57 using std::setprecision;5859 #include "point.h"6061 class Circle : public Point { // Circle inherits from Point62 friend ostream &operator<<( ostream &, const Circle & );63 public:64 // default constructor 2000 Prentice Hall, Inc. All rights reserved.Outline1. Circle definition--------------------1. Load header1.1 Function Definitions65 Circle( double r = 0.0, int x = 0, int y = 0 );6667 void setRadius( double ); // set radius68 double


View Full Document

IUPUI CS 265 - Inheritance

Download Inheritance
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 Inheritance 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 Inheritance 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?