Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Last active June 20, 2019 00:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/e62eaf81c32f2053cf4660fb9f917fc2 to your computer and use it in GitHub Desktop.
Save parzibyte/e62eaf81c32f2053cf4660fb9f917fc2 to your computer and use it in GitHub Desktop.
#include <iostream>
class Persona {
private:
std::string nombre;
int edad;
void metodoPrivado() {
std::cout << "Solo puedo ser llamado desde dentro de la clase";
}
public:
// Constructor sin argumentos
Persona() {
std::cout << "Se llama al constructor";
this->metodoPrivado();
}
// Constructor con nombre y edad
Persona(std::string nombre, int edad) {
this->edad = edad;
this->nombre = nombre;
}
int getEdad() { return this->edad; }
void setEdad(int edad) { this->edad = edad; }
std::string getNombre() { return this->nombre; }
void setNombre(std::string nombre) { this->nombre = nombre; }
void saludar() {
std::cout << "Hola, me llamo " << this->nombre << " y mi edad es "
<< this->edad << "\n";
}
// Definido virtual para que se pueda sobrescribir ;)
virtual void saludarAmigo(std::string nombre) {
std::cout << "Hola " << nombre << ", me llamo " << this->nombre
<< " y mi edad es " << this->edad << "\n";
}
};
class Estudiante : public Persona {
public:
// Constructor vacío
Estudiante(std::string nombre, int edad, std::string escuela)
: Persona(nombre, edad) {}
// Definir propios métodos
void estudiar() { std::cout << "*estudia*\n"; }
// Sobrescribir otros
void saludarAmigo(std::string nombre) override {
// Usamos getEdad y getNombre porque es una subclase
// y las variables son privadas
std::cout << "Qué onda " << nombre << ", me llamo " << this->getNombre()
<< " y mi edad es " << this->getEdad() << "\n";
}
};
int main() {
Persona p1("Luis", 21);
p1.saludar();
Persona p2;
p2.setEdad(1);
p2.setNombre("John Galt");
p2.saludar();
Estudiante e1("Luis", 3, "Ninguna");
e1.saludar();
e1.saludarAmigo("Pedro");
e1.estudiar();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment