Sobrecarga de operadores 2

#include <windows.h> #include <iostream> using namespace std; class Numeros { public: //Variable int real, img; //Inicializando variables #TooMuchValue Numeros() { real = 0; img = 0; } //Sobrecarga de operadores bool operator==(int a) { if (img == 0 && real == 0) return true; else return false; } Numeros operator=(Numeros a) { real = a.real; img = a.img; return a; } Numeros operator+(Numeros a) { a.real = real + a.real; a.img = img + a.img; return a; } Numeros operator-(Numeros a) { a.real = real - a.real; a.img = img - a.img; return a; } Numeros operator*(Numeros a) { a.real = real * a.real; a.img = img * a.img; return a; } Numeros operator/(Numeros a) { if (a.real != 0) { a.real = real / a.real; } if (img != 0) { a.img = img / a.img; } return a; } }; ostream& operator<< (ostream& o, Numeros num) { o << "Numero imaginario: " << num.img << endl << "Numero real: " << num.real << endl; return o; } istream& operator>> (istream& i, Numeros &num) { cout << "Ingrese el numero real:" << endl; i >> num.real; cout << "Ingrese el numero imaginario: " << endl; i >> num.img; return i; } int main() { Numeros A, B, D; cout << "Numeros A:" << endl; cin >> A; cout << "Numeros B: " << endl; cin >> B; D = A + B; cout << D; D = A - B; cout << D; D = A * B; cout << D; if (B == 0) cout << "No es posible hacer las divisiones" << endl; else //No está ajustado para que soporte decimales D = A / B; cout << D; system("pause"); return 0; }
Programa de sobrecarga de operadores, ahora con operadores aritméticos y "<<", ">>".

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.