Home Uncategorized Write a Program to illustrate pointer to a class.

Write a Program to illustrate pointer to a class.

SHARE

AIM: Write a C++ program to illustrate pointer to class.

THEORY:

A pointer to a C++ class is done exactly the same way as a pointer to a structure and to access members of a pointer to a class you use the member access operator -> operator, just as you do with pointers to structures. Also as with all pointers, you must initialize the pointer before using it.

SOURCE CODE:

#include<iostream>
using namespace std;
int main()
{
   class man
   {
      public:
      char name[20];
      int age;
   };
   man m={“SUDHA”,22};
   man *ptr;
   ptr=&m;
   cout<<“Calling through object:\n”;
   cout<<m.name<<“\t”<<m.age<<endl;
   cout<<“Calling through pointer:\n”;
   cout<<ptr->name<<“\t”<<ptr->age<<endl;
}

OUTPUT:

Calling through object:
SUDHA 22
Calling through pointer:
SUDHA 22

Back to Programs.