Задание: Создать класс целых чисел Integer. Определить перегруженную функцию, воз-вращающую максимальное из двух аргументов. Функция не является членом класса целых чисел. Перегруженные функции имеют аргументы типа int, double, Integer. Тело перегруженных функций должны быть одинаковыми.
Код я написал, он работает. Но сомневаюсь, что правильно реализовал перегрузку дружественных функций. Все ли я правильно сделал?
#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;
}