0 Bewertungen0% fanden dieses Dokument nützlich (0 Abstimmungen)
4 Ansichten2 Seiten
The document describes a C++ program that illustrates function overloading. It defines a sum function that can take 2, 3, or 4 integer arguments. The main function calls each version of the sum function, prompting the user to enter the appropriate number of integers and displaying the result. Function overloading allows the same function name to be used for multiple tasks as long as the number or type of arguments is different.
The document describes a C++ program that illustrates function overloading. It defines a sum function that can take 2, 3, or 4 integer arguments. The main function calls each version of the sum function, prompting the user to enter the appropriate number of integers and displaying the result. Function overloading allows the same function name to be used for multiple tasks as long as the number or type of arguments is different.
The document describes a C++ program that illustrates function overloading. It defines a sum function that can take 2, 3, or 4 integer arguments. The main function calls each version of the sum function, prompting the user to enter the appropriate number of integers and displaying the result. Function overloading allows the same function name to be used for multiple tasks as long as the number or type of arguments is different.
Write a C++ program illustrating function overloading
Algorithm : 1.start 2.declare a,b,c,d,result variables. 3.read 2 integers and call function sum and display its result 4.read 3 integers and call function sum and display its result. 5.stop If Same function used for multiple tasks ,then it is called as function overloading PROGRAM #include <iostream.h> #include <conio.h> void main() { int sum(int,int); //function with 2 arguments int sum(int,int,int); //function with 3 arguments int sum(int,int,int,int); //function with 4 arguments int sum(int a,int b,int c,int d); clrscr(); int a,b,c,d,result; cout<<"\n\nfor 2 argument\n"; cout<<"Enter 2 Integers\n"; cin>>a>>b; result=sum(a,b); cout<<"Addition ="<<result; cout<<"\n\nfor 3 argument\n"; cout<<"Enter 3 Integers\n"; cin>>a>>b>>c; result=sum(a,b,c); cout<<"Addition ="<<result; cout<<"\n\nfor 4 argument\n"; cout<<"Enter 4 Integers\n"; cin>>a>>b>>c>>d; result=sum(a,b,c,d); cout<<"Addition ="<<result; getch(); }
//function with 2 argument
int sum(int a,int b) { return(a+b); } //function with 3 argument int sum(int a,int b,int c) { return(a+b+c); } //function with 4 argument int sum(int a,int b,int c,int d) { return(a+b+c+d); }
} INPUT & OUTPUT
for 2 arguments Enter 2 Integers 2 3 Addition =5 for 3 arguments Enter 3 Integers 2 3 4 Addition =9 for 4 arguments Enter 4 Integers 2 3 4 5 Addition =14