Skip to content

Paso — 2-constructor-metodos

Step 3 of 4

Constructor and methods

The constructor initializes the object. Methods define behavior (show, encrypt, etc.).

						#include <iostream>
#include <string>

class Mensaje {
private:
    std::string texto;

public:
    Mensaje(const std::string& t) : texto(t) {}

    void mostrar() const {
        std::cout << texto << std::endl;
    }
};

int main() {
    Mensaje m("Hola desde POO");
    m.mostrar();
    return 0;
}
					
						public:
    Mensaje(const std::string& t) : texto(t) {}

    void mostrar() const {
        std::cout << texto << std::endl;
    }