Wednesday, March 28, 2018

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


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={"BALA",18};
            man *ptr;
            ptr=&m;
            cout<<"\n"<<m.name<<" "<<m.age;
            cout<<"\n"<<ptr->name<<" "<<ptr->age;
}

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home