vect2

Código comentado línea a línea y paso a paso:

Entregas: vect2.hpp, vect2.cpp. Flags: c++ -Wall -Wextra -Werror -std=c++98

Qué pide (en palabras normales)

Subject (enunciado original)
Assignement name: vect2
Expected files: vect2.cpp, vect2.hpp
-------------------------------------------------------------------------------------------

Create a class vect2 representing a mathematical vector of dimension 2 containing ints.

It must support addition, substraction and multiplication by a scalar (such as the expression (vect2(2,2) * 2 == vect2(4,4)) is true).

the operator [] can be used to access the component of your vector (with 0 and 1), no bound checking is required.

Your function must be printable with the << operator such as:
        std::cout << v << std::endl;
produce the same output as:
        std::cout << "{" << v[0] << ", " << v[1] << "}" << std::endl;

You will find a main in this directory that must work with your class.

#include "vect2.hpp"
#include <iostream>

int main()
{
    vect2 v1; // 0, 0
    vect2 v2(1, 2); // 1, 2
    const vect2 v3(v2); // 1, 2
    vect2 v4 = v2; // 1, 2

    std::cout << "v1: " << v1 << std::endl;
    std::cout << "v1: " << "{" << v1[0] << ", " << v1[1] << "}" << std::endl;
    std::cout << "v2: " << v2 << std::endl;
    std::cout << "v3: " << v3 << std::endl;
    std::cout << "v4: " << v4 << std::endl;
    std::cout << v4++ << std::endl; // 2, 3
    std::cout << ++v4 << std::endl; // 3, 4
    std::cout << v4-- << std::endl; // 2, 3
    std::cout << --v4 << std::endl; // 1, 2
    v2 += v3; // 2, 4
    v1 -= v2; // -2, -4
    v2 = v3 + v3 *2; // 3, 6
    v2 = 3 * v2; // 9, 18
    v2 += v2 += v3; // 20, 40
    v1 *= 42; // -84, -168
    v1 = v1 - v1 +v1;
    std::cout << "v1: " << v1 << std::endl;
    std::cout << "v2: " << v2 << std::endl;
    std::cout << "-v2: " << -v2 << std::endl;
    std::cout << "v1[1]: " << v1[1] << std::endl;
    v1[1] = 12;
    std::cout << "v1[1]: " << v1[1] << std::endl;
    std::cout << "v3[1]: " << v3[1] << std::endl;
    std::cout << "v1 == v3: " << (v1 == v3) << std::endl;
    std::cout << "v1 == v1: " << (v1 == v1) << std::endl;
    std::cout << "v1 != v3: " << (v1 != v3) << std::endl;
    std::cout << "v1 != v1: " << (v1 != v1) << std::endl;
}

[MAIN]
#include "vect2.hpp"

int main()
{
	vect2 v1; // 0, 0
	vect2 v2(1, 2); // 1, 2
	const vect2 v3(v2); // 1, 2
	vect2 v4 = v2; // 1, 2

	std::cout << "v1: " << v1 << std::endl;
	std::cout << "v1: " << "{" << v1[0] << ", " << v1[1] << "}" << std::endl;
	std::cout << "v2: " << v2 << std::endl;
	std::cout << "v3: " << v3 << std::endl;
	std::cout << "v4: " << v4 << std::endl;
	std::cout << v4++ << std::endl; // 2, 3
	std::cout << ++v4 << std::endl; // 3, 4
	std::cout << v4-- << std::endl; // 2, 3
	std::cout << --v4 << std::endl; // 1, 2


	v2 += v3; // 2, 4
	v1 -= v2; // -2, -4
	v2 = v3 + v3 *2; // 3, 6
	v2 = 3 * v2; // 9, 18
	v2 += v2 += v3; // 20, 40
	v1 *= 42; // -84, -168
	v1 = v1 - v1 + v1;

	std::cout << "v1: " << v1 << std::endl;
	std::cout << "v2: " << v2 << std::endl;
	std::cout << "-v2: " << -v2 << std::endl;
	std::cout << "v1[1]: " << v1[1] << std::endl;
	v1[1] = 12;
	std::cout << "v1[1]: " << v1[1] << std::endl;
	std::cout << "v3[1]: " << v3[1] << std::endl;
	std::cout << "v1 == v3: " << (v1 == v3) << std::endl;
	std::cout << "v1 == v1: " << (v1 == v1) << std::endl;
	std::cout << "v1 != v3: " << (v1 != v3) << std::endl;
	std::cout << "v1 != v1: " << (v1 != v1) << std::endl;

	return (0);
}

