Pointers to Objects


#include <iostream.h>
/*----------------------Class Interfaces-------------------------*/
class A
{
        int a1;                        // private data member
    public :
        int a2;                        // public data member
        void set_a1(int);        // public member functions
        void display_a1a2();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for class A
void A::set_a1(int x)
{
    a1 = x;
}
void A::display_a1a2()
{
    cout << "a1 = " << a1 << "\n";
    cout << "a2 = " << a2 << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
    A obj, *p;        // pointer to object declared
    p = &obj;              // pointer to object assigned
    p->set_a1(10);
    p->a2 = 20;
    (*p).display_a1a2();// same as 'p->display_a1a2()'
}

0 comments:

Post a Comment

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|