C++ Program to implement Function Overloading – Q8
Q8. Program to implement Function Overloading:
Implement Function Overloading for following requirements
– Getting Sum of two number
– Concatenating two strings
– Adding two matrices
… from College notes (BCA/MCA assignments):
#include <iostream.h> #include <conio.h> const int MAX = 3; int Sum (int, int); void Sum (char *, char *); void Sum (int [][MAX], int [][MAX]); void main(){ int a, b; char x[10], y[10]; int mat1[MAX][MAX], mat2[MAX][MAX], tot[MAX][MAX]; clrscr(); cout<<"\n Integers:- "<<endl; cout<<"\n Enter value of a: "; cin>>a; cout<<"\n Enter value of b: "; cin>>b; cout<<"\n Sum of Integers "<<a<<" & "<<b<<" is: "<<Sum(a, b)<<endl; cout<<"\n Strings:- "<<endl; cout<<"\n Enter value of x: "; cin>>x; cout<<"\n Enter value of y: "; cin>>y; cout<<"\n Sum of Strings "<<x<<" & "<<y<<" is: "; Sum(x, y); cout<<"\n Integer Matrices:- "<<endl; cout<<"\n Enter values in Matrix mat1: "; for(int i=0; i<MAX; i++) for(int j=0; j<MAX; j++) cin>>mat1[i][j]; cout<<"\n Enter value in Matrix mat2: "; for(i=0; i<MAX; i++) for(j=0; j<MAX; j++) cin>>mat2[i][j]; cout<<"\n Sum of Matrices is:-"<<endl; Sum(mat1, mat2); getch(); } int Sum(int a, int b){ return (a+b); } void Sum(char *a, char *b){ int i = 0, j = 0; char c[20]; while(*(a+i) != '\0'){ *(c+i) = *(a+i); i++; } while(*(b+j) != '\0'){ *(c+i) = *(b+j); i++; j++; } *(c+i) = '\0'; cout<<c; } void Sum(int m1[MAX][MAX], int m2[MAX][MAX]){ int tot[MAX][MAX]; for(int i=0; i<MAX; i++){ for(int j=0; j<MAX; j++){ tot[i][j] = m1[i][j] + m2[i][j]; cout<<" "<<tot[i][j]; } cout<<endl; } }
Output:
Integers:-
Enter value of a: 45
Enter value of b: 12
Sum of Integers 45 & 12 is: 57
Strings:-
Enter value of x: Manoj
Enter value of y: Pandey
Sum of Strings Manoj & Pandey is: ManojPandey
Integer Matrices:-
Enter values in Matrix mat1: 1 2 3 4 5 6 7 8 9
Enter values in Matrix mat2: 9 8 7 6 5 4 3 2 1
Sum of Matrices is :-
10 10 10
10 10 10
10 10 10
Categories: Cpp
Cpp, Function Overloading
Comments (0)
Trackbacks (0)
Leave a comment
Trackback