KEMBAR78
Pointers and Operators | PDF
0% found this document useful (0 votes)
148 views1 page

Pointers and Operators

This document discusses pointers to classes in C++. It provides an example of a base class A with data members and methods, including an initialize method and display method. An object of class A is created, and a pointer variable is used to point to the object. The -> operator is used to access members of the object being pointed to, and the . operator is used to access members when dereferencing the pointer with *. It also provides a summary of common pointer operators like *, &, ., and -> and notes that pointers to structures work the same way as pointers to classes.

Uploaded by

nita23arora2321
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
148 views1 page

Pointers and Operators

This document discusses pointers to classes in C++. It provides an example of a base class A with data members and methods, including an initialize method and display method. An object of class A is created, and a pointer variable is used to point to the object. The -> operator is used to access members of the object being pointed to, and the . operator is used to access members when dereferencing the pointer with *. It also provides a summary of common pointer operators like *, &, ., and -> and notes that pointers to structures work the same way as pointers to classes.

Uploaded by

nita23arora2321
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 1

Computer Science (083) Pointers to Classes

Pointers to classes (Example)


#include <iostream.h>
#include <conio.h>

class A // ********** BASE CLASS ************


{ int x2,x3;
public :
int x1;
void initialize(int a,int b,int c)
{x1=a;x2=b;x3=c;
cout <<"Data members initialized ....\n";
getch();
}
void display()
{cout<<x1<<'\t'<<x2<<'\t'<<x3<<endl;}

};
void main()
{
clrscr();
A obj;
obj.initialize(10,20,30);
A * ptr=&obj;
ptr->display();
cout<<"\nptr->x1 = "<<ptr->x1;
cout<<"\n(*ptr).x1 = "<<(*ptr).x1 ; // NOTE x1 is declared under public section
//otherwise will not be accessible
getch();
(*ptr).display();
getch();
}
Summary of pointer and operators (*, &, ., ->)

expression can be read as


*x pointed by x
&x address of x
x.y member y of object x
x->y member y of object pointed by x
(*x).y member y of object pointed by x (equivalent to the previous one)

NOTE : What has been discussed for classes is also valid for pointers to
structures.

By Nita Arora

You might also like