C++ Tutorial/Operator Overloading/overload with friend function — различия между версиями
Admin (обсуждение | вклад) м (1 версия: Импорт контента...) |
(нет различий)
|
Текущая версия на 13:30, 25 мая 2010
friend operator
<source lang="cpp">#include <iostream.h> class Num {
friend Num operator+(Num ob, int i); friend Num operator+(int i, Num ob); int count;
public:
Num(int cc=0){ count=cc; } Num& operator=(int i); void Show(){ cout <<count<<endl; }
}; Num& Num::operator=(int i){
count=i; return *this;
} Num operator+(Num ob, int i) // This handles ob+int. {
Num temp; temp.count=ob.count+i; return temp;
} Num operator+(int i, Num ob) // This handles int+ob. {
Num temp; temp.count=ob.count+i; return temp;
} main(void) {
Num obj; obj=10; obj.Show(); obj=10+obj; obj.Show(); obj=obj+12; obj.Show(); return 0;
}</source>
10 20 32