Показать сообщение отдельно
Старый 10.04.2006, 06:07     # 2
gandalf_g
Junior Member
 
Регистрация: 09.12.2003
Адрес: Москва
Сообщения: 62

gandalf_g Путь к славе только начался
пример класса с перегруженным +=

Цитата:
#include <iostream>
using namespace std;

class Fraction
{
public:
Fraction(int num = 0, int den = 1)
{
this->num = num;
this->den = den;
}

Fraction& operator+=(const Fraction &rhs)
{
this->num = rhs.den * this->num +
this->den * rhs.num;
this->den = this->den * rhs.den;
return *this;
}

void print()
{
cout << num << "/" << den << endl;
}
private:
int num;
int den;
};

int main()
{
Fraction a(1,2);
Fraction c(1,1);

cout << "a is ";
a.print();
cout << endl;

cout << "c is ";
c.print();
cout << endl;

c += a;
cout << "c += a is ";
c.print();

return 0;
}
__________________
не учите людей думать, они вам этого не простят...
gandalf_g вне форума