Task: Create a class of integers Integer. Determine the overloaded function that returns the maximum of two arguments. The function is not a member of the class of integers. Overloaded functions have arguments of type int, double, Integer. The body of overloaded functions should be the same.
I wrote the code, it works. But I doubt that I correctly implemented the overload of friendly functions. Did I do the right thing?
#include "stdafx.h" #include <iostream> #include <conio.h> using namespace std; class Integer { private: double a, b; public: Integer(); ~Integer(); void input(); void output(); friend int max(int a, double b); friend int max(int a, double b, Integer c); }; Integer::Integer() { a = 0; b = 0; cout << "Constructor works" << endl; } Integer::~Integer() { cout << "Destructor works" << endl; a = NULL; b = NULL; } void Integer::input() { cout << "Input two elements: " << endl;; cin >> a >> b; } void Integer::output() { cout << "The largest number is " << max(a,b)<<endl; } int max(int a, double b) { if (a > b) { cout << "The first number is larger than the second" << endl; return a; } else { cout << "The second number is larger than the first" << endl; return b; } } int max(int a, double b, Integer c) { if (a > b) { cout << "The first number is larger than the second" << endl; return a; } else { cout << "The second number is larger than the first" << endl; return b; } } int main() { Integer c; char p='y'; while (p=='y'||p=='Y') { c.input(); c.output(); cout << "Want to continue? y/n" << endl; p = _getch(); } return 0; }