Wednesday, May 7, 2014

Simple Pointers to Object Example in C++





#include<iostream>// In old compiler use <iostream.h>
#include<conio.h>

using namespace std;

class A
{
     public:
     int a;
     A()
     {
           a=5;
     }
     void display()
     {
           cout<<a;
     }
};

int main()
{
     A a1;
     A *pt;
     pt=&a1;
     cout<<pt->a<<endl;//or you can use this also
     cout<<(*pt).a<<endl;
     a1.display();
     cout<<endl;//or you can use this also
    (*pt).display();
}

No comments:

Post a Comment