Advertisements
Home > Cpp > C++ Program to implement Pointer to function – Q14

C++ Program to implement Pointer to function – Q14


Q14. Program to implement Pointer to function:

Get the sum of two numbers using the concept of pointer to function.

… from College notes (BCA/MCA assignments):

#include <iostream.h>
#include <conio.h>

int sumNum(int a, int b){
	return (a+b);
	}

int (*ptr_f)(int, int);	// pointer to function.

void main(){
	int num1, num2;
	clrscr();

	cout<<"\n Enter 1st Number: ";
	cin>>num1;
	cout<<"\n Enter 2nd Number: ";
	cin>>num2;

	ptr_f = &sumNum;	// ptr_f points to display.

	cout<<"\n Sum of two Numbers: "<<ptr_f(num1, num2);

	getch();
}

 

Output:

Enter 1st Number: 34

Enter 2nd Number: 45

Sum of two Numbers: 79


Advertisements
Categories: Cpp Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: