Upload
abhishek-modi
View
214
Download
0
Embed Size (px)
Citation preview
7/31/2019 Pointer to Class-2
1/20
Pointers in Class
17/02/11
7/31/2019 Pointer to Class-2
2/20
Declaration
List L1 ;
List *ptr;
ptr = &(List) L1;The last line may be written as
ptr = &List(L1) ;
7/31/2019 Pointer to Class-2
3/20
Example
#includeclass List
{
private :
int x,y ;public :
void Setdata(int a, int b)
{
cout a;
coutb;
7/31/2019 Pointer to Class-2
4/20
Contd..
x = a;y = b;
}
void Display1()
{
cout
7/31/2019 Pointer to Class-2
5/20
Contd..
void Display3()
{
cout
7/31/2019 Pointer to Class-2
6/20
Contd..
void main()
{
List L1;
List *ptr;
ptr = &(List) L1;int i , j ;
(*ptr).Setdata(i, j);
ptr -> Display1();
ptr -> Display2();(*ptr) .Display3();
}
7/31/2019 Pointer to Class-2
7/20
An increment to pointer to a class
points to second objects of a class#includeclass List
{
private :
int x, y, z ;
public :
void Display1(){
cout
7/31/2019 Pointer to Class-2
8/20
Contd..
void Setprice(int a, int b, int c)
{
x = a, y = b, z = c;
}
void Display2(){
cout
7/31/2019 Pointer to Class-2
9/20
Contd..
void main()
{
List L1;
List *ptr;
ptr = &(List) L1;
ptr -> Setprice(6,8,10);
ptr -> Display1();
cout
7/31/2019 Pointer to Class-2
10/20
Contd..
ptr -> Setprice(32,27,38) ;
ptr -> Display1();
cout
7/31/2019 Pointer to Class-2
11/20
Pointer to objects of a class
Let L1 be an object of class List, the pointer to
L1 be declared and assigned as:
List *ptr = &L1;
7/31/2019 Pointer to Class-2
12/20
#includeclass List
{
private :int x, y, z ;
public :
List (int a, int b, int c) {
x = a, y = b, z = c;
}
7/31/2019 Pointer to Class-2
13/20
Contd..
void Display()
{
cout
7/31/2019 Pointer to Class-2
14/20
Contd..
void main(){
int n = 0;
List L1(12, 15, 27);
cout
7/31/2019 Pointer to Class-2
15/20
Pointer to Function Members of a
class
Declaration and Assignment of pointer to memberfunction of class:
type(class_name ::*pointer_name)(types_parameters)
= & class_name ::Function_name;Eg :
void Setsides( int L, int W) { x = L, y = W ; }
The pointer *ptrSet to the function is declared andassigned as:
void (Rect :: *ptrSet)(int, int) = &Rect :: Setsides;
(R1. *ptrSet)(20, 15) ;
7/31/2019 Pointer to Class-2
16/20
Example
#include
Class Rect
{
int x, y;
public:
void Setsides (int L, int W)
{ x = L, y = W; }
7/31/2019 Pointer to Class-2
17/20
Contd..
int Area()
{
return x*y ;}
};
int main(){
Rect R1, R2, R3;
7/31/2019 Pointer to Class-2
18/20
Contd..
void (Rect :: *ptrSet)(int, int) = & Rect :: Setsides ;
(R1.*ptrSet)(20,15) ;
cout
7/31/2019 Pointer to Class-2
19/20
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;
7/31/2019 Pointer to Class-2
20/20
Next Day..
Accessing private data of an object through
pointers
The this pointer
Static data members of a class
Static function member of a class
Dynamic memory management for classobjects