-
-
Save parzibyte/9fd34c96e717d3252a0d77dd6e5eec20 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
class Persona { | |
private: | |
std::string nombre; | |
int edad; | |
void metodoPrivado() { std::cout << "Llamada privada dentro de la clase\n"; } | |
public: | |
// Constructor sin argumentos | |
Persona() { | |
std::cout << "Se llama al constructor sin argumentos\n"; | |
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"; | |
} | |
}; | |
int main() { | |
// Crear persona con argumentos | |
Persona p1("Luis", 21); | |
p1.saludar(); | |
// Obtener su edad | |
int edad = p1.getEdad(); | |
// Crear otra persona, sin argumentos | |
Persona p2; | |
// Establecer valores por separado | |
p2.setEdad(1); | |
p2.setNombre("John Galt"); | |
p2.saludar(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment