vect2 · Cristi
▶ Aprender vect2 (Cristi) paso a paso · explicación de vect2
Otras versiones: One line · Multi line · Orden del main · one line · Orden del main · multi line
Código línea a línea
Tres columnas: el código, el trozo del subject o del main que obliga a escribir esa línea, y el por qué.
vect2.hpp
| Código | Subject | Por qué |
|---|---|---|
#pragma once | Que el .hpp no se incluya dos veces. | |
#include <iostream> | printable with the << operator | std::ostream para el << que imprime. El main usa std::cout y solo incluye vect2.hpp. |
| ||
class vect2 { | Create a class vect2 | La clase. Llave en la misma línea: da igual. |
| ||
private: | x e y solo los toca la propia clase. | |
| ||
int x, y; | vector of dimension 2 containing ints | Los dos en una línea. |
| ||
public: | You will find a main in this directory that must work with your class. | Todo lo que usa el main. |
vect2() : x(0), y(0) {} | vect2 v1; // 0, 0 | Sin nada → {0, 0}. Lista de inicialización: x(0) = x empieza en 0. |
vect2(int x, int y) : x(x), y(y) {} | vect2 v2(1, 2); // 1, 2 | Mismo nombre que los miembros: en x(x) el de fuera es el miembro y el de dentro el parámetro. |
vect2(const vect2 &oth) : x(oth.x), y(oth.y) {} | const vect2 v3(v2);vect2 v4 = v2; | Copia. v4 = v2 al nacer también es copia, no operator=. |
vect2 &operator=(const vect2 &oth) { x = oth.x; y = oth.y; return *this; } | v2 = 3 * v2; | Me cambia → sin const. Me devuelvo a mí → vect2 &. |
| ||
int &operator[](int i) { return (i == 0 ? x : y); } | v1[1] = 12; | Cambiar. v1[1] = 12 necesita la casilla de verdad → int &. |
int operator[](int i) const { return (i == 0 ? x : y); } | v3[1] | Leer. v3 es const → hace falta la versión const. |
| ||
vect2 operator+(const vect2 &oth) const { return vect2(x + oth.x, y + oth.y); } | v2 = v3 + v3 *2; | Uno nuevo, construido ya sumado. Solo miro → const. |
vect2 &operator+=(const vect2 &oth) { x += oth.x; y += oth.y; return *this; } | v2 += v3;v2 += v2 += v3; | Me cambia y me devuelvo a mí → vect2 &. |
vect2 &operator++() { x++; y++; return *this; } | ++v4 | ++v4: me sumo 1 y me devuelvo a mí. |
vect2 operator++(int) { vect2 tmp = *this; x++; y++; return tmp; } | v4++ | v4++: guardo cómo era, me sumo, devuelvo lo viejo. |
| ||
vect2 operator-(const vect2 &oth) const { return vect2(x - oth.x, y - oth.y); } | v1 = v1 - v1 + v1; | Uno nuevo, construido ya restado. |
vect2 &operator-=(const vect2 &oth) { x -= oth.x; y -= oth.y; return *this; } | v1 -= v2; | Igual que += restando. |
vect2 &operator--() { x--; y--; return *this; } | --v4 | Igual que ++v4 restando. |
vect2 operator--(int) { vect2 tmp = *this; x--; y--; return tmp; } | v4-- | Igual que v4++ restando. |
vect2 operator-() const { return vect2(-x, -y); } | -v2 | Menos de un solo lado: uno nuevo con los dos signos cambiados. No me cambia → const. |
| ||
vect2 operator*(int s) const { return vect2(x * s, y * s); } | (vect2(2,2) * 2 == vect2(4,4))v3 *2 | v * 2: uno nuevo, construido ya multiplicado. |
vect2 &operator*=(int s) { x *= s; y *= s; return *this; } | multiplication by a scalarv1 *= 42; | Me cambia y me devuelvo a mí. |
friend vect2 operator*(int s, const vect2 &v) { return vect2(v.x * s, v.y * s); } | v2 = 3 * v2; | 3 * v: a la izquierda un int → friend (de fuera, pero ve x e y). |
| ||
bool operator==(const vect2 &oth) const { return x == oth.x && y == oth.y; } | (v1 == v3) | Sí/no → bool. Iguales si x e y coinciden. |
bool operator!=(const vect2 &oth) const { return !(*this == oth); } | (v1 != v3) | Lo contrario de ==. |
| ||
friend std::ostream &operator<<(std::ostream &os, const vect2 &v) { os << "{" << v.x << ", " << v.y << "}"; return os; } | printable with the << operator std::cout << "{" << v[0] << ", " << v[1] << "}" << std::endl; | friend: a la izquierda está cout. Escribe en os y devuelve os. |
}; |
vect2.cpp
| Código | Subject | Por qué |
|---|---|---|
#include "vect2.hpp" | Expected files: vect2.cpp, vect2.hpp | El .cpp es obligatorio, pero todo está en el .hpp: basta con incluirlo. |
El main, comentado
El main del subject con una nota en cada línea: qué función de la clase obliga a escribir.
main.cpp
#include "vect2.hpp" // → #pragma once + #include <iostream> en el .hpp (el main usa std::cout sin incluirlo)
int main()
{
vect2 v1; // 0, 0 → vect2()
vect2 v2(1, 2); // 1, 2 → vect2(int x, int y)
const vect2 v3(v2); // 1, 2 → vect2(const vect2 &) · v3 es const: todo lo que se le pida a v3 tiene que ser const
vect2 v4 = v2; // 1, 2 → vect2(const vect2 &) · v4 está naciendo: es copia, NO operator=
std::cout << "v1: " << v1 << std::endl; // → friend operator<< · devuelve os para poder encadenar << std::endl
std::cout << "v1: " << "{" << v1[0] << ", " << v1[1] << "}" << std::endl; // → int &operator[](int) · y el formato exacto que tiene que imitar operator<<
std::cout << "v2: " << v2 << std::endl;
std::cout << "v3: " << v3 << std::endl; // → operator<< recibe const vect2 & (si no, v3 no se puede imprimir)
std::cout << "v4: " << v4 << std::endl;
std::cout << v4++ << std::endl; // 2, 3 → vect2 operator++(int) · devuelve la copia de ANTES (sale 1, 2)
std::cout << ++v4 << std::endl; // 3, 4 → vect2 &operator++() · devuelve el ya sumado
std::cout << v4-- << std::endl; // 2, 3 → vect2 operator--(int)
std::cout << --v4 << std::endl; // 1, 2 → vect2 &operator--()
v2 += v3; // 2, 4 → vect2 &operator+=(const vect2 &)
v1 -= v2; // -2, -4 → vect2 &operator-=(const vect2 &)
v2 = v3 + v3 *2; // 3, 6 → operator*(int) const · operator+ const (v3 es const) · operator=
v2 = 3 * v2; // 9, 18 → friend operator*(int, const vect2 &): a la izquierda hay un int, no un vect2
v2 += v2 += v3; // 20, 40 → += no puede ser void: el (v2 += v3) de la derecha se usa como valor · devuelve vect2 &
v1 *= 42; // -84, -168 → vect2 &operator*=(int)
v1 = v1 - v1 + v1; // → operator-(const vect2 &) const
std::cout << "v1: " << v1 << std::endl;
std::cout << "v2: " << v2 << std::endl;
std::cout << "-v2: " << -v2 << std::endl; // → vect2 operator-() const: sin parámetro, cambia signos (la resta "de más")
std::cout << "v1[1]: " << v1[1] << std::endl;
v1[1] = 12; // → int &operator[](int): devuelve la casilla de verdad (con & para poder asignar)
std::cout << "v1[1]: " << v1[1] << std::endl;
std::cout << "v3[1]: " << v3[1] << std::endl; // → int operator[](int) const: la versión para leer, porque v3 es const
std::cout << "v1 == v3: " << (v1 == v3) << std::endl; // → bool operator==(const vect2 &) const
std::cout << "v1 == v1: " << (v1 == v1) << std::endl;
std::cout << "v1 != v3: " << (v1 != v3) << std::endl; // → bool operator!=(const vect2 &) const: !(*this == oth)
std::cout << "v1 != v1: " << (v1 != v1) << std::endl;
return (0);
} // → aquí se destruyen v1..v4: el destructor lo pone el compilador