Pointer For Accessing Private Data
Pointer For Accessing Private Data
23/02/11
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 ; }
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
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
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 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
Contd..
How does a function know on which object to act?
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
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