Advertisement
Guest User

Fuction Overloading

a guest
Oct 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. // прототипы перегруженных функций
  4. int multiplication(int n, int *a);
  5. float multiplication(int n, float *b);
  6. double multiplication(int n, double *c);
  7. int main()
  8. {
  9.     int n;
  10.     cin >> n;
  11.     int *a = new int[n]; // for first (int) multiplication
  12.     float *b = new float[n]; // for second (float) multiplication
  13.     double *c = new double[n];// for third (double) multiplication
  14.     for (int i = 0; i < n; i++) {
  15.         cin >> a[i];
  16.     }
  17.     cout << "Int multiplication: " << multiplication(n, a) << endl;
  18.     for (int i = 0; i < n; i++) {
  19.         cin >> b[i];
  20.     }
  21.     cout << "Float multiplication: " << multiplication(n, b) << endl;
  22.     for (int i = 0; i < n; i++) {
  23.         cin >> c[i];
  24.     }
  25.     cout << "Double multiplication: " << multiplication(n, c) << endl;
  26.     return 0;
  27. }
  28. int multiplication(int n, int *a) {
  29.     int res = 1;
  30.     for (int i = 0; i < n; i++) {
  31.         if (a[i] != 0)
  32.             res *= a[i];
  33.     }
  34.     return res;
  35. }
  36. float multiplication(int n, float *a) {
  37.     float res = 1;
  38.     for (int i = 0; i < n; i++) {
  39.         if (a[i] != 0)
  40.             res *= a[i];
  41.     }
  42.     return res;
  43. }
  44. double multiplication(int n, double *a) {
  45.     double res = 1;
  46.     for (int i = 0; i < n; i++) {
  47.         if (a[i] != 0)
  48.             res *= a[i];
  49.     }
  50.     return res;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement