Learners Support Publications www.lsp4you.com Pointers, Virtual Functions and Polymorphism.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Constructors: Access Considerations DerivedClass::DerivedClass( int iR, float fVar) : BaseClass(fVar) { m_uiRating = uiR; } Alternatively DerivedClass::DerivedClass(
CSI 3125, Preliminaries, page 1 Polymorphism, Virtual function.
:PRESENTED BY: SHUBHAM KANDHARKAR SHUBHAM BHAGEL NIKET KOTHARI MANJEET SINGH ALHAWAT PRANAV TOSHNIWAL.
Polymorphism, Virtual Methods and Abstract Classes.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
VIRTUAL FUNCTIONS AND DYNAMIC POLYMORPHISM. *Polymorphism refers to the property by which objects belonging to different classes are able to respond to.
Operator Overloading and Type Conversions
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
CONCEPTS OF OBJECT ORIENTED PROGRAMMING. Topics To Be Discussed………………………. Objects Classes Data Abstraction and Encapsulation Inheritance Polymorphism.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Pointers review Let a variable aa be defined as ‘int *aa;’, what is stored in aa? Let a variable aa be defined as ‘int ** aa;’ what is stored in aa? Why.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Learners Support Publications Classes and Objects.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Learners Support Publications Functions in C++
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
CSCI-383 Object-Oriented Programming & Design Lecture 18.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
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.
Operator overloading: Overloading a unary operator is similar to overloading a binary operator except that there is one Operand to deal with. When you.
Chapter -6 Polymorphism
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Unit VI polymorphism. Md.Jaffar Sadiqsumalatha Polymorphism refers to : one name, many forms. Polymorphism is of two types:  Compile time polymorphism.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Polymorphism Lecture - 9.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Learners Support Publications Constructors and Destructors.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0]
Constructors and Destructors
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism, Virtual Methods and Abstract Classes
Andy Wang Object Oriented Programming in C++ COP 3330
Class A { public : Int x; A()
ATS Application Programming: Java Programming
Polymorphism Lec
Lecture 4-7 Classes and Objects
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Dr. Bhargavi Dept of CS CHRIST
Polymorphism Polymorphism
Constructors and Destructors
Classes and Objects.
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Pointers Dr. Bhargavi Goswami Department of Computer Science
Virtual Functions Polymorphism is supported by C++ both at compile time and at run time. Compile-time polymorphism is achieved by overloading functions.
VIRTUAL FUNCTIONS RITIKA SHARMA.
Submitted By : Veenu Saini Lecturer (IT)
Chapter 6 Polymorphism.
COP 3330 Object-oriented Programming in C++
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:

Learners Support Publications Pointers, Virtual Functions and Polymorphism

Learners Support Publications Early Binding or Compile Time Polymorphism  The concept of polymorphism is implemented using overloaded functions and operators.  The overloaded member functions are selected for invoking by matching arguments, both type and numbers.  This information is known to the compiler at the compile time and, therefore, compiler is able to select the appropriate function for a particular call at the compile time itself.  This is called early binding or static binding or static linking.

Learners Support Publications Late Binding or Run Time Polymorphism class A { int x; int x; public: public: void show( ) {……} void show( ) {……}}; class B : public A { int y; int y; public: public: void show( ) {……} void show( ) {……}};  Since the prototype of show( ) is the same in both the places, the function is not overloaded and therefore static binding does not apply.  The class resolution operator is used to specify the class while invoking the functions with the derived class.  Appropriate member function is selected while the program is running.

Learners Support Publications Late Binding or Run Time Polymorphism  The appropriate version of function will be invoked at runtime.  Since the function is linked with a particular class much later after the compilation, this process is termed as late binding.  This also known as dynamic binding, since the selection of appropriate function is done dynamically at runtime. Polymorphism Compile timeRun time Function Overloading Operator Overloading Virtual Functions Achieving Polymorphism

Learners Support Publications Pointer To Objects  item * it_ptr ; where item is a class and it_ptr is a pointer of type item.  Object pointers are useful in creats objects at run time.  An object pointer can be used to access the public members of an object.

Learners Support Publications Pointer To Objects class item { int code; int code; float price; float price; public: public: void getdata( int a, float b ) void getdata( int a, float b ) { code =a; price = b;} { code =a; price = b;} void show( void ) void show( void ) { cout << “Code :” << code<<endl; { cout << “Code :” << code<<endl; << “Price :” << price << endl; } << “Price :” << price << endl; }}; item x; item *ptr = &x;  We can refer to the member functions of item in two ways: Using dot operator and object. x.getdata(100,75.50); x.show( ); Using arrow operator and object pointer. ptr -> getdata(100, 75.50); ptr -> show( ); Since *ptr is an alias of x (*ptr).show( ); continue…

Learners Support Publications Pointer To Objects  We can also create the objects using pointers and new operator as: item * ptr = new item ; item * ptr = new item ; This statement allocates enough memory for the data members in the object structure and assigns the address of the memory space to ptr. This statement allocates enough memory for the data members in the object structure and assigns the address of the memory space to ptr.  We can also create an array of objects using pointers Item *ptr = new item[10]; Item *ptr = new item[10]; Creates memory space for an array of 10 objects of item. Creates memory space for an array of 10 objects of item. continue…

Learners Support Publications this Pointer  C++ uses a unique keyword called this to represent an object that invokes a member function.  This unique pointer is automatically passed to a member function when it is called.  The pointer this acts as an implicit argument to all the member functions.  One important application of the pointer this is to return the object it points to. return *this;

Learners Support Publications Pointer to Derived Classes  We can use pointers not only to the base objects but also to the objects of derived classes.  Pointers to objects of a base class are type- compatible with pointers to objects of a derived class.  Therefore, single pointer variable can be made to point to objects belonging to different classes.

Learners Support Publications Pointer to Derived Classes  If B is a base class and D is a derived class from B, then a pointer declared as a pointer to B can also be a pointer to D. B * bptr ; B b; D d; bptr = &b; We can also make bptr = &d; continue…

Learners Support Publications Pointer to Derived Classes  Using bptr, we can access only those members which are inherited from B and not the members that originally belong to D.  In case a member of D has the same name as one of the members of B, then any reference to that member by bptr will always access the base class member. continue…

Learners Support Publications Virtual Functions  Polymorphism refers to the property by which objects belonging to different classes are able to respond to the same message, but in different forms.  An essential requirement of polymorphism is therefore the ability to refer to objects without any regard to their classes.  This necessitates the use of a single pointer variable to refer to the objects of different classes.

Learners Support Publications Virtual Functions  We use pointer to base class to refer to all the derived objects.  When we use the same function name in both the base and derived classes, the function in base class is declared as virtual using the keyword virtual preceding its normal declaration.  When a function made virtual, C++ determines which function to use at run time based on the type of object pointed to by the base pointer, rather than the type of the pointer. continue…

Learners Support Publications Virtual Functions  One important point to remember is that, we must access virtual functions through the use of a pointer declared as a pointer to the base class.  Run time polymorphism is achieved only when a virtual function is accessed through a pointer to the base class. continue…

Learners Support Publications Rules for Virtual Functions  The virtual functions must be members of some class.  They cannot be static members.  They are accessed by using object pointers.  A virtual function can be a friend of another class.  A virtual function in a base class must be defined, even though it may not be used.

Learners Support Publications Pure Virtual Functions  A pure virtual function is a function declared in a base class that has no definition relative to the base class.  A do-nothing function may be defined as follows: virtual void display( ) = 0;  A class containing pure virtual functions cannot be used to declare any objects of its own. – abstract classes.

Learners Support Publications Pure Virtual Functions  The main objective of an abstract base class is to provide some traits to the derived classes and to create a base pointer required for achieving run time polymorphism.

Learners Support Publications Thank You Learners Support Publications