How can I create object from a file in C++?

You can do it like this for each line:

void Complejo::f(const std::string& line)
{
  std::stringstream  s(line);
  s>>real>>imaginary;
}

You can add checks for a legit line etc.

BTW: the mostrar function should be able to print negative values like: -1-1i, therefore it can be:

std::string Complejo::mostrar()const{
    std::stringstream  s;
    s << real << (imaginary >= 0) ? "+" : "" << imaginary <<"i";
    return s.str();
}

Leave a Comment