Está en la página 1de 15

SOBRECARGA DE OPERADORES

#include<iostream>
#include<windows.h>
using namespace std;
struct complejo
{
float r,i;
};
complejo operator + (complejo a, complejo b)
{
complejo TEMP={a.r +b.r , a.i+b.i};
return TEMP;
}
int main()
{
system("title NUMEROS COMPLEJOS");
int a=-8,b=0;
complejo X={1,2};
complejo Y={2,5};
complejo Z;
Z=X+Y;
a=a+b;

cout<<endl<<"RESULTADO = "<<Z.r<<","<<Z.i<<"i"<<endl;
cout<<endl<<a;

_____________________________________________________________________________________
NUMEROS COMPLEJOS

#include<iostream>
#include<windows.h>
using namespace std;

struct complejo
{
float r,i;
};

complejo operator + (complejo a, complejo b)


{
complejo TEMP={a.r +b.r , a.i+b.i};
return TEMP;
}

complejo operator - (complejo c, complejo d)


{
complejo TEMP={c.r - d.r , c.i - d.i};
return TEMP;
}

complejo operator * (complejo e,complejo f)


{
complejo TEMP={ (e.r*f.r)-(e.i*f.i) ,(e.r*f.i)+(e.i*f.r) };
return TEMP;
}

complejo operator / (complejo g,complejo h)


{

complejo TEMP={ ( ( (g.r*h.r)+(g.i*h.i) ) / ( (h.r*h.r)+


(h.i*h.i) ) ) , ( ( ( g.i*h.r )-(g.r*h.i) ) / ( (h.r*h.r)+(h.i*h.i) ) ) };
return TEMP;
}

int main()
{
inicio:
float x1,x2,y1,y2;
int op;
system("cls");
system("title CALCULADORA DE NUMEROS COMPLEJOS");

cout<<"\n\tIntroducir el Primer Numero Complejo:\n";


cout<<"\n\t\tNumero Real:

";

cin>>x1;
cout<<"\t\tNumero Imaginario:

";

cin>>x2;
cout<<"\nX= [ "<<x1<<" , "<<x2<<" i]";

cout<<endl<<"\n\tIntroducir el Segundo Numero


Complejo:\n";
cout<<"\n\t\tNumero Real:

";

cin>>y1;
cout<<"\t\tNumero Imaginario:

";

cin>>y2;
cout<<"\nY= [ "<<y1<<" , "<<y2<<" i]\n\n\t\t";

system("pause");

complejo X={x1,x2};
complejo Y={y1,y2};
complejo Z;

start:
system("cls");

cout<<"Se Obtuvieron Los Siguientes Numeros:\n";


cout<<"\nX= [ "<<x1<<" , "<<x2<<" i]";
cout<<"\nY= [ "<<y1<<" , "<<y2<<" i]\n\n";
cout<<"\t\t\tQue Operacion Desea Hacer?..."<<endl<<endl;
cout<<"\t\tPARA SUMAR.....................................(1)\n";
cout<<"\t\tPARA RESTAR....................................(2)\n";
cout<<"\t\tPARA MULTIPLICAR...............................(3)\n";
cout<<"\t\tPARA DIVIDIR...................................(4)\n\n";
cout<<"\t\tPARA VOLVER A INTRODUCIR DATOS.................(5)\n";
cout<<"\t\tPARA SALIR.....................................(6)\n\n\t\t";
cin>>op;

switch(op)
{
case 1:

system("cls");
cout<<"\t\t\t\tSumando...\n\n";
cout<<"X= [ "<<x1<<" , "<<x2<<" i]";
cout<<"\nY= [ "<<y1<<" , "<<y2<<" i]\n\n";

cout<<"\t\t\t\t [X+Y]\n\n";

cout<<"\t\t\t[ "<<x1<<" , "<<x2<<" i] + [ "<<y1<<" ,


"<<y2<<" i]";

cout<<"\n\n\t\tCalculando...\n\n";
system("pause");
Z=X+Y;
cout<<endl<<"\n\t\tRESULTADO = [ "<<Z.r<<" ,
"<<Z.i<<" i]"<<endl<<endl;
system("pause");
goto start;
break;

case 2:
system("cls");
cout<<"\t\t\t\tRestando...\n\n";
cout<<"X= [ "<<x1<<" , "<<x2<<" i]";
cout<<"\nY= [ "<<y1<<" , "<<y2<<" i]\n\n";

cout<<"\t\t\t\t [X-Y]\n\n";
cout<<"\t\t\t[ "<<x1<<" , "<<x2<<" i] - [ "<<y1<<" ,
"<<y2<<" i]";

