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

Pointer To Class-2

This document discusses different types of pointers that can be used with classes in C++, including: 1. Pointers to objects of a class - A pointer to an object of a class can be declared and used to access members of that class. 2. Pointers to function members of a class - Pointers can be declared to member functions of a class and used to call those member functions on class objects. 3. Pointers to data members of a class - Pointers can also be declared to data members of a class to access the data of class objects. The document provides examples of declaring and using each type of pointer with class code examples. It also lists additional topics to be covered

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)
57 views

Pointer To Class-2

This document discusses different types of pointers that can be used with classes in C++, including: 1. Pointers to objects of a class - A pointer to an object of a class can be declared and used to access members of that class. 2. Pointers to function members of a class - Pointers can be declared to member functions of a class and used to call those member functions on class objects. 3. Pointers to data members of a class - Pointers can also be declared to data members of a class to access the data of class objects. The document provides examples of declaring and using each type of pointer with class code examples. It also lists additional topics to be covered

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/ 20

Pointers in Class

17/02/11

Declaration
List L1 ; List *ptr; ptr = &(List) L1; The last line may be written as ptr = &List(L1) ;

Example
#include<iostream.h> class List { private : int x,y ; public : void Setdata(int a, int b) { cout<<Enter number of items : cin>> a; cout<<Enter price of one item:; cin>>b;

Contd..
x = a; y = b; } void Display1() { cout<<Number of items =<<x<<endl ; } void Display2() { cout<<Price of each item = <<y<<endl; }

Contd..
void Display3() { cout<<Cost of<<x<<items at the rate of<<y<<per item =<<x*y<<endl; } };

Contd..
void main() { List L1; List *ptr; ptr = &(List) L1; int i , j ; (*ptr).Setdata(i, j); ptr -> Display1(); ptr -> Display2(); (*ptr) .Display3(); }

An increment to pointer to a class points to second objects of a class


#include<iostream.h> class List { private : int x, y, z ; public : void Display1() { cout<<Welcome to this market<<endl ; }

Contd..
void Setprice(int a, int b, int c) { x = a, y = b, z = c; } void Display2() { cout<<Price of item1 = <<x<<endl; cout<<Price of item2 = <<y<<endl; cout<<Price of item3 = <<z<<endl; } };

Contd..
void main() { List L1; List *ptr; ptr = &(List) L1; ptr -> Setprice(6,8,10); ptr -> Display1(); cout<<Price List 1<<endl; ptr -> Display2(); List L2; *ptr++;

Contd..
ptr -> Setprice(32,27,38) ; ptr -> Display1(); cout<< Price List 2<<endl; ptr->Display2(); }

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;

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

Contd..
void Display() { cout<<Price of item1 = <<x<<endl; cout<<Price of item2 = <<y<<endl; cout<<Price of item3 = <<z<<endl; } };

Contd..
void main() { int n = 0; List L1(12, 15, 27); cout<< List<<++n<<endl; List *ptr = &L1; ptr -> Display(); cout << \n List<<++n<<\n; List L2 (30, 54, 60); ptr = & L2 ; (*ptr).Display(); }

Pointer to Function Members of a class


Declaration and Assignment of pointer to member function 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 and assigned as: void (Rect :: *ptrSet)(int, int) = &Rect :: Setsides; (R1. *ptrSet)(20, 15) ;

Example
#include<iostream.h> Class Rect { int x, y; public: void Setsides (int L, int W) { x = L, y = W; }

Contd..
int Area() { return x*y ; } }; int main() { Rect R1, R2, R3;

Contd..
void (Rect :: *ptrSet)(int, int) = & Rect :: Setsides ; (R1.*ptrSet)(20,15) ; cout<<Area of R1=<<R1.Area()<<endl ; ( R2. *ptrSet)(20, 30); cout<<Area of R2=<<R2.Area()<<endl ; Rect *ptr3 = &R3; (ptr3 -> *ptrSet)(16,10); cout<<Area of R3=<<R3.Area()<<endl ; return 0 ; }

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;

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 class objects

You might also like