How to define a subclass of an abstract class so that it is concrete

Asked

Viewed 59 times

1

With these classes:

class SerVivo {
 public:
  virtual void funcA() = 0;
  virtual void funcB() = 0;
};

class Vegetal : public SerVivo{
 public:
  virtual void funcB(){ cout << "funcB em Vegetal \n"; }
  virtual void funcC() = 0;
};

I wanted to know how I build the Tree class, derived from Vegetal, so that I can build Tree-type objects.

1 answer

0


You need to implement all functions purely virtual (defined as "virtual nome_da_duncao(...) = 0" ) of the hierarchy. How Arvore is descended from Vegetal, she needs to implement funcC. But how Vegetal is descended from SerVivo, she also needs to implement funcA. No need to implement funcB because this has already been implemented in Vegetal.

Example:

class Arvore : public Vegetal {
public:
    void funcA() { cout << "funcA em Arvore \n"; }
    void funcC() { cout << "funcC em Arvore \n"; }
};


int main()
{

    Arvore arvore1{};
    arvore1.funcA();
    arvore1.funcB();
    arvore1.funcC();

    return 0;
}

Exit:

funcA em Arvore                                                                                                                                                                             
funcB em Vegetal                                                                                                                                                                            
funcC em Arvore


...Program finished with exit code 0
  • Thanks for the help :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.