Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 1
Department of Information Technology
POINTER AND
OBJECT IN C++
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 2
Department of Information Technology
Pointer to object
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 3
Department of Information Technology
How to store address of object into pointer
and access members?
 Ans
 Pointer to object
 Two Ways to
access member
 (*ptr).member
 Ptr->member
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 4
Department of Information Technology
Pointer to object
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 5
Department of Information Technology
How to store address of object created using new into pointer
and access members?
 new Base(10)
allocate memory
at memory heap
that is to be
pointed by p1
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 6
Department of Information Technology
How to create pointer to array of object?
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 7
Department of Information Technology
How to call constructor of array of object
through new?
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 8
Department of Information Technology
What is this pointer?
 C++ use unique key word called this to represent
object that invokes member function.
 i.e. Compiler store address of invoking object in this!!!!
 Example: obj1.max(obj2)
 Address of obj1 will get stored into this pointer.
 Use of this pointer
 To access data member of invoking object within class
 To return invoking object!!!
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 9
Department of Information Technology
To access data member of invoking
object within class
30
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 10
Department of Information Technology
To return invoking object!!
30
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 11
Department of Information Technology
Pointer to derived class
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 12
Department of Information Technology
Pointer to derived class
Note: Base class pointer could point to DerivedNote: Base class pointer could point to Derived
Class object but derived class pointer can notClass object but derived class pointer can not
Point to base class pointer!!!Point to base class pointer!!!
Note: Base class pointer could point to DerivedNote: Base class pointer could point to Derived
Class object but derived class pointer can notClass object but derived class pointer can not
Point to base class pointer!!!Point to base class pointer!!!
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 13
Department of Information Technology
Pointer to derived class
Compiler generates error Because derived classCompiler generates error Because derived class
public members can not be Accessed throughpublic members can not be Accessed through
base class Pointer!!!base class Pointer!!!
Reason: Base class pointer can only points toReason: Base class pointer can only points to
Base class object within derived class.Base class object within derived class.
Compiler generates error Because derived classCompiler generates error Because derived class
public members can not be Accessed throughpublic members can not be Accessed through
base class Pointer!!!base class Pointer!!!
Reason: Base class pointer can only points toReason: Base class pointer can only points to
Base class object within derived class.Base class object within derived class.
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 14
Department of Information Technology
Pointer to derived class
Compiler generates error Because derived classCompiler generates error Because derived class
public members can not be Accessed throughpublic members can not be Accessed through
base class Pointer!!!base class Pointer!!!
Reason: Base class pointer can only points toReason: Base class pointer can only points to
Base class object within derived class.Base class object within derived class.
Compiler generates error Because derived classCompiler generates error Because derived class
public members can not be Accessed throughpublic members can not be Accessed through
base class Pointer!!!base class Pointer!!!
Reason: Base class pointer can only points toReason: Base class pointer can only points to
Base class object within derived class.Base class object within derived class.
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 15
Department of Information Technology
Method Overriding
 Method Overgrind: If same signature of a functionMethod Overgrind: If same signature of a function
in base class as well derived class.in base class as well derived class.
 Binding of Fun. Depend on type of Invoking objectBinding of Fun. Depend on type of Invoking object
 Base class-> base class member functionBase class-> base class member function
 Derived class-> Derived class member funDerived class-> Derived class member fun
 Compile time polymorphismCompile time polymorphism
 Method Overgrind: If same signature of a functionMethod Overgrind: If same signature of a function
in base class as well derived class.in base class as well derived class.
 Binding of Fun. Depend on type of Invoking object.Binding of Fun. Depend on type of Invoking object.
 Base class-> base class member functionBase class-> base class member function
 Derived class-> Derived class member funDerived class-> Derived class member fun
 Compile time polymorphismCompile time polymorphism
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 16
Department of Information Technology
Method Overriding
 Binding of Fun. Depend on type of Invoking object.Binding of Fun. Depend on type of Invoking object.
 p->print() call base type print function as type ofp->print() call base type print function as type of
p is Base.p is Base.
 Binding of Fun. Depend on type of Invoking object.Binding of Fun. Depend on type of Invoking object.
 p->print() call base type print function as type ofp->print() call base type print function as type of
p is Base.p is Base.
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 17
Department of Information Technology
How to call derived class method using
base class pointer??
 Solution
 Run time polymorphism
 What it mean?
 Binding will done at run time Instead of compile time.
 What happen in Runtime binding
 Based on content of the object compiler will bind the