4 piezas

1. this = "yo"

this = el vector que ejecuta la función ("yo"). *this = yo mismo.

2. const al final = "solo miro"

Pregunta única: ¿cambia algo de mi vector? No → const · Sí → sin const.

El main tiene const vect2 v3 → a v3 solo se le pueden pedir cosas const.

3. return *this = "me devuelvo a mí" → vect2 &

vect2&	operator+=(const vect2& o) { x += o.x; y += o.y; return *this; }   // me devuelvo a mí

a += b cambia a y te devuelve el mismo a. Si ves return *this;, delante va vect2 &. Siempre juntos.

4. Devolver uno nuevo → vect2 (sin &)

vect2	operator+(const vect2& o) const { vect2 t(*this); return t += o; }   // copia de mí, le sumo y la devuelvo

a + b no cambia ni a ni b: fabrica un vector nuevo y te lo da. Es nuevo, no soy "yo" → sin &. Y como no me cambia → const.

Cristi no copia: construye el nuevo directamente, return vect2(x + oth.x, y + oth.y);. Mismo resultado.

vect2 función por función

Función¿Cambia algo?¿Qué devuelve?Firma
constructor vacío--vect2();
constructor con números--vect2(int a, int b);
copia--vect2(const vect2 &o);
destructor--~vect2();
=síyovect2 &operator=(const vect2 &o);
v[i] para leernoun númeroint operator[](int i) const;
v[i] para cambiarsíla casilla de verdadint &operator[](int i);
+= -= *=síyovect2 &operator+=(const vect2 &o);
+ - *nouno nuevovect2 operator+(const vect2 &o) const;
-vnouno nuevovect2 operator-() const;
++v --vsíyovect2 &operator++();
v++ v--síuno nuevo (copia de cómo era antes)vect2 operator++(int);
== !=nosí/nobool operator==(const vect2 &o) const;
friend (a la izquierda no hay un vect2: no pueden ser miembros, pero se escriben dentro de la clase)
2 * v-uno nuevofriend vect2 operator*(int n, const vect2 &v);
cout << v-el mismo osfriend std::ostream &operator<<(std::ostream &os, const vect2 &v);

*= y * con int n es lo que usa el main. Las versiones con const vect2 &o son extra (optim las trae, cristi no).

v[i] tiene dos versiones: leer (da una copia del número, const) y cambiar (v[1] = 12 necesita la casilla de verdad → int &).

v++ es la rara: sí cambia v, pero te devuelve una copia de cómo era antes. El (int) no se usa: solo sirve para distinguirlo de ++v.

Cuerpos

vect2()        : x(0), y(0) {}
vect2(a, b)    : x(a), y(b) {}
copia          : x(o.x), y(o.y) {}
=              x = o.x; y = o.y; return *this;
v[i]           return i == 0 ? x : y;          (las dos versiones igual)
+= -= *=       tocan x e y · return *this;
+  -  *        vect2 t(*this); return t += o;   (usa el de arriba)
-v             return vect2(-x, -y);
++v            ++x; ++y; return *this;
v++            vect2 t(*this); ++(*this); return t;
==             return x == o.x && y == o.y;
!=             return !(*this == o);
2 * v          return v * n;
cout << v      friend: return os << "{" << v.x << ", " << v.y << "}";

Orden: escribe primero +=, -=, *=. Luego +, -, * salen gratis usándolos.

Checklist