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

Pointer For Accessing Private Data

1. The this pointer refers to the address of the current object and is passed implicitly as a hidden argument to non-static member functions. This allows member functions to access and modify the current object's members. 2. Private data members of a class can be accessed using pointers by taking the address of the object and casting it to the appropriate pointer type. 3. Pointers to data members allow accessing and modifying class members through a pointer instead of directly through the class object. They are declared using the class name, scope resolution operator, and member name.

Uploaded by

Abhishek Modi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views

Pointer For Accessing Private Data

1. The this pointer refers to the address of the current object and is passed implicitly as a hidden argument to non-static member functions. This allows member functions to access and modify the current object's members. 2. Private data members of a class can be accessed using pointers by taking the address of the object and casting it to the appropriate pointer type. 3. Pointers to data members allow accessing and modifying class members through a pointer instead of directly through the class object. They are declared using the class name, scope resolution operator, and member name.

Uploaded by

Abhishek Modi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 30

Pointers for accessing private data of an object

23/02/11

Pointer to Data member of a Class


The syntax for declaration and assignment of pointer to data members is : type class_name :: *pointer_name = & class_name :: data_name; Eg : int Rect :: *ptrx = &Rect :: x; int Rect :: *ptry = &Rect :: y;

Example
#include<iostream.h> Class Rect { friend int Area (Rect a); int x, y ; public : friend double cost (Rect a, double); Rect (int L, int W) { x = L, Y = W ; } };

Contd..
int Area (Rect b) { int Rect :: *ptrx = & Rect :: x; int Rect :: * ptry = & Rect :: y; Rect *ptrb = &b; return b.*ptrx * b. *ptry ; };

Contd..
int Area (Rect b) { int Rect :: *ptrx = & Rect :: x ; int Rect :: *ptry = & Rect :: y ; Rect *ptrb = &b; return b.*ptrx * b. *ptry ; }; double cost (Rect b , double m) { return b.x * b.y * m; }

Contd..
int main() { double n = 4.5; Rect R1(5,6), R2(3,4) ; cout << Area of R1 = << Area(Rect(R1))<<\n; cout << Area of R2 = << Area(Rect (R2))<<\n; cout << cost for R1 = << cost(Rect (R1), n)<<\n; return 0 ; }

Accessing Private Data of an Object through pointers


#include<iostream.h> class List { private : int x, y, z; public : int Alpha ; List (int a, int b, int c) { x = a, y = b, z = c ; } };

Contd..
void main () { List L1(10, 20, 30); int *pAlpha ; L1. Alpha = 50; pAlpha = &L1.Alpha; cout << Alpha = << *pAlpha <<endl; cout << Address of Alpha = <<pAlpha <<endl; pAlpha --; cout<<The z data of L1 = << *pAlpha<<endl; cout <<Address of z = <<pAlpha <<endl;

Contd..
x(private) y(private) z(private) Alpha(public)
10 20 30 50

0012FF70 0012FF74 0012FF78 0012FF7C

Contd..
pAlpha --; cout <<The y data of L1 = <<*pAlpha<<endl; cout <<Address of y = <<pAlpha<<endl; pAlpha --; cout <<The x data of L1 = <<*pAlpha<<endl; cout <<Address of x = <<pAlpha<<endl; }

Contd..
The expected output is : Alpha = 50 Address of Alpha = 0012FF7C The z data of L1 = 30 Address of z = 0012FF78 The y data of L1 = 20 Address of y = 0012FF74 The x data of L1 = 10 Address of x = 0012FF70

Accessing private data members from address of object


#include<iostream.h> class List { private : int x, y, z ; public : int s ; List (int a, int b, int c) { x = a, y = b, z = c ; } };

Contd..
void main() { List L1(10,20,30) ; L1.s = 40 ; int *ptrL1 ; ptrL1 = (int*) &L1 ; cout << Value of x = << *ptrL1<<endl; cout << Address of x = <<ptrL1<<endl ; ptrL1 ++ ; cout << The y data of L1 = << *ptrL1 <<endl; cout << Address of y = << ptrL1 << endl ;

Contd..
ptrL1++ ; cout << The z data of L1 = << *ptrL1 <<endl ; cout << Address of z = <<ptrL1 <<endl ; ptrL1 ++ ; cout << The s data of L1 =<<*ptrL1<<endl ; cout <<Address of s =<<ptrL1 <<endl; }

The this pointer

The word this is a keyword of C++ It is the name of pointer to an object of a class

this pointer
class Student{ int rollno ; char *name ; float GPA ; public : int getRollNo(); void setRollNo(int aRollNo); }

this pointer
The compiler reserves space for the functions defined in the class Space for data is not allocated(since no object is yet created)
Function space getRollNo(),

Contd..
Student S1, S2, S3;

S1(rollno,)
S2(rollno,) Function space getRollNo(), S3(rollno,)

Contd..
Function space is common for every variable When a new object is created : - Memory is reserved for variable only. - previously defined function are used over and over again.

Contd..
Memory layout for object created

S1 address

S2 address

S3 address

S4 address

Function space getRollNo(),

Contd..
How does a function know on which object to act?

Passing this pointer


Whenever a function is called the this pointer is passed as additional parameter to function Function with n parameter is actually called n+1 parameters

Declaration of this
Data type *const this ;

Contd..
Address of each object is passed to the calling function.
S1(rollno,..) S2(rollno,..) S3(rollno,..) address address address S4(rollno,..) address

This address is dereferenced by the function and hence they act on correct objects. The variable containing self address is called pointer

Compiler generated code


Student :: Student() { rollno = 0; } Student :: Student() { this->rollno = 0; }

Example
#include<iostream.h> class Cuboid { public : Cuboid(int L, int W, int H) { this -> x = L, (*this).y = W, this -> z = H; } int surface_area() ; int volume() ;

Contd..
void Display1() { cout<< x = this -> x<< , y = <<(*this).y <<, z = <<this -> z <<endl ; } void Display2() { cout << this <<endl ; } private : int x, y, z; };

Contd..
int Cuboid :: surface _area() { return 2*(x*y + y*z + z*x) ; } int Cuboid :: volume() { return x*y*z ; } int main () { Cuboid C1(5,6,4) , C2(7,8,5) ; C1.Display1(); C1.Display2(); cout << &C2<<endl ; cout <<Volume of cuboid C1 =<<C1.volume()<<endl ; cout <<Volume of cuboid C2 =<<C2.volume()<<endl ; cout <<Volume of cuboid C1 =<<C1.volume()<<endl ; cout <<Surface area of cuboid C1 =<<C1.surface_area()<<endl ; cout <<Surface area of cuboid C2 =<<C2.surface_area()<<endl ; }

Contd..
The expected output is: x = 5, y = 6, z = 4 0012FF74 0012FF74 x = 7, y = 8, z = 5 0012FF68 0012FF68 Volume of cuboid C1 = 120 Volume of cuboid C2 = 280 Surface area of C1 = 148 Surface area of C2 = 262

Next class
Operator Overloading

You might also like