Добавить Перегрузку Опраторов += И [] К Имеющемуся Коду

yurov

New member
10.01.2013
1
0
#1
есть перегрузки для пяти операторов,нужно добавить еще две перегрузки,а именно += и []



#include "stdafx.h"
#include <iostream>

class Number
{
private:
double num;

public:
Number() { }
Number(double n): num(n) { }
double getNum() const;
Number operator +(const Number &);
Number operator -(const Number &);
Number operator *(const Number &);
Number operator /(const Number &);
Number &operator =(const Number &);
};

double Number::getNum() const
{
return num;
}

Number Number::eek:perator +(const Number &rhs)
{
return Number (num + rhs.getNum());
}

Number Number::eek:perator -(const Number &rhs)
{
return Number (num - rhs.getNum());
}

Number Number::eek:perator *(const Number &rhs)
{
return Number (num * rhs.getNum());
}

Number Number::eek:perator /(const Number &rhs)
{
return Number (num / rhs.getNum());
}

Number &Number::eek:perator =(const Number &rhs)
{
if (this == &rhs)
return *this;

num = rhs.getNum();

return *this;
}

int main()
{
Number n1, n2, n3;

n1 = 1;
n2 = 2;
n3 = n1 + n2;

std::cout << "n1 = " << n1.getNum() << "\tn2 = " << n2.getNum() << "\tn3 = " << n3.getNum() << std::endl;

n3 = n1 - n2;

std::cout << "n1 = " << n1.getNum() << "\tn2 = " << n2.getNum() << "\tn3 = " << n3.getNum() << std::endl;

n3 = n1 * n2;

std::cout << "n1 = " << n1.getNum() << "\tn2 = " << n2.getNum() << "\tn3 = " << n3.getNum() << std::endl;

n3 = n1 / n2;

std::cout << "n1 = " << n1.getNum() << "\tn2 = " << n2.getNum() << "\tn3 = " << n3.getNum() << std::endl;

std::cin.get();
return 0;
}
 
W

Whatka

#2
в чём проблема?
уже перегрузили пару операторов,по аналогии,мм?