0% found this document useful (0 votes)
13 views34 pages

Chapter-9 Pointers - KJD - OLD

Uploaded by

alex.moon268
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views34 pages

Chapter-9 Pointers - KJD - OLD

Uploaded by

alex.moon268
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

U & P U.

Patel Department of Computer Engineering

UNIT - 9
“POINTER”

1
INTRODUCTION

• C++ allows us to create a class with its members such as data members and member functions. One
way to access the data members and member function of a class is through the object of the class
with a dot operator.
• C++ also provides us a few operators through we could access the data members and member
functions of a class by using pointers. These operators are known as dereferencing operators.

De-Referencing Operator Description


This operator allows us to create a pointer to a class member, which could be data
::*
member or member function.
This operator uses the pointer to the member of a class and a pointer to the object
->*
of the same class, to access the member of a class.
This operator uses the pointer to the member of a class and an object of the same
.*
class, to access member of a class.

2
POINTER TO OBJECT

• We can create a pointer of type class.


• We can access member of class by pointer type object.
Syntax:
Class_Name *pointer_var;
Example
class Student Normally: With Pointer:
{
void display(); Obj.display(); Ob1 -> display();
}; Or
Student obj; (*Ob1).display();
Student *Ob1;
Ob1 = &obj; //parenthesis due to precedence

