using function pointers


#include<iostream>
using namespace std;


void add(int a,int b);
void divide(int a,int b);
void multiply(int a,int b);
void sub(int a,int b);
int a,b;
void(*f[4])(int,int)={divide,multiply,add,sub};//store functions adress starting from 0

int main(){

cout<<"enter 0 to divide "<<endl;
cout<<"enter 1 to multiply "<<endl;
cout<<"enter 2 to add "<<endl;
cout<<"enter 3 to subtract "<<endl;

while(terminate!=0){
int choice;
cout<<endl<<"your choice = ";
cin>>choice;
cout<<"enter 2 numbers "<<endl;
cout<<" a = ";
cin>>a;
cout<<endl<<" b = ";
cin>>b;
(*f[ choice ])( a,b );//calling function at our desired choice

}
}
void divide(int a,int b){
cout<<"the result is = "<<a/b<<endl;
}
void multiply(int a,int b){
cout<<"the result is = "<<a*b<<endl;
}
void add (int a,int b){
cout<<"the result is = "<<a+b<<endl;
}
void sub(int a,int b){
cout<<"the result is = "<<a-b<<endl;
}

















Previous
Next Post »

Comments:

Disqus Shortname