Create a program that will overload a function four (4) times. The function takes a single parameter but each parameter is different in each function.
#include <iostream>
#include <cmath>
// Absolute value of integer
int modul(int i) {
if (i >= 0)
return i;
else
return -1;
}
// Absolute value of real number
double modul(double x) {
if (x >= 0)
return x;
else
return -x;
}
struct complex_t {
double real;
double imag;
};
// Absolute value of complex number
double modul(complex_t z) {
return std::sqrt(z.real*z.real + z.imag*z.imag);
}
// Module of 3-d vector
double modul(double v[]) {
return std::sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
}
int main() {
int i = 5;
double x = -12;
complex_t z = {1, 1};
double v[3] = { 1, 2, 3};
std::cout << "modul(i) = " << modul(i) << std::endl;
std::cout << "modul(x) = " << modul(x) << std::endl;
std::cout << "modul(z) = " << modul(z) << std::endl;
std::cout << "modul(v) = " << modul(v) << std::endl;
return 0;
}
Comments
Leave a comment