www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to use D-language polymorphism?

reply Jean Cesar <jeanzonta777 yahoo.com.br> writes:
I have the following code in c++ using polymorphism how would I 
convert D-language polymorphism?
#include <cstdlib>
#include <iostream>

class Mamifero
{
  protected:
    int idade;

  public:
    Mamifero(){}
    ~Mamifero(){}

    virtual void somMamifero() const
    {
      std::cout<<"\n\tSom de mamifero.\n";
   }
};

class Boi: public Mamifero
{
  public:
    void somMamifero() const
    {
     std::cout<<"\n\tMuu ..! Muu..!!\n";
    }
};

class Gato: public Mamifero
{
  public:
    void somMamifero() const
    {
     std::cout<<"\n\tMiAu ..! MiAu..!!\n";
    }
};

class Porco: public Mamifero
{
  public:
    void somMamifero() const
    {
     std::cout<<"\n\tOinc ..! Oinc..!!\n";
    }
};

class Cachorro: public Mamifero
{
  public:
    void somMamifero() const
    {
     std::cout<<"\n\tAu ..! Au..!!\n";
    }
};

int main()
{
   Mamifero* mamPtr;
   int op;
   while(op != 5)
   {
     std::cout<<"\n\t(1) Boi"
              <<"\n\t(2) Gato"
              <<"\n\t(3) Porco"
              <<"\n\t(4) Cachorro"
              <<"\n\t(5) Sair"
	     <<"\n\tDigite: ";
      std::cin>>op;
    switch(op)
     {
      case 1:{
        mamPtr = new Boi();
        mamPtr->somMamifero();
        break;
       }
      case 2:{
        mamPtr = new Gato();
        mamPtr->somMamifero();
        break;
       }
      case 3:{
        mamPtr = new Porco();
        mamPtr->somMamifero();
        break;
       }
      case 4:{
        mamPtr = new Cachorro();
        mamPtr->somMamifero();
        break;
       }
      case 5:{
        std::cout<<"\n\tGood Bye\n\n";
        exit(0);
        break;
       }
      default:
        std::cout<<"\n\tOpção Inválida ..!!!\n";
     }
   }
}
Feb 16 2017
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 02/16/2017 11:09 AM, Jean Cesar wrote:
 I have the following code in c++ using polymorphism how would I convert
 D-language polymorphism?
import std.stdio; class Mamifero { int idade; void somMamifero() { writeln("\n\tSom de mamifero."); } } class Boi: Mamifero { override void somMamifero() const { writeln("\n\tMuu ..! Muu..!!"); } } class Gato: Mamifero { override void somMamifero() const { writeln("\n\tMiAu ..! MiAu..!!"); } } class Porco: Mamifero { override void somMamifero() const { writeln("\n\tOinc ..! Oinc..!!"); } } class Cachorro: Mamifero { override void somMamifero() const { writeln("\n\tAu ..! Au..!!"); } } void main() { Mamifero mamPtr; int op; while(op != 5) { write("\n\t(1) Boi" ~ "\n\t(2) Gato" ~ "\n\t(3) Porco" ~ "\n\t(4) Cachorro" ~ "\n\t(5) Sair" ~ "\n\tDigite: "); readf(" %s", &op); switch(op) { case 1:{ mamPtr = new Boi(); mamPtr.somMamifero(); break; } case 2:{ mamPtr = new Gato(); mamPtr.somMamifero(); break; } case 3:{ mamPtr = new Porco(); mamPtr.somMamifero(); break; } case 4:{ mamPtr = new Cachorro(); mamPtr.somMamifero(); break; } case 5:{ writeln("\n\tGood Bye"); return; } default: writeln("\n\tOpção Inválida ..!!!"); } } } In D you can have interfaces as well: interface Mamifero { void somMamifero(); } Ali
Feb 16 2017