0% found this document useful (0 votes)
2 views

Lecture 6-2

Uploaded by

jan21march22
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 6-2

Uploaded by

jan21march22
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Prepared By

Prashant Soni
Assistant Professor,UCER Naini,Allahabad

Object Oriented System Design


Unit 5 Lecture 6
POINTER TO OBJECTS
POINTER TO OBJECTS
• A Variable That Holds an Address value is called a
Pointer variable or simply pointer.
• We already know about pointer that's point to Simple
data types likes int, char, float etc.
• So similar to these type of data type, Objects can also
have an address, so there is also a pointer that can
point to the address of an Object, This Pointer is
Known as This Pointer.
• Every Object in C++ has access to its own address
through an important pointer This Pointer.
• The This Pointer is an implicit parameter to all
member functions
• Therefore, inside a member function, this may be
used to refer to the invoking object.
• Whenever a member function is called, it is
automatically passed an implicit arguments that is
This pointer to the invoking object (i.e. The object on
which the function is invoked).
• The This pointer is passed as a hidden argument to all
Nonstatic member function calls and is available as a
local variable within the body of all Nonstatic
functions.
• This Pointer is a constant pointer that holds the
memory address of the current object.
• This pointer is not available in static member
functions as static member functions can be called
without any object (with class name).
#include<iostream>
using namespace std;
class Point
{
private:
int x;
public:
void setx(int a) //simple function.
{
this->x=a;
}
};
main()
{
Point obj1; //1st Object of Class Point.
Point obj2; //2nd Object of Class point.
obj1.setx(10); //function calling using 1st object.
obj2.setx(30); //function calling using 2nd object.
C++ Declaration and Uses of
Object Pointers
• Just like Other Pointers, the object pointers are declared by placing in
front of a object pointer's name.
• The Syntax is:
• class_name * Object_pointer_name;
• In above Syntax, class_Name is the name of an already defined class
and object_pointer_name is the pointer to an object of this class
type.
• For example: To declared an Pointer ptr as an object pointer of class
Date, We shall write:
• Date * ptr;
#include<iostream>
using namespace std;
class Date
{
private:
short int dd, mm, yy;
public:
Date() //constrctor:
{
dd = mm = yy = 0;
}
void getdata(int i, int j, int k)
{
dd = i;
mm = j;
yy = k;
}
void prndata(void)
{
cout<<"\nData is "<<dd<<"/"<<mm<<"/"<<yy<<"\n";
}
};
main()
{
Date D1; //simple object having type Data:
Date *dptr; //Pointer Object having type Date:
cout<<"Initializing data members using the object, with values 19, 10, 2016"<<endl;
D1.getdata(19,10,2016);
cout<<"Printing members using the object ";
D1.prndata();
dptr = &D1;
cout<<"Printing members using the object pointer ";
dptr->prndata();
cout<<"\nInitializing data members using the object pointer, with values 20, 10, 2016"<<endl;
dptr->getdata(20, 10, 2016);
cout<<"printing members using the object ";
D1.prndata();
cout<<"Printing members using the object pointer ";
dptr->prndata();
return 0;
}
Note
To Access public members using an object the
Operator is used, and to access public members
using an Object pointer, the Arrow -> operator is
used.

You might also like