Inheritance: Polymorphism and Virtual Functions

Slides:



Advertisements
Similar presentations
Derived Classes. C++ 2 Outline  Definition  Virtual functions  Virtual base classes  Abstract classes. Pure virtual functions.
Advertisements

CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related.
Abstract Classes An abstract class is a base class that will never have an object instantiated from it. –Abstract classes are used only for inheritance,
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 28P. 1Winter Quarter Inheritance and Overloading.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
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.
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
C++ Review Classes and Object Oriented Programming Parasol Lab, Texas A&M University.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
Chapter 10 Inheritance and Polymorphism
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Object-Oriented Programming in C++ More examples of Association.
Computer Programming & Applications Mohamed Iqbal Pallipurath Lecture 02P. 1 Inheritance and Overloading Lecture 28.
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Overview of C++ Polymorphism
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
1 C++ Classes & Object Oriented Programming Overview & Terminology.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
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.
1 More About Derived Classes and Inheritance Chapter 9.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
CMSC 202 Polymorphism.
Class and Objects UNIT II.
Abstract Data Types Programmer-created data types that specify
Ch 10- Advanced Object-Oriented Programming Features
7. Inheritance and Polymorphism
CSC241: Object Oriented Programming
Class A { public : Int x; A()
Object-Oriented Programming
Polymorphism.
Inheritance & Polymorphism
Review: Two Programming Paradigms
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
CS250 Introduction to Computer Science II
Templates.
Inheritance Often, software encapsulates multiple concepts for which some attributes/behaviors overlap E.g. A computer (role-playing) game has: Monsters:
Classes in C++ C++ originally called "C with classes":
HYBRID INHERITANCE : AMBIGUITY REMOVAL
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Polymorphism 1 CMSC 202.
Inheritance: Polymorphism and Virtual Functions
Polymorphism CMSC 202, Version 4/02.
Virtual Functions Polymorphism is supported by C++ both at compile time and at run time. Compile-time polymorphism is achieved by overloading functions.
Inheritance: Polymorphism and Virtual Functions
Function “Inputs and Outputs”
CISC/CMPE320 - Prof. McLeod
Fundaments of Game Design
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Overview of C++ Polymorphism
Inheriting Multiple Base Classes
CPS120: Introduction to Computer Science
CS410 – Software Engineering Lecture #8: Advanced C++ III
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Lecture 6: Polymorphism
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Computer Science II for Majors
Presentation transcript:

Inheritance: Polymorphism and Virtual Functions Lecture 23 Wed, Mar 19, 2008 5/11/2019 Inheritance

Access to Inherited Members Three Access Modes Public Protected Private Base-class members and derived-class members have their usual member-access modes. The inheritance relation itself has a separate access mode. 5/11/2019 Inheritance

Access to Inherited Members We will normally use public inheritance. Public and protected members of the base class will be accessible from within the scope of the derived classes. Only public members of the base class will be accessible from outside the scope of the derived classes. Private members of the base class are accessible only from within the base class. 5/11/2019 Inheritance

Public Inheritance Accessibility Suppose we have the following. class A { public: funcA(); protected: int aMem; } class B : public A { public: funcB(); protected: int bMem; } int main() { A a; B b; } 5/11/2019 Inheritance

Public Inheritance Accessibility From Scope of A Scope of B Global Scope funcA() a.funcA() b.funcA() aMem a.aMem b.aMem funcB() b.funcB() bMem b.bMem 5/11/2019 Inheritance

Access to Inherited Members Occasionally we will use private inheritance. Private inheritance limits public access by way of class B objects to the public members of class B. However, from within the scope of class B, there is still access to the class A public and protected members. 5/11/2019 Inheritance

Private Inheritance Accessibility Suppose we have the following. class A { public: funcA(); protected: int aMem; } class B : private A { public: funcB(); protected: int bMem; } int main() { A a; B b; } 5/11/2019 Inheritance

Public Inheritance Accessibility From Scope of A Scope of B Global Scope funcA() a.funcA() b.funcA() aMem a.aMem b.aMem funcB() b.funcB() bMem b.bMem 5/11/2019 Inheritance

Polymorphism A function that specifies a base-class object in its parameter list may accept a derived-class object in its place. This phenomenon is called polymorphism. Polymorphism works because the derived-class object IS-A base-class object. We have already seen this used in the constructors. 5/11/2019 Inheritance

Polymorphism and Passing by Value If a function passes the base-class object by value, then the derived-class object is considered to be an object of the base class. Why does this happen? 5/11/2019 Inheritance

Polymorphism and Passing by Value This happens because the base-class copy constructor was used to create the local object. The local object loses its derived-class data members and functions. 5/11/2019 Inheritance

Example: Polymorphism int main() { Man man("John"); Woman woman("Jane"); Describe(man); Describe(woman); } void Describe(Person p) cout << p << endl; // Is p a Man, a Woman? return; 5/11/2019 Inheritance

Demonstration Run the program DescribePeople.cpp to see what happens. 5/11/2019 Inheritance

Polymorphism and Passing by Reference If the function passes the base-class object by reference, then the derived-class object is considered to be an object of the derived class. 5/11/2019 Inheritance

Example: Polymorphism int main() { Man man("John"); Woman woman("Jane"); describe(man); describe(woman); } void describe(Person& p) cout << p << endl; // Is p a Man, a Woman? return; 5/11/2019 Inheritance

Demonstration In the program DescribePeople.cpp, make the describe() function parameter a reference parameter. Run the program to see what happens. 5/11/2019 Inheritance

Virtual Functions When base class and a derived class have distinct functions of the same name, how does the compiler know which one to invoke? If the base-class function is virtual, then The computer will invoke the member function of that name that is closest to the class of the invoking object. Write the keyword virtual at the beginning of the function prototype. 5/11/2019 Inheritance

Example: Virtual Functions class Person { virtual void output(ostream& out) const {out << name << ‘ ‘ << sex;} }; 5/11/2019 Inheritance

Example: Virtual Functions int main() { Man man("John"); Woman woman("Jane"); describe(man); describe(woman); } void describe(Person& p) cout << p << endl; // What will happen? return; 5/11/2019 Inheritance

Demonstration In the file person.h, make output() a virtual function. Run the program DescribePeople.cpp to see what happens 5/11/2019 Inheritance

Virtual Functions and Value Parameters What happens when the function is virtual and the parameter is a value parameter? 5/11/2019 Inheritance

Example: Virtual Functions int main() { Man man("John"); Woman woman("Jane"); describe(man); describe(woman); } void describe(Person p) cout << p << endl; // What will happen? return; 5/11/2019 Inheritance

Demonstration In the program DescribePeople.cpp, make the function parameter a value parameter. Run the program to see what happens. 5/11/2019 Inheritance

Pure Virtual Functions A function may be designated as a pure virtual function. Write The function is not instantiated at this level in the class hierarchy. The function must be instantiated in the derived classes. virtual function(parameters) = 0; 5/11/2019 Inheritance

Pure Virtual Functions This is done when the function must be implemented at some level in the hierarchy, but there is not enough information at the top (base) level to implement it there. Example? 5/11/2019 Inheritance

Abstract Classes An abstract class is a class that contains a pure virtual function. No object of an abstract class may be instantiated. Function parameters of an abstract class type must be passed by reference. Why? 5/11/2019 Inheritance

Example: Pure Virtual Functions class Person { virtual void output(ostream& out) const = 0; }; 5/11/2019 Inheritance

Example: Pure Virtual Functions int main() { Man man("John"); Woman woman("Jane"); describe(man); describe(woman); } void describe(Person& p) cout << p << endl; // What will happen? return; 5/11/2019 Inheritance

Demonstration In the file person.h, make the Output() function pure virtual. In DescribePeople.cpp, make the function parameter a reference parameter. Run the program to see what happens. 5/11/2019 Inheritance

Demonstration In DescribePeople.cpp, make the function parameter a value parameter. Run the program to see what happens. 5/11/2019 Inheritance

Example: Abstract Class Circles, squares, and triangles are shapes. Create a shape class as a base class. 5/11/2019 Inheritance

Example: Abstract Class Shape Circle Rectangle Triangle 5/11/2019 Inheritance

Example: Abstract Class Each shape has an area and a perimeter. However, we cannot find the area or perimeter until we know the particular shape. Therefore, Shape should be an abstract class. 5/11/2019 Inheritance

Example: Abstract Class Shape virtual area() = 0; Circle area() {r2} Rectangle area() {lw} Triangle area() {½bh} 5/11/2019 Inheritance