Polymorphie



  • Hallo habe ein Problem bei einer Aufgabe, ich soll hier Typumwandlungspolymorphie mitttel Operator machen ... weiß leider nicht wie das funktioniert kenne nur parametische und Überladungspolymorphie

    #include <iostream>
    #include <cstdlib>
    using namespace std;
    class Multiplikation {
    public:
    Multiplikation(const double& op1,const double& op2):op1_(op1),op2_(op2) {
    }
    double ergebnis() const {
    return op1_ * op2_;
    }
    protected:
    double op1_,op2_;
    };
    class Addition {
    public:
    Addition(const double& op1,const double& op2):op1_(op1),op2_(op2) {
    }
    double ergebnis() const {
    return op1_ + op2_;
    }
    operator Multiplikation() const {
    	return Multiplikation();
    }
    protected:
    double op1_,op2_;
    };
    void anzeigen(const Multiplikation & ausdruck) {
    cout << ausdruck.ergebnis() << endl;
    }
    int main() {
    Addition a(2,3);
    Multiplikation m(2,3);
    anzeigen(a);
    anzeigen(m);
    system("pause");
    return 0;
    

    der Compiler meldet ständig "Kein geeigneter Standardkonstruktor verfügbar"

    Hilfe!!!



  • fehler ist da:

    operator Multiplikation() const { 
        return Multiplikation(); 
    }
    

    und da

    void anzeigen(const Multiplikation & ausdruck) {
    

    cu



  • und wie mach ich das nun richtig???



  • polymorphie ist zb sowas:

    class Mensch
    {
    public:
        virtual void zeichnen()
    	{
    		cout << "sdlfnlsndoi324324'$$$%&&//()()))" << endl;
    	}
    };
    
    class Kind: public Mensch
    {
    public:
    	void zeichnen()
    	{
    		cout << "----------------" << endl;
    	}
    };
    
    class Omi: public Mensch
    {
    public:
       void zeichnen()
       {
    	   cout << "~##++~~~~1212``´´´''**++~~**''''" << endl;
       }
    };
    
    class Lebewesen
    {
    public:
    	void zeichnen(Mensch *mensch)
    	{
    		mensch->zeichnen();
    	}
    };
    
    int main()
    {
      Lebewesen m_ptr;
    
      Kind k1;
      m_ptr.zeichnen(&k1);
    
      Omi o1;
      m_ptr.zeichnen(&o1);
    
      cin.get();
    
      return 0;
    }
    

    voraussetzung für polymorphie ist ne vererbung...

    cu



  • danke erstmal ... weiß schon ungefähr was Polymorphie ist aber wie ich meinen Fehler korrigiere weiß ich trotzdem noch nicht



  • class Addition
    {
    public:
    	Addition(double op1, double op2): op1_(op1), op2_(op2) {} 
    
    	virtual double ergebnis() 
    	{ 
    		return op1_ + op2_; 
    	} 
    
    protected:
    	double op1_;
    	double op2_;
    };
    
    class Multiplikation: public Addition
    {
    public:
    	Multiplikation(double op1, double op2): Addition(op1, op2) {}
    
    	double ergebnis()
    	{ 
    		return op1_ * op2_; 
    	} 
    };
    
    void anzeigen(Addition *ausdruck) 
    { 
    	cout << ausdruck->ergebnis() << endl; 
    }
    
    int main() 
    {
    	Addition a1(2, 6);
    	anzeigen(&a1);
    
    	Multiplikation m1(2, 6);
    	anzeigen(&m1);
    
             cin.get();	
    	return 0;
    }
    

    der sinn davon sei mal dahin gestellt...
    schau mal was passiert wenn man das virtual löscht...

    cu 😃


Anmelden zum Antworten