function definition with specific functional call!!!
 How to instruct to the compiler to do binding at
runtime instead of compile time
 By making base class function virtual. Generally referred as
virtual Function!!!!
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 18
Department of Information Technology
Method Overriding
 When compiler find virtual keyword in baseWhen compiler find virtual keyword in base
class member function, Compiler perform runtimeclass member function, Compiler perform runtime
binding instead of compile time binding.binding instead of compile time binding.
Content of p is address of derived class so it callsContent of p is address of derived class so it calls
print() method of derived class!!!!print() method of derived class!!!!
 When compiler find virtual keyword in baseWhen compiler find virtual keyword in base
class member function, Compiler perform runtimeclass member function, Compiler perform runtime
binding instead of compile time binding.binding instead of compile time binding.
Content of p is address of derived class so it callsontent of p is address of derived class so it calls
rint() method of derived class!!!!rint() method of derived class!!!!
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 19
Department of Information Technology
Definition: Virtual Function
 A virtual function is a member function which is
declared within base class and is re-
defined (Overridden) by derived class. When you refer
to a derived class object using a pointer or a reference
to the base class, you can call a virtual function for
that object and execute the derived class's version of
the function.
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 20
Department of Information Technology

Pointer and Object in C++

  • 1.
    Classified e-Material ©CopyrightsCharotar Institute of Technology-CHARUSAT, Changa 1 Department of Information Technology POINTER AND OBJECT IN C++
  • 2.
    Classified e-Material ©CopyrightsCharotar Institute of Technology-CHARUSAT, Changa 2 Department of Information Technology Pointer to object
  • 3.
    Classified e-Material ©CopyrightsCharotar Institute of Technology-CHARUSAT, Changa 3 Department of Information Technology How to store address of object into pointer and access members?  Ans  Pointer to object  Two Ways to access member  (*ptr).member  Ptr->member
  • 4.
    Classified e-Material ©CopyrightsCharotar Institute of Technology-CHARUSAT, Changa 4 Department of Information Technology Pointer to object
  • 5.
    Classified e-Material ©CopyrightsCharotar Institute of Technology-CHARUSAT, Changa 5 Department of Information Technology How to store address of object created using new into pointer and access members?  new Base(10) allocate memory at memory heap that is to be pointed by p1
  • 6.
    Classified e-Material ©CopyrightsCharotar Institute of Technology-CHARUSAT, Changa 6 Department of Information Technology How to create pointer to array of object?
  • 7.
    Classified e-Material ©CopyrightsCharotar Institute of Technology-CHARUSAT, Changa 7 Department of Information Technology How to call constructor of array of object through new?
  • 8.
    Classified e-Material ©CopyrightsCharotar Institute of Technology-CHARUSAT, Changa 8 Department of Information Technology What is this pointer?  C++ use unique key word called this to represent object that invokes member function.  i.e. Compiler store address of invoking object in this!!!!  Example: obj1.max(obj2)  Address of obj1 will get stored into this pointer.  Use of this pointer  To access data member of invoking object within class  To return invoking object!!!
  • 9.
    Classified e-Material ©CopyrightsCharotar Institute of Technology-CHARUSAT, Changa 9 Department of Information Technology To access data member of invoking object within class 30
  • 10.
    Classified e-Material ©CopyrightsCharotar Institute of Technology-CHARUSAT, Changa 10 Department of Information Technology To return invoking object!! 30
  • 11.
    Classified e-Material ©CopyrightsCharotar Institute of Technology-CHARUSAT, Changa 11 Department of Information Technology Pointer to derived class
  • 12.
    Classified e-Material ©CopyrightsCharotar Institute of Technology-CHARUSAT, Changa 12 Department of Information Technology Pointer to derived class Note: Base class pointer could point to DerivedNote: Base class pointer could point to Derived Class object but derived class pointer can notClass object but derived class pointer can not Point to base class pointer!!!Point to base class pointer!!! Note: Base class pointer could point to DerivedNote: Base class pointer could point to Derived Class object but derived class pointer can notClass object but derived class pointer can not Point to base class pointer!!!Point to base class pointer!!!
  • 13.
    Classified e-Material ©CopyrightsCharotar Institute of Technology-CHARUSAT, Changa 13 Department of Information Technology Pointer to derived class Compiler generates error Because derived classCompiler generates error Because derived class public members can not be Accessed throughpublic members can not be Accessed through base class Pointer!!!base class Pointer!!! Reason: Base class pointer can only points toReason: Base class pointer can only points to Base class object within derived class.Base class object within derived class. Compiler generates error Because derived classCompiler generates error Because derived class public members can not be Accessed throughpublic members can not be Accessed through base class Pointer!!!base class Pointer!!! Reason: Base class pointer can only points toReason: Base class pointer can only points to Base class object within derived class.Base class object within derived class.
  • 14.
    Classified e-Material ©CopyrightsCharotar Institute of Technology-CHARUSAT, Changa 14 Department of Information Technology Pointer to derived class Compiler generates error Because derived classCompiler generates error Because derived class public members can not be Accessed throughpublic members can not be Accessed through base class Pointer!!!base class Pointer!!! Reason: Base class pointer can only points toReason: Base class pointer can only points to Base class object within derived class.Base class object within derived class. Compiler generates error Because derived classCompiler generates error Because derived class public members can not be Accessed throughpublic members can not be Accessed through base class Pointer!!!base class Pointer!!! Reason: Base class pointer can only points toReason: Base class pointer can only points to Base class object within derived class.Base class object within derived class.
  • 15.
    Classified e-Material ©CopyrightsCharotar Institute of Technology-CHARUSAT, Changa 15 Department of Information Technology Method Overriding  Method Overgrind: If same signature of a functionMethod Overgrind: If same signature of a function in base class as well derived class.in base class as well derived class.  Binding of Fun. Depend on type of Invoking objectBinding of Fun. Depend on type of Invoking object  Base class-> base class member functionBase class-> base class member function  Derived class-> Derived class member funDerived class-> Derived class member fun  Compile time polymorphismCompile time polymorphism  Method Overgrind: If same signature of a functionMethod Overgrind: If same signature of a function in base class as well derived class.in base class as well derived class.  Binding of Fun. Depend on type of Invoking object.Binding of Fun. Depend on type of Invoking object.  Base class-> base class member functionBase class-> base class member function  Derived class-> Derived class member funDerived class-> Derived class member fun  Compile time polymorphismCompile time polymorphism
  • 16.
    Classified e-Material ©CopyrightsCharotar Institute of Technology-CHARUSAT, Changa 16 Department of Information Technology Method Overriding  Binding of Fun. Depend on type of Invoking object.Binding of Fun. Depend on type of Invoking object.  p->print() call base type print function as type ofp->print() call base type print function as type of p is Base.p is Base.  Binding of Fun. Depend on type of Invoking object.Binding of Fun. Depend on type of Invoking object.  p->print() call base type print function as type ofp->print() call base type print function as type of p is Base.p is Base.
  • 17.
    Classified e-Material ©CopyrightsCharotar Institute of Technology-CHARUSAT, Changa 17 Department of Information Technology How to call derived class method using base class pointer??  Solution  Run time polymorphism  What it mean?  Binding will done at run time Instead of compile time.  What happen in Runtime binding  Based on content of the object compiler will bind the function definition with specific functional call!!!  How to instruct to the compiler to do binding at runtime instead of compile time  By making base class function virtual. Generally referred as virtual Function!!!!
  • 18.
    Classified e-Material ©CopyrightsCharotar Institute of Technology-CHARUSAT, Changa 18 Department of Information Technology Method Overriding  When compiler find virtual keyword in baseWhen compiler find virtual keyword in base class member function, Compiler perform runtimeclass member function, Compiler perform runtime binding instead of compile time binding.binding instead of compile time binding. Content of p is address of derived class so it callsContent of p is address of derived class so it calls print() method of derived class!!!!print() method of derived class!!!!  When compiler find virtual keyword in baseWhen compiler find virtual keyword in base class member function, Compiler perform runtimeclass member function, Compiler perform runtime binding instead of compile time binding.binding instead of compile time binding. Content of p is address of derived class so it callsontent of p is address of derived class so it calls rint() method of derived class!!!!rint() method of derived class!!!!
  • 19.
    Classified e-Material ©CopyrightsCharotar Institute of Technology-CHARUSAT, Changa 19 Department of Information Technology Definition: Virtual Function  A virtual function is a member function which is declared within base class and is re- defined (Overridden) by derived class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class's version of the function.
  • 20.
    Classified e-Material ©CopyrightsCharotar Institute of Technology-CHARUSAT, Changa 20 Department of Information Technology