Inheritance: Polymorphism and Virtual Functions

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Derived Classes. C++ 2 Outline  Definition  Virtual functions  Virtual base classes  Abstract classes. Pure virtual functions.
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.
1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related.
DERIVED CLASSES AND INHERITANCE Moshe Fresko Bar-Ilan University Object Oriented Programing
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.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
1 Virtual Functions and Polymorphism Chapter What You Will Learn What is polymorphism? How to declare and use virtual functions for abstract classes.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
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.
Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public:
C++ Review Classes and Object Oriented Programming Parasol Lab, Texas A&M University.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
Polymorphism and Virtual Functions. Topics Polymorphism Virtual Functions Pure Virtual Functions Abstract Base Classes Virtual Destructors V-Tables Run.
Chapter 10 Inheritance and Polymorphism
Computer Programming & Applications Mohamed Iqbal Pallipurath Lecture 02P. 1 Inheritance and Overloading Lecture 28.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
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
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
Inheritance - 3. Virtual Functions Functions defined as virtual are ones that the base expects its derived classes may redefine. Functions defined as.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
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.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
CMSC 202 Polymorphism.
Polymorphism, Virtual Methods and Abstract Classes
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
CS250 Introduction to Computer Science II
Inheritance, Polymorphism, and Virtual Functions
Polymorphism CT1513.
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.
CISC/CMPE320 - Prof. McLeod
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Overview of C++ Polymorphism
CIS 199 Final Review.
Inheritance: Polymorphism and Virtual Functions
CS410 – Software Engineering Lecture #8: Advanced C++ III
C++ Polymorphism Reference and pointer implicit type casting
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 27 Fri, Mar 26, 2004 2/22/2019 Inheritance

Topics Access to inherited data members Inherited member functions Polymorphism Virtual functions Abstract classes 2/22/2019 Inheritance

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

Access to Inherited Members Derived-class members have access to public and protected base-class members. Non-members have access to public base-class members only. Base member access public protected private Inheritance type no access 2/22/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. 2/22/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. 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. 2/22/2019 Inheritance

Example: Polymorphism int main() { Man m("Joe"); Woman w("Jane"); Describe(m); Describe(w); } void Describe(Person p) cout << p << endl; // What will happen? return; 2/22/2019 Inheritance

Demonstration Run the program FamilyTree.cpp to see what happens. 2/22/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. 2/22/2019 Inheritance

Example: Polymorphism int main() { Man m("Joe"); Woman w("Jane"); Describe(m); Describe(w); } void Describe(Person& p) cout << p << endl; // What will happen? return; 2/22/2019 Inheritance

Demonstration In the program FamilyTree.cpp, make the function parameter a reference parameter. Run the program to see what happens. 2/22/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. 2/22/2019 Inheritance

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

Example: Virtual Functions int main() { Man m("Joe"); Woman w("Jane"); Describe(m); Describe(w); } void Describe(Person& p) cout << p << endl; // What will happen? return; 2/22/2019 Inheritance

Demonstration In the file person.h, make Output() a virtual function. Run the program FamilyTree.cpp to see what happens 2/22/2019 Inheritance

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

Example: Virtual Functions int main() { Man m("Joe"); Woman w("Jane"); Describe(m); Describe(w); } void Describe(Person p) cout << p << endl; // What will happen? return; 2/22/2019 Inheritance

Demonstration In the program FamilyTree.cpp, make the function parameter a value parameter. Run the program to see what happens. 2/22/2019 Inheritance

Pure Virtual Functions A function may be designated as a pure virtual function. Write virtual function(parameters) = 0; The function is not instantiated at this level in the class hierarchy. The function must be instantiated in the derived classes. 2/22/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 level to implement it there. 2/22/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. 2/22/2019 Inheritance

Example: Pure Virtual Functions class Person { virtual void Output(ostream& out) const = 0; }; 2/22/2019 Inheritance

Example: Pure Virtual Functions int main() { Man m("Joe"); Woman w("Jane"); Describe(m); Describe(w); } void Describe(Person& p) cout << p << endl; // What will happen? return; 2/22/2019 Inheritance

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

Demonstration In FamilyTree.cpp, make the function parameter a value parameter. Run the program to see what happens. 2/22/2019 Inheritance

Example: Abstract Class Circles, squares, and triangles are shapes. Create a shape class as a base 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. 2/22/2019 Inheritance