Pointers Dr. Bhargavi Goswami Department of Computer Science

Slides:



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

V IRTUAL F UNCTIONS Chapter 10 Department of CSE, BUET 1.
Inheritance, Polymorphism, and Virtual Functions
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.
Chapter 12: Adding Functionality to Your Classes.
Pointer Data Type and Pointer Variables
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
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.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
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.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
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.
Chapter -6 Polymorphism
CS212: Object Oriented Analysis and Design Lecture 16: Runtime Polymorphism.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Polymorphism Lecture - 9.
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.
Constructors and Destructors
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Andy Wang Object Oriented Programming in C++ COP 3330
Polymorphism &Virtual Functions
Class A { public : Int x; A()
Inheritance, Polymorphism, and Virtual Functions
CS212: Object Oriented Analysis and Design
Polymorphism & Virtual Functions
Comp 249 Programming Methodology
Object Oriented Programming
Polymorphism Lec
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Inheritance Basics Programming with Inheritance
Support for Object-Oriented Programming in Ada 95
Virtual Functions Department of CSE, BUET Chapter 10.
Advanced Java Topics Chapter 9
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Inheritance, Polymorphism, and Virtual Functions
Dr. Bhargavi Dept of CS CHRIST
Polymorphism Polymorphism
Constructors and destructors
Constructors and Destructors
Operator overloading Dr. Bhargavi Goswami
Computer Programming with JAVA
Java – Inheritance.
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Polymorphism CT1513.
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Virtual Functions Polymorphism is supported by C++ both at compile time and at run time. Compile-time polymorphism is achieved by overloading functions.
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
Advanced Inheritance Concepts
VIRTUAL FUNCTIONS RITIKA SHARMA.
Chapter 6 Polymorphism.
COP 3330 Object-oriented Programming in C++
C++ Polymorphism Reference and pointer implicit type casting
C++ Object Oriented 1.
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:

Pointers Dr. Bhargavi Goswami Department of Computer Science Christ University Bangalore. bhargavigoswami@gmail.com 9426669020

Pointers, virtual, polymorphism Polymorphism means, one name, multiple forms. Means? Overloading? Yes/No. Yes. Two Types: 1) Early Binding/Static Binding/Static Linking. 2) Late Binding/Dynamic Binding/ Early Binding Information is known to compiler before execution starts. Called Compile time polymorphism. Means: Object is bound to its function call at compile time.

Pointers, virtual, polymorphism Problem: We cannot select appropriate function during the execution of the program. Solution? 2) Late Binding / dynamic binding / run time polymorphism. Here, selection of appropriate function is done dynamically at run time. How do we do that? Ans: we need pointers to objects and virtual functions for that. How? That’s what we will study in this chapter.

Examples refVar.cpp refVar1.cpp refVarPointerEx.cp p ReturnByReference .cpp firstPointer.cpp morePointers.cpp CallbyValAddrRef.c pp PointerArray.cpp PointersAsArgumen tsConstants.cpp voidPointer.cpp PointertoMember.c pp PointerToFunction. cpp DereferencingOper ator.cpp

With Pointers we did Introduction to Pointers Declaring and Initializing Pointers Manipulation of Pointers Arithmetic Operations on Pointers Pointers with arrays and arrays of pointers Pointers to functions Pointers to objects Array of pointers to objects If you have forgotten, please revise by referring to previously listed programs. Lets see some advanced concepts of pointers now.

This pointer This is a keyword to represent an object that invokes a member function. This is a pointer to object for which this function was called. Eg. A.max() will set pointer this to address of obj A. Important: This pointer acts as implicit argument to all the member function calls. Object is passed implicitly to called function in unary operator overloading. Similarly we pass 1st operand implicitly to binary operator overloading function call and 2nd operand as an argument. Eg. Suppose A is member variable. Both are same: A = 123; //easy so frequently used. this->A = 123; //complex so not frequently used. Another application is to return the object. Eg. return *this; Don’t get confused, lets see example to clarify doubts. See Program of This Pointer: ThisPointer.cpp

Pointer to Derived Objects We can use pointers not only to the base objects but also to the objects of derived classes Pointers to base class has type compatibility to derived class. Means? Same pointer can point to any of the class (derived or base). Rule: We can access only those members which are inherited from Base Class and not the member of Derived Class. If the function names are same, pointer call will access base class and not derived class member. Then? We need to define separate pointer to derived class. See Program of Pointer to Derived Objects. PointersToDerivedObjetcs.cpp

Virtual functions Virtual Function is a function in base class, which is overridden in the derived class, and which tells the compiler to perform Late Binding on this function. Virtual Keyword is used to make a member function of the base class Virtual. When we use Base class's pointer to hold Derived class's object, base class pointer or reference will always call the base version of the function On using Virtual keyword with Base class's function, Late Binding takes place and the derived version of function will be called, because base class pointer pointes to Derived class object. Lets see the example. All your confusion will be resolved. PureVirtualFunction.cpp VirtualFunctions.cpp RuntimePolymorphism.cpp

Rules for Virtual functions Virtual functions ensure that the correct function is called for an object, regardless of the type of reference (or pointer) used for function call. They are mainly used to achieve Runtime polymorphism Functions are declared with a virtual keyword in base class. The resolving of function call is done at Run-time. They Must be declared in public section of class. Virtual functions cannot be static and also cannot be a friend function of another class. Virtual functions should be accessed using pointer or reference of base class type to achieve run time polymorphism. The prototype of virtual functions should be same in base as well as derived class. They are always defined in base class and overridden in derived class. It is not mandatory for derived class to override (or re- define the virtual function), in that case base class version of function is used. A class may have virtual destructor but it cannot have a virtual constructor.

Pure Virtual function Pure Virtual functions can be given a small definition in the Abstract class, which you want all the derived classes to have. Still you cannot create object of Abstract class. Also, the Pure Virtual function must be defined outside the class definition. If you will define it inside the class definition, complier will give an error. Inline pure virtual definition is Illegal. We have done this during abstract class.

Chapter Over. Thank you. See u soon.