Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-1 Learning Objectives  Inheritance  Virtual Function.

Slides:



Advertisements
Similar presentations
Chapter 15 Polymorphism and Virtual Functions. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Virtual Function.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 14 Inheritance. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Inheritance Basics Derived classes, with.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
CMSC 202 Inheritance. Aug 6, Object Relationships An object can have another object as one of instance variables. The Person class had two Date.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
C++ Inheritance Systems Programming.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
CS102--Object Oriented Programming Lecture 7: – Inheritance Copyright © 2008 Xiaoyan Li.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 15 Inheritance.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 17 – Payroll Application: Introducing Inheritance.
Inheritance, Polymorphism, and Virtual Functions
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
Chapter 12: Adding Functionality to Your Classes.
Chapter 15 Inheritance. Slide Overview 15.1 Inheritance Basics 15.2 Inheritance Details 15.3 Polymorphism.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Polymorphism and Virtual Functions
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
Chapter 15 Polymorphism and Virtual Functions. Learning Objectives Virtual Function Basics – Late binding – Implementing virtual functions – When to use.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Slide 1 Polymorphism and Virtual Functions. Slide 2 Learning Objectives  Virtual Function Basics  Late binding  Implementing virtual functions  When.
Polymorphism and Virtual Functions. Motivation Polymorphism is one of the fundamental mechanisms offered by OOP, and it is directly related to inheritance.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
Polymorphism, Abstraction and Virtual Functions. In this slide, we introduce virtual functions and two complex and powerful uses for derived classes that.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
CMSC 202 Inheritance I Class Reuse with Inheritance.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
CMSC 202 Inheritance II. Version 10/092 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
Chapter -6 Polymorphism
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley What Is Inheritance? 15.1.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Virtual Function and Polymorphism
Polymorphism and Virtual Functions
Chapter 15 Inheritance. Chapter 15 Inheritance.
COMP 2710 Software Construction Inheritance
Inheritance, Polymorphism, and Virtual Functions
Polymorphism & Virtual Functions
Inheritance Often, software encapsulates multiple concepts for which some attributes/behaviors overlap E.g. A computer (role-playing) game has: Monsters:
Chapter 14 Inheritance Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Inheritance I Class Reuse with Inheritance
Learning Objectives Inheritance Virtual Function.
Inheritance, Polymorphism, and Virtual Functions
CMSC 202 Inheritance.
Class Reuse with Inheritance
Polymorphism Polymorphism
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Inheritance Chapter 15 & additional topics.
Fundaments of Game Design
C++ Object Oriented 1.
Chapter 7 Inheritance.
Computer Science II for Majors
Presentation transcript:

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Introduction to Inheritance  General form of class is defined  Specialized class is then defined  Inherit properties (data member and function member) of general class  Add new data members  Add new functions or modify inherited functions

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Inheritance Basics  Base class (parent class)  "General" class from which others derive  Derived class (child class)  Automatically has base class’s:  Member variables  Member functions  Can then add additional member functions and variables and redefine existing functions

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Employee Example: Base Class  General concept of employee helpful!  All have names  All have social security numbers  Associated functions for these "basics" are similar among all employees  So "general" class can contain all these "things" about employees

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Employee Example (base class) Class Employee { private: string name; string SSN; public: Employee (string, string); void print_check(); }

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Employee Example (base class)  Look at function print_check(), different checks should be printed for different types of employees  So, let’s write it like this Employee:: void print_check() { cout << "This function is left for derived class"; }

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Employee Example (derived class)  A specific employee could be either a:  Monthly employee (salary is calculated monthly)  Hourly employee (salary is calculated hourly)  Each is a type of employee

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Public interitance  class DerivedClass : public BaseClass { … //public keyword (most commonly used) //specifies "publicly inherited“ from //Employee class //other keywords (protected, private) are //rarely used …… }

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Employee Example: Derived Class  Define Monthly_Employee and Hourly_Employee as the derived class of general (base) class Employee  From base class  Inherit all member variables  Inherit all member functions  Can then  Define new member functions  Redefine inherited functions

Copyright © 2006 Pearson Addison-Wesley. All rights reserved MonthlyEmployee Class class MonthlyEmployee : public Employee { private: double wage; //new data member public: MonthlyEmployee(string, string, double) // constructor void reset_wage(double); // new function member void print_check(); // redefine function member }

Copyright © 2006 Pearson Addison-Wesley. All rights reserved HourlyEmployee Class class HourlyEmployee : public Employee { private: double wageRate; //new data member double hours; //new data member public: HourlyEmployee(string, string, double, double) // constructor void reset_wage(double); // new function member void print_check(); // redefine function member }

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Constructors in Derived Classes  Base class constructors are NOT inherited in derived classes!  But they can be invoked within derived class constructor  Base class constructor must initialize all base class member variables  Derived needs to initialize all new data members

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Derived Class Constructor Example  MonthlyEmployee constructor: MonthlyEmployee::MonthlyEmployee(string Name, string Number, double wage): Employee(Name, Number) { this->wage = wage; }

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Derived Class Constructor Example  HourlyEmployee constructor: HourlyEmployee::HourlyEmployee(string Name, string Number, double wageRate, double hours): Employee(Name, Number) { this->wageRate = wageRate; this->hours = hours; }

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Function Redefinition In Derived Class MonthlyEmployee:: void print_check() { cout << wage; } HoulyEmployee:: void print_check() { cout << wageRate*hours; }

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Redefining vs. Overloading  Very different!  Redefining in derived class:  SAME parameter list  Essentially "re-writes" same function  Overloading:  Defined "new" function that takes different parameters  Overloaded functions must have different signatures

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Accessing Redefined Base Function  When function is redefined in derived class, base class’s definition is not "lost"  Can specify it’s use: Employee JaneE; HourlyEmployee SallyH; JaneE.printCheck(); //calls Employee’s printCheck function SallyH.printCheck(); //calls HourlyEmployee printCheck function SallyH.Employee::printCheck(); //Calls Employee’s printCheck function!  Not typical here, but useful sometimes

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Destructors in Derived Classes  Base class destructor handles data defined in base class  Derived class destructors handles derived class variables  When derived class destructor is invoked:  Automatically calls base class destructor!  So no need for explicit call

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Destructor Calling Order  Consider: class B derives from class A class C derives from class B  When object of class C goes out of scope:  Class C destructor called 1 st  Then class B destructor called  Finally class A destructor is called  Opposite of how constructors are called  Class A constructor is called  Class B constructor is called  Class C constructor is called

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Base Class Member Qualifier class DerivedClass : public BaseClass { private: public: protected: }

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Base Class Private Data  Derived class "inherits" private member variables  But still cannot directly access them  Not even through derived class member functions!  Private member variables can ONLY be accessed "by name" in member functions of the class they’re defined in

Copyright © 2006 Pearson Addison-Wesley. All rights reserved The protected: Qualifier  New classification of class members  Allows access "by name" in derived class  In other classes, these members act like private

Class member types Member (dada/function) type publicprotectedprivate Accessible by functions in Same class Yes Derived classes Yes No Other classes YesNo Copyright © 2006 Pearson Addison-Wesley. All rights reserved

Copyright © 2006 Pearson Addison-Wesley. All rights reserved "Is a" vs. "Has a" Relationships  Inheritance  Considered an "Is a" class relationship  e.g., a car is a vehicle  e.g., a computer is a machine  e.g., a dog is an animal  A class contains objects of another class as it’s member data  Considered a "Has a" class relationship  e.g., a car has an engine  e.g., a computer has a CPU  e.g., a dog has four legs

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Multiple Inheritance  Derived class can have more than one base class!  Syntax just includes all base classes separated by commas: class derived: public base1, base2 {…}  Dangerous undertaking!  Some believe this should never be used

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Virtual Function Basics  Polymorphism  Associating many meanings to one function  Virtual functions provide this capability  Polymorphism, virtual function, and dynamic (late) binding talk about the same thing

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Figures Example  Classes for several kinds of figures  Rectangle, Circle, Oval, Square, etc.  Each is an object of different class  Rectangle data: height, width, center point  Circle data: center point, radius  All derive from one parent-class: Figure  Each class needs different draw function Rectangle r; Circle c; r.draw(); //Calls Rectangle class’s draw c.draw(); //Calls Circle class’s draw

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Figures Example (continued)  How about this one? void function(Figure f) { f.draw(); }  We would like different draw functions being called for different type of f  Standard non-virtual function can not do this  Virtual functions are the answer

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Virtual: How?  Virtual function virtual return_type function_name(parameters)  Involves late binding (dynamic binding)  Tells compiler to "wait" until function is used in program  Decide which definition to use based on calling object at runtime

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Overriding  Virtual function definition changed in a derived class  We say it’s been "overridden"  Similar to “redefined” standard (non-virtual) functions  So, in derived class:  Virtual functions changed: overridden  Non-virtual functions changed: redefined  Seem same for the programmer, but treated differently by the compiler: early binding or late binding

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Pure Virtual Functions  Base class might not have "meaningful“ definition; It’s purpose solely for others to derive from  Recall class Figure  All real figures are objects of derived classes  Rectangles, circles, triangles, etc.  Class Figure has no idea how to draw!  Make it a pure virtual function: virtual void draw() = 0;  Pure virtual function  No definitions in based class  Must be overridden in derived classes

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Abstract Class  A class with one or more pure virtual functions is called an abstract class  An abstract class can only be used as a base class to derive other classes  We can not create objects of an abstract class, because it is not a complete class definition.