Here is the program which uses pointers to members, variables and functions.
Pointer to variables are done without error as i checked keeping the blocks with problem in comment line.
The problem is with member function. I did it as it was given in book and even saw same format on net.
The errors are like this:cannot call member function 'void' sejal::get without object.
The compiler is Bloodshed's Dev c++
Code:#include<stdio.h> #include<conio.h> #include<iostream> using namespace std; class sejal { public: int y; void get(); void put(); friend sejal add(sejal,sejal); }; void sejal::get() { cout<<"\nEnter the value"; cin>>y; }; void sejal::put() { cout<<"\nThe value is :"<<y; }; sejal add(sejal m, sejal k) { sejal o; o.y=m.y+k.y; cout<<"The addition value is:"; return(o); }; main() { sejal a,b,c; a.get(); b.get(); c=add(a,b); c.put(); /*using pointer to member variable*/ int sejal::*p=&sejal::y; cout<<"\nPrinting using pointer to member"<<":"<<a.*p<<"\n"<<b.*p; cout<<"Now taking pointer to object"; sejal *i; i=&a; sejal *h; h=&b; cout<<"\n"<<"Printing using pointer to object"<<i->*p<<"\n"<<h->*p; cout<<"Using pointers to member function"; void (sejal::*pf)(void)=&sejal::get(); a.*pf(); b.*pf(); void (sejal::*pg)(void)=&sejal::put(); a.*pg(); b.*pg(); cout<<"\nNow using pointer to object\n"; i->*pf(); h->*pf(); cout<<"\nPrinting"; i->*pg; h->*pg; cout<<"Done"; getch(); }



1Likes
LinkBack URL
About LinkBacks