cout<<"\n\n\t\tCalculando...\n\n";
system("pause");
Z=X-Y;
cout<<endl<<"\n\t\tRESULTADO = [ "<<Z.r<<" ,
"<<Z.i<<" i]"<<endl<<endl;
system("pause");
goto start;
break;

case 3:
system("cls");
cout<<"\t\t\t\tMultiplicando...\n\n";
cout<<"X= [ "<<x1<<" , "<<x2<<" i]";
cout<<"\nY= [ "<<y1<<" , "<<y2<<" i]\n\n";

cout<<"\t\t\t\t [X*Y]\n\n";
cout<<"\t\t\t[ "<<x1<<" , "<<x2<<" i] * [ "<<y1<<" ,
"<<y2<<" i]";

cout<<"\n\n\t\tCalculando...\n\n";
system("pause");
Z=X*Y;
cout<<endl<<"\n\t\tRESULTADO = [ "<<Z.r<<" ,
"<<Z.i<<" i]"<<endl<<endl;
system("pause");
goto start;
break;

case 4:
system("cls");
cout<<"\t\t\t\tDividiendo...\n\n";
cout<<"X= [ "<<x1<<" , "<<x2<<" i]";
cout<<"\nY= [ "<<y1<<" , "<<y2<<" i]\n\n";

cout<<"\t\t\t\t [X/Y]\n\n";
cout<<"\t\t\t[ "<<x1<<" , "<<x2<<" i] / [ "<<y1<<" ,
"<<y2<<" i]\n";

cout<<"\n\n\t\tCalculando...\n\n";
system("pause");

Z=X/Y;
cout<<endl<<"\n\t\tRESULTADO = [ "<<Z.r<<" ,
"<<Z.i<<" i]"<<endl<<endl;

system("pause");
goto start;
break;

case 5:
goto inicio;
break;

case 6:
goto exit;
break;

default:
cout<<"\n\n\tCaracter NO Valido, Intentelo de
Nuevo...\n\n\n\n";
system("pause");
goto start;
}

exit:
cout<<"\n\nAdios...\n";

_____________________________________________________________________________________

SOBRECARGA DE OPERADOR CIN (>>)


#include<iostream>
#include<windows.h>
using namespace std;

struct complejo
{
float r,i;
};

complejo operator + (complejo a,complejo b)


{
complejo TEMP={ (a.r+b.r) , (a.i+b.i) };
return TEMP;
}

ostream& operator << (ostream& out,complejo& c)


{
out << " ( " << c.r << " , " << c.i << " ) ";
return out;
}

istream& operator >> (istream& in, complejo& c)


{
cout<<"\n\n\t\tIntroducir Valores Para ( r , i ) : \n\t\t";
in>>c.r>>c.i;
return in;
}

int main ()

{
complejo X{50,75};

complejo Y;
cin>>Y;

complejo Z = X + Y ;

cout <<endl;
cout << "\t\tX = " << X <<"\n";
cout << "\t\tY = " << Y <<"\n";
cout << "\t\tZ = " << Z <<"\n\n";

return 0;
}

_____________________________________________________________________________________

SOBRECARGA DE OPERADOR COUT (<<)


#include<iostream>
#include<windows.h>
using namespace std;

struct complejo
{
float r,i;
};

complejo operator + (complejo a,complejo b)


{
complejo TEMP={ (a.r+b.r) , (a.i+b.i) };
return TEMP;
}

ostream& operator << (ostream& out,complejo& c)


{
out << " ( " << c.r << " , " << c.i << " ) ";
return out;
}

int main()
{

system("title Sobrecarga Operador cout (ostream)");


complejo X {50,75};
complejo Y {150,175};
complejo Z = X+Y;

cout <<endl;
cout << "\t\tX = " << X <<"\n";
cout << "\t\tY = " << Y <<"\n";
cout << "\t\tZ = " << Z <<"\n\n";

return 0;
}
_____________________________________________________________________________________

CLASES AMIGAS
#include <iostream>
using namespace std;

class A
{
private:
int a;
public:
A()
{
a=0;cout<<"Constante 1 Creado"<<endl;
}
void Aa()
{
cin>>a;
}
};

class B: public A
{
private:
float b;
public:

B()
{
b=0;cout<<"Objeto 2 Creado";
}
void Ab()

{
cin>>b;
}

};

int main()
{
A ob1; B ob2;

ob1.Aa();
ob2.Aa();
ob2.Ab();
}

_____________________________________________________________________________________

_____________________________________________________________________________________

También podría gustarte