3
EXAMPLE
#include<iostream> cout << "\nStudent Roll No : " << roll;
#include<string.h> cout << "\nStudent Name : " << name;
using namespace std; }
class Student };
{ int main()
int roll; {
char name[10]; Student Obj;
public: Student *sp;
void get(int x, char *y) sp = &Obj;
{ sp -> get(1,“Ram");
roll = x; //(*sp).get(2 ,“sita");
strcpy(name,y); sp -> put();
} return 0;
void put() }
{ 4
POINTER TO DATA MEMBER OF A CLASS -I

Dereferencing operators ::* and .* to access the data members:


• The syntax of ::* dereferencing operator is -
data-type class-name ::* pointer-name = &class-name :: data-member-name;
The data-type is the data type of the data member.
The class-name is the class name of the class of which the data member is a part of.
The pointer-name is the name of the pointer, pointing to the member function.
The data-member-name is the name of the data member being referenced.

• The syntax of .* dereferencing operator is -


object-name.*pointer-to-data-member;

5
EXAMPLE:DEREFERENCING OPERATORS ::* AND .* TO
ACCESS THE DATA MEMBERS

#include<iostream> //Pointer to member variable y of class A


using namespace std; int A::* p2 = &A :: y;
class A //Creating an object of class A
{ A ob;
public: ob.*p1 = 10;
int x,y; ob.*p2 = 10;
}; cout<<"The value of x is : " << ob.*p1 << "\n";
int main() cout<<"The value ot y is : " << ob.*p2 << "\n";
{ }
//Pointer to member variable x of class A
int A::* p1 = &A :: x; 6
POINTER TO DATA MEMBER OF A CLASS -II

Dereferencing operators ::* and ->* to access the data members:


• The syntax of ::* dereferencing operator is –
data-type class-name ::* pointer-name = &class-name :: data-member-name;
The data-type is the data type of the data member.
The class-name is the class name of the class of which the data member is a part of.
The pointer-name is the name of the pointer, pointing to the member function.
The data-member-name is the name of the data member being referenced.

• The syntax of ->* dereferencing operator is -


pointer-to-object->*pointer-to-data-member;

7
EXAMPLE: DEREFERENCING OPERATORS ::*
AND ->* TO ACCESS THE DATA MEMBERS
#include<iostream> int A :: *p1 = &A :: x;
using namespace std; int A :: *p2 = &A :: y;
class A //Using pointer to object to access data member,
{ //pointed by a pointer
ptr->*p1 = 30;
public:
//Using pointer to object to access data member,
int x,y;
//pointed by a pointer
};
ptr->*p2 = 30;
int main()
cout<<"The value of x is : " << ptr->*p1 << "\n";
{
cout<<"The value ot y is : " << ptr->*p2 << "\n";
A ob;
}
//Pointer to object
A *ptr = &ob;
8
POINTER TO MEMBER FUNCTION OF A CLASS-I

Dereferencing operators ::* and .* to access the data members:


• The syntax of ::* dereferencing operator is -
return-type class-name ::* pointer-name(argument) = &class-name :: member-function-name;
The return-type is the return type of the member function.
The class-name is the class name of the class of which the member function is a part of.
The pointer-name is the name of the pointer, pointing to the member function.
The member-function-name is the name of the member function being referenced.

• The syntax of .* dereferencing operator is -


(object-name.*pointer-to-member-function)(argument-list);
9
POINTER TO MEMBER FUNCTION OF A CLASS-I

Dereferencing operators ::* and .* to access the data members:


• The syntax of ::* dereferencing operator is -
return-type class-name ::* pointer-name(argument) = &class-name :: member-function-name;
The return-type is the return type of the member function.
The class-name is the class name of the class of which the member function is a part of.
The pointer-name is the name of the pointer, pointing to the member function.
The member-function-name is the name of the member function being referenced.

• The syntax of .* dereferencing operator is -


(object-name.*pointer-to-member-function)(argument-list);
10
EXAMPLE : DEREFERENCING OPERATORS ::*
AND .* TO ACCESS THE MEMBER FUNCTION
#include<iostream> int main()
using namespace std; {
class A //A Pointer to member function
{ void (A :: *pt1_f) (int,int) = &A :: put_value;
public: //A pointer to member function
int x,y; void (A :: *pt2_f) () = &A :: show_value;
void put_value(int a, int b) //Creating an object of class A
{ A ob;
x=a; //Using a pointer-to-member-function to call the member
y=b; //function put_value()
} (ob.*pt1_f)(20,20);
void show_value() //Using a pointer-to-member-function to call the member
{ //function show_value()
cout<<"The value of x is : " << x << "\n"; (ob.*pt2_f)();
cout<<"The value ot y is : " << y << "\n"; }
}
};
11
POINTER TO MEMBER FUNCTION OF A CLASS -II

Dereferencing operators ::* and ->* to access the data members:


• The syntax of ::* dereferencing operator is –
return-type class-name ::* pointer-name(argument) = &class-name :: member-function-name;
The return-type is the return type of the member function.
The class-name is the class name of the class of which the member function is a part of.
The pointer-name is the name of the pointer, pointing to the member function.
The member-function-name is the name of the member function being referenced.

• The syntax of ->* dereferencing operator is -


(pointer-to-object->*pointer-to-member-function)(argument-list);
12
EXAMPLE : DEREFERENCING OPERATORS ::*
AND ->* TO ACCESS THE MEMBER FUNCTION
#include<iostream> {
using namespace std; A ob;
class A //Pointer to object
{ A *ptr = &ob;
public: int A :: *p1 = &A :: x;
int x,y; int A :: *p2 = &A :: y;
void put_value(int a, int b) //A Pointer to member function
{ void (A :: *pt1_f) (int,int) = &A :: put_value;
x=a; //A pointer to member function
y=b; void (A :: *pt2_f) () = &A :: show_value;
} //Using pointer to object to access member function,
void show_value() //pointed by a pointer
{ (ptr->*pt1_f)(40,40);
cout<<"The value of x is : " << x << "\n"; //Using pointer to object to access member function,
cout<<"The value ot y is : " << y << "\n"; //pointed by a pointer
} (ptr->*pt2_f)();
}; }
int main()
13
“this” POINTERS

• To understand ‘this’ pointer, it is important to know how objects look at functions and data members of
a class.
1. Each object gets its own copy of the data member.
2. All-access the same function definition as present in the code segment.
• Then now question is that if only one copy of each member function exists and is used by multiple
objects, how are the proper data members are accessed and updated?
➢ The compiler supplies an implicit pointer along with the names of the functions as ‘this’.
➢ The ‘this’ pointer is passed as a hidden argument to all non-static member function calls and is
available as a local variable within the body of all non-static functions. ‘this’ pointer is not available
in static member functions as static member functions can be called without any object (with class
name).
➢ It is issued implicitly but, we can use it explicitly.
• It is a special keyword which represents the current object by holding the address of the current object.
14
“this” POINTERS
“THIS” POINTER

• For Example:
class x
{
int a;
public:
void fun()
{
a = 100; //or this -> a = 100; or (*this).a = 100;
}
};
x obj;
obj.fun();
cout << “\nobject address” << this;

15
Situations where “this” Pointer is used

1. Local variable/member have same name:


class x
{
int a;
public:
void fun(int a)
{
a = a; //not right
this->a = a; //Right:
}
};

16
Situations where “this” Pointer is used

2. To return current object: else


#include<iostream> return *this;
#include<string.h> }
using namespace std; void display()
class Max {
{ cout << "\nGreater Value: " << no;
int no; }
public: };
Max(int no=0) int main()
{ {
this->no = no; Max Obj1(10), Obj2(20), Obj3;
} Obj3 = Obj1.Greater(Obj2);
Max Greater(Max &X) Obj3.display();
{ return 0;
if(X.no > no) }
return X;
17
POINTER TO DERIVED CLASS

Q: Why?
A: To access the members of the derived class using the base class pointer.

Q: Which members?
A: Only publically inherited members can be directly accessed and not the added/own members.

18
EXAMPLE
#include <iostream> };
using namespace std; int main ()
class Base {
{ Base B1;
public: Base *ptr;
int x; ptr = &B1;
void display () ptr->x = 10;
{ ptr->display();
cout<<"X="<<x<<endl; Derive D1;
} ptr = &D1;
}; ptr->x = 20;
class Derive: public Base ptr->y = 30;
{ ptr->display();
public: }
int y;
void display()
{
cout<<"X="<<x<<endl;
cout<<"Y="<<y<<endl;
}
19
EXAMPLE
#include <iostream> cout<<"\nY="<<y;
using namespace std; }
class Base };
{ int main ()
public: {
int x; Base B1;
void display () Base *ptr;
{ ptr = &B1;
cout << "\nBase Class"; ptr->x = 10;
cout<<"\nX="<<x; ptr->display();
} Derive D1;
}; ptr = &D1;
class Derive: public Base ((Derive*)ptr)->x = 20;
{
public: ((Derive*)ptr)->y = 30;
int y; ((Derive*)ptr)->display();
void display() }
{
cout << "\nDerived Class";
cout<<"\nX="<<x;
20
EXAMPLE
#include <iostream> }
using namespace std; };
class Base int main ()
{ {
public: Base B1;
int x; Base *ptr;
void display () ptr = &B1;
{ ptr->x = 10;
cout << "\nBase Class"; ptr->display();
cout<<"\nX="<<x; Derive D1;
} Derive *p1;
};
class Derive: public Base p1 = &D1;
{ p1->x = 20;
public: p1->y = 30;
int y;
void display() p1->display();
{ }
cout << "\nDerived Class";
cout<<"\nX="<<x;
cout<<"\nY="<<y; 21
BASIS FOR COMPARISON STATIC BINDING DYNAMIC BINDING

Events occur at compile time are


Events occur at run time are
Event Occurrence "Static Binding".
"Dynamic Binding".

All information need to call a


All information needed to call a
Information function come to know at run
function is known at compile time.
time.
Advantage Efficiency. Flexibility.
Time Fast execution. Slow execution.
Alternate name Early Binding. Late Binding.

Overloaded function call,


Example Virtual function in C++
overloaded operators.

22
VIRTUAL FUNCTION
A virtual function is a member function which is declared within a base class and is re-defined (Overridden) by
a 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.
• 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.
❖ Rules for Virtual Functions:
• 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.
23
EXAMPLE
#include<iostream> }
using namespace std; void show()
class base {
{ cout << "show derived class" << endl;
public: }
virtual void print() };
{ int main()
cout << "print base class" << endl; {
} base* bptr;
void show() derived d;
{ bptr = &d;
cout << "show base class" << endl; // virtual function, binded at runtime
} bptr->print();
}; // Non-virtual function, binded at compile time
class derived : public base bptr->show();
{ }
public:
void print()
{
cout << "print derived class" << endl;
24
EXPLANATION
Explanation: Runtime polymorphism is achieved only through a pointer (or reference) of base class type.
Also, a base class pointer can point to the objects of base class as well as to the objects of derived class. In
above code, base class pointer ‘bptr’ contains the address of object ‘d’ of derived class.

Late binding(Runtime) is done in accordance with the content of pointer (i.e. location pointed to by pointer)
and Early binding(Compile time) is done according to the type of pointer, since print() function is declared
with virtual keyword so it will be bound at run-time (output is print derived class as pointer is pointing to
object of derived class ) and show() is non-virtual so it will be bound during compile time(output is show
base class as pointer is of base type ).

NOTE: If we have created a virtual function in the base class and it is being overridden in the derived class
then we don’t need virtual keyword in the derived class, functions are automatically considered as virtual
functions in the derived class.

25
EXAMPLE
#include <iostream> p = &obj1;
using namespace std;
// Early binding because fun1() is non-virtual
class base { // in base
public: p->fun_1();
void fun_1() { cout << "base-1\n"; }
virtual void fun_2() { cout << "base-2\n"; } // Late binding (RTP)
virtual void fun_3() { cout << "base-3\n"; } p->fun_2();
virtual void fun_4() { cout << "base-4\n"; }
}; // Late binding (RTP)
p->fun_3();
class derived : public base {
public: // Late binding (RTP)
void fun_1() { cout << "derived-1\n"; } p->fun_4();
void fun_2() { cout << "derived-2\n"; }
void fun_4(int x) { cout << "derived-4\n"; } // Early binding but this function call is
}; // illegal(produces error) because pointer
// is of base type and function is of
int main() // derived class
{ // p->fun_4(5);
base* p; }
derived obj1; 26
EXPLANATION
Explanation: Initially, we create a pointer of type base class and initialize it with the address of the
derived class object. When we create an object of the derived class, the compiler creates a pointer as a
data member of the class containing the address of the derived class.

Similar concept of Late and Early Binding is used as in above example. For fun_1() function call, base
class version of function is called, fun_2() is overridden in derived class so derived class version is
called, fun_3() is not overridden in derived class and is virtual function so base class version is called,
similarly fun_4() is not overridden so base class version is called.

27
PURE VIRTUAL FUNCTION

• Sometimes implementation of all function cannot be provided in a base class because we don’t know the
implementation. Such a class is called abstract class. For example, let Shape be a base class. We cannot
provide implementation of function draw() in Shape, but we know every derived class must have
implementation of draw(). Similarly an Animal class doesn’t have implementation of move() (assuming that
all animals move), but all animals must know how to move. We cannot create objects of abstract classes.
• A pure virtual function (or abstract function) in C++ is a virtual function for which we don’t have
implementation, we only declare it. A pure virtual function is declared by assigning 0 in declaration. See the
following example.
// An abstract class
class Test
{
// Data members of class
public:
// Pure Virtual Function
virtual void show() = 0;
/* Other members */
}; 28
EXAMPLE
#include<iostream> int main(void)
using namespace std; {
Derived d;
class Base d.fun();
{ return 0;
int x; }
public:
virtual void fun() = 0;
int getX()
{
return x;
}
};
// This class inherits from Base and implements fun()
class Derived: public Base
{
int y;
public:
void fun() { cout << "fun() called"; }
};
29
SOME INTERESTING FACTS!!
#include<iostream> int main(void)
1. A class is abstract if it has at least using namespace std; {
one pure virtual function. class Test Test t;
{ return 0;
• In the following example, Test is int x; }
an abstract class because it has a public:
pure virtual function show(). virtual void show() = 0;
int getX() { return x; }
};

30
SOME INTERESTING FACTS!!

2. We can have pointers and #include<iostream> cout << "In Derived \n";
references of abstract class type. using namespace std; }
class Base };
• For example the following { int main(void)
program works fine. public: {
virtual void show() = 0; Base *bp = new Derived();
}; bp->show();
class Derived: public Base return 0;
{ }
public:
void show()
{

31
SOME INTERESTING FACTS!!
#include<iostream> {
using namespace std;
3. If we do not override the pure class Base };
virtual function in derived class, { int main(void)
then derived class also becomes public: {
abstract class. virtual void show() = 0; Derived d;
• The following example }; return 0;
demonstrates the same. }
class Derived : public Base

32
SOME INTERESTING FACTS!!
#include<iostream> Derived(int i, int j):Base(i)
using namespace std; { y = j; }
4. An abstract class can have class Base void fun() {
constructors. { cout << "x = " << x << ", y = "
protected: << y;
• For example, the following int x; }
program compiles and runs fine. public: };
virtual void fun() = 0;
Base(int i) { x = i; } int main(void)
}; {
Derived d(4, 5);
class Derived: public Base d.fun();
{ return 0;
int y; }
public:

33
ANY QUESTION?

34

You might also like