//SOBRECARGA DE OPERADORES
#include <iostream>
//#include "Vector.h"
using namespace std;
//***********.H*******************
/*#ifndef VECTOR_H
#define VECTOR_H
#include <iostream>
using namespace std;*/
class Vector
{
public:
Vector();
Vector(float _x, float _y, float _z);
void Leer_Vector();
void Imprimir_Vector();
Vector Sumar_Vectores(Vector &vec1, Vector &vec2);
Vector Restar_Vectores(Vector &vec1, Vector &vec2);
float Multiplicar_Vectores(Vector &vec1, Vector &vec2);
// Métodos de sobrecarga de operadores
Vector operator +(Vector &vec2);
Vector operator -(Vector &vec2);
float operator *(Vector &vec2);
Vector operator *(float k);//vector por escalar es un vector
//operador por incencion accede al los atributos de la clase
friend istream & operator>>(istream &entrada, Vector &vec2);
/*metodos de la clase */
//istream clase de c++ y friend atributos de la clase
friend ostream & operator>>(ostream &salida, Vector &vec2);
//ostream clase de c++ stram de flujo o de salida
~Vector();
private:
float x, y, z;
};
//#endif // VECTOR_H
//***********.CPP*******************
//#include "Vector.h"
Vector::Vector()
{x=0;
y=0;
z=0;
}
Vector::Vector(float _x, float _y, float _z)
{x=_x;
y=_y;
z=_z;
}
Vector::~Vector()
{
cout<<"FINAL....."<<endl; // Destructor
}
void Vector::Leer_Vector()
{
cout << "Introduce x: "; cin >> x;
cout << "Introduce y: "; cin >> y;
cout << "Introduce z: "; cin >> z;
}
void Vector::Imprimir_Vector()
{
cout << "(" << x << "i, " << y << "j, " << z << "k)" << endl;
}
Vector Vector::Sumar_Vectores(Vector &vec1, Vector &vec2)
{
return Vector(vec1.x + vec2.x, vec1.y + vec2.y, vec1.z + vec2.z);
}
Vector Vector::Restar_Vectores(Vector &vec1, Vector &vec2)
{
return Vector(vec1.x - vec2.x, vec1.y - vec2.y, vec1.z - vec2.z);
}
float Vector::Multiplicar_Vectores(Vector &vec1, Vector &vec2)
{
return (vec1.x * vec2.x + vec1.y * vec2.y + vec1.z * vec2.z);
}
// Métodos de sobrecarga de operadores
Vector Vector::operator +(Vector &vec2)
{
return Vector(x + vec2.x, y + vec2.y, z + vec2.z);
}
Vector Vector::operator -(Vector &vec2)
{
return Vector(x - vec2.x, y - vec2.y, z - vec2.z);
}
float Vector::operator *(Vector &vec2)
{
return (x * vec2.x + y * vec2.y + z * vec2.z);
}
Vector Vector::operator *(float k)
{
return Vector(x*k, y*k, z*k);//vector con escalar
}
//funcion de la clase alias friend
istream & operator>>(istream &entrada, Vector &vec2)
//istream clase de c++ y friend atributos de la clase
{
cout<<"Introduce componente en x:";
entrada>>vec2.x;
cout<<"Introduce componente en y:";
entrada>>vec2.y;
cout<<"Introduce componente en z:";
entrada>>vec2.z;
}
ostream & operator>>(ostream &salida, Vector &vec2)
//ostream clase de c++ stram de flujo o de salida
{
salida<<"("<<vec2.x<<"i,"<<vec2.y<<"j,"<<vec2.z<<"k)"<<endl;
}
//***********MAIN*******************
void vectores_sin_sobrecarga();
void vectores_con_sobrecarga();
int main()
{
int mps;
while (true)//bucle si es verdaderoo
{
cout << "******** MENU PRINCIPAL ********" << endl;
cout << "[1] Vectores sin sobrecarga" << endl;
cout << "[2] Vectores con sobrecarga" << endl;
cout << "[3] Salir" << endl;
cout << "Introduce la opcion del 1 al 3: ";
cin >> mps;
switch (mps)
{
case 1:
vectores_sin_sobrecarga();
break;
case 2:
vectores_con_sobrecarga();
break;
case 3:
cout << "Saliendo del programa." << endl;
return 0;
default:
cout << "no se puedeeee" << endl;
break;
}
system("pause");
system("cls");
}
}
void vectores_sin_sobrecarga()
{
Vector V1, V2, VR;
int opc;
float res;
cout << "Vector V1" << endl;
V1.Leer_Vector();
V1.Imprimir_Vector();
cout << "Vector V2" << endl;
V2.Leer_Vector();
V2.Imprimir_Vector();
while (true)
{
cout << " ******** MENU VECTORES SIN SOBRECARGA ******** " << endl;
cout << "[1] Suma Vectores" << endl;
cout << "[2] Resta Vectores" << endl;
cout << "[3] Multiplicar Vectores" << endl;
cout << "[4] Salir al menu principal" << endl;
cout << "Introduce la opcion del 1 al 4: ";
cin >> opc;
switch (opc)
{
case 1:
VR = VR.Sumar_Vectores(V1, V2);
VR.Imprimir_Vector();
break;
case 2:
VR = VR.Restar_Vectores(V1, V2);
VR.Imprimir_Vector();
break;
case 3:
res = VR.Multiplicar_Vectores(V1, V2);
cout << "Multiplicacion de vectores = " << res << endl;
break;
case 4:
cout << "Regresando al menu principal." << endl;
return; // Regresa al menú principal
default:
cout << "Opcion no valida. Intente de nuevo." << endl;
break;
}
system("pause");
system("cls");
}
}
void vectores_con_sobrecarga()
{
Vector V1, V2, VR;
int opc;
float res;
float k;
cout << "Vector V1" << endl;
//V1.Leer_Vector();
V1.Imprimir_Vector();
cin>>V1;
//cout<<V1;
cout << "Vector V2" << endl;
//V2.Leer_Vector();
cin>>V2;
V2.Imprimir_Vector();
//cout<<V1;
while (true)
{
cout << " ******** MENU VECTORES CON SOBRECARGA ******** " << endl;
cout << "[1] Suma Vectores" << endl;
cout << "[2] Resta Vectores" << endl;
cout << "[3] Multiplicar Vectores" << endl;
cout << "[4] Multiplicar Vector por Escalar" << endl;
cout << "[5] Salir al menu principal" << endl;
cout << "Introduce la opcion del 1 al 4: ";
cin >> opc;
switch (opc)
{
case 1:
VR = V1 + V2;
//cout<<VR;
VR.Imprimir_Vector();
break;
case 2:
VR = V1 - V2;
VR.Imprimir_Vector();
break;
case 3:
res = V1 * V2;
cout << "Multiplicacion de vectores = " << res << endl;
break;
case 4:
cout << "Multiplicar Vector por Escalar." << endl;
cin>>k;
VR=V1*k;
VR.Imprimir_Vector();
//cout<<VR;
return; // regresa al menuuu
case 5:
cout << "Regresando al menu principal." << endl;
return; // regresa al menuuu
default:
cout << "no se puedeeee " << endl;
break;
}
system("pause");
system("cls");
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: