Override toString in a C ++ class inheritance

-1

I'm trying to implement a toString() method for the classes below but I'm not making it work, when you access the method, only the one that is in class Vehiculo is executed. What am I doing wrong? Can I have to use override ?

In the hpp I have this:

class Vehiculo{
   virtual string toString();
}

class Auto : Vehiculo {
  string toString();
}

In the cpp I have this:

string Avion :: toString() {
  return "esto es un avion";
}


class Avion : Vehiculo {
  string toString();
}


string Auto :: toString() {
  return "esto es un auto";// ademas me interesa que se pueda acceder al toString de Vehiculo
}

But I'm not achieving what I want, what am I doing wrong? Greetings

    
asked by Mathias 24.03.2018 в 22:48
source

2 answers

3
  

What am I doing wrong?

class Vehiculo{
   virtual string toString();
}

In class the visibility of the elements is, by default, private. This means that only members of Vehiculo can access. To change this visibility you must expressly indicate:

class Vehiculo{
public: // <<--- Visibilidad publica
   virtual string toString();
}

And the same thing happens with inheritances ... if you use class the inheritances will be private unless otherwise indicated ... What does it mean that an inheritance is private?

It is a bit more convoluted to explain but simplifying a bit could be said that the inheritance will not be visible ... as if it did not exist for anyone except for the class that receives the inheritance.

At the moment to simplify your life try not to use private inheritance:

class Auto : public Vehiculo {
//           ^^^^^^ herencia publica
  string toString();
}

Note: By the way, the statements of a class must end always with a semicolon:

class Vehiculo{
public:
   virtual string toString();
}; // <<--- Importante!!!
  

may I have to use override ?

override is a reserved word that is available from the C ++ 11 standard and only serves for the compiler to help us detect inconsistencies when overwriting functions. When you mark a function as override , all you do is tell the compiler to verify the following:

  • That in some parent class there is a function with exactly the same statement
  • That the function of the parent class is marked as virtual

That is:

struct Base
{
  virtual void Func1(int) const;
  virtual void Func2(int) const;
  virtual void Func3(int) const;
  virtual int Func4(int) const;

  void Func5();
};

struct Derivada : Base
{
  void Func1(int) const override; // OK
  void Func2(int) override;       // ERROR: falta const
  void Func3() const override;    // ERROR: falta el argumento int
  void Func4(int) const override; // ERROR: el tipo de retorno no es el mismo
  void Func5() override;          // ERROR: Base::Func5 no es virtual
};

If in the previous example we had not used override the compiler would not notify us of those errors and the final program, very surely, would not work as we expect.

  

I'm also interested in accessing toString of Vehiculo

You can force the call to the function of the parent class with Vehiculo:: :

string Auto::toString() {
  return "esto es un auto\n" + Vehiculo::toString();
}
    
answered by 25.03.2018 / 11:26
source
2

I was testing your code in Visual C ++, I guess you copied wrong because you had problems with missing a semicolon. Also weird that you could access toString () because it is declared private. In addition, the inheritance was not public. You did not implement the Vehicle class implementation I the vehicle toString () would declare it as pure virtual.

#include <string>
#include <conio.h>
using namespace std;

class Vehiculo
{
public:
    virtual string toString(); // Esto personalmente lo declararía virtual puro
};
class Auto : public Vehiculo
{
    string toString();
};
class Avion : public Vehiculo
{
    string toString();
};

string Vehiculo :: toString() { return "esto es un vehiculo"; }
string Avion    :: toString() { return "esto es un avion"; }
string Auto     :: toString() { return "esto es un auto"; }

void main()
{
    Vehiculo* v1 = new Vehiculo();
    Vehiculo* v2 = new Avion();
    Vehiculo* v3 = new Auto();
    printf("v1: %s\n", v1->toString().c_str());
    printf("v2: %s\n", v2->toString().c_str());
    printf("v3: %s\n", v3->toString().c_str());
    delete v1;
    delete v2;
    delete v3;
    getch();
}
    
answered by 25.03.2018 в 01:50