<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Vererbung]]></title><description><![CDATA[<p>Hallo!<br />
Ich habe folgende Matrixklasse geschrieben:</p>
<pre><code class="language-cpp">class Matrix
{
protected:
	int m, n; //Dimensionen
	vector &lt;double&gt; entry; //Einträge der m x n Matrix
public:
	int getM(){return m;};
	int getN(){return n;};
	virtual double getCoefficient(int i, int j){return entry[i*getM() + j];};
	virtual void setCoefficient(int i, int j, double coeff){entry[i*getM() + j] = coeff;};
	void print(){for(int i =0; i&lt;m; i++){for(int j = 0; j&lt;n; j++){cout&lt;&lt;&quot;[&quot;&lt;&lt;i&lt;&lt;&quot;][&quot;&lt;&lt;j&lt;&lt;&quot;]: &quot;&lt;&lt;getCoefficient(i, j)&lt;&lt;&quot; &quot;;}cout&lt;&lt;endl;}cout&lt;&lt;endl;};
	Matrix(int m, int n); //Konstruktor für Matrix m x n ohne Einträge
	Matrix(int m, int n, double init);	//Konstruktor für m x n Matrix mit init intialisiert
	virtual void init(); //komplette Matrix initialisieren
	virtual Matrix operator*(Matrix &amp;M); //Matrix Matrix Multiplikation
};
</code></pre>
<p>Von dieser Klasse habe ich jetzt die Klasse SquareMatrix abgeleitet:</p>
<pre><code class="language-cpp">class SquareMatrix : public Matrix //speichert quadratische Matrizen
{
public:
	SquareMatrix(int n, double init) : Matrix(n, n, init){}; //Konstruktor wird von Matrix übernommen
};
</code></pre>
<p>Was ich jetzt erreichen möchte, ist, Objekte vom Typ SquareMatrix mit dem * - Operator zu multiplizieren. Da der Rückgabetyp hier aber Matrix ist, funktioniert das bei mir nicht. Ich hab das jetzt vorerst so gelöst, dass ich die Klasse SquareMatrix um die Methode &quot;SquareMatrix operator*(SquareMatrix &amp;M);&quot; erweitert habe, also den Rückgabetypen angepasst.<br />
Das scheint mir aber keine besonders elegante Lösung zu sein, meine Frage daher:<br />
gibt es eine Möglichkeit, die Methode in die abgeleitete Klasse zu übernehmen, sodass sie auch von den abgeleiteten Klassen ohne spezielle Anpassung verwendet werden können?</p>
<p>Danke im Voraus<br />
Lg Monkey12345</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/305074/vererbung</link><generator>RSS for Node</generator><lastBuildDate>Thu, 25 Jun 2026 07:23:15 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/305074.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 20 Jun 2012 12:27:12 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Vererbung on Wed, 20 Jun 2012 12:27:12 GMT]]></title><description><![CDATA[<p>Hallo!<br />
Ich habe folgende Matrixklasse geschrieben:</p>
<pre><code class="language-cpp">class Matrix
{
protected:
	int m, n; //Dimensionen
	vector &lt;double&gt; entry; //Einträge der m x n Matrix
public:
	int getM(){return m;};
	int getN(){return n;};
	virtual double getCoefficient(int i, int j){return entry[i*getM() + j];};
	virtual void setCoefficient(int i, int j, double coeff){entry[i*getM() + j] = coeff;};
	void print(){for(int i =0; i&lt;m; i++){for(int j = 0; j&lt;n; j++){cout&lt;&lt;&quot;[&quot;&lt;&lt;i&lt;&lt;&quot;][&quot;&lt;&lt;j&lt;&lt;&quot;]: &quot;&lt;&lt;getCoefficient(i, j)&lt;&lt;&quot; &quot;;}cout&lt;&lt;endl;}cout&lt;&lt;endl;};
	Matrix(int m, int n); //Konstruktor für Matrix m x n ohne Einträge
	Matrix(int m, int n, double init);	//Konstruktor für m x n Matrix mit init intialisiert
	virtual void init(); //komplette Matrix initialisieren
	virtual Matrix operator*(Matrix &amp;M); //Matrix Matrix Multiplikation
};
</code></pre>
<p>Von dieser Klasse habe ich jetzt die Klasse SquareMatrix abgeleitet:</p>
<pre><code class="language-cpp">class SquareMatrix : public Matrix //speichert quadratische Matrizen
{
public:
	SquareMatrix(int n, double init) : Matrix(n, n, init){}; //Konstruktor wird von Matrix übernommen
};
</code></pre>
<p>Was ich jetzt erreichen möchte, ist, Objekte vom Typ SquareMatrix mit dem * - Operator zu multiplizieren. Da der Rückgabetyp hier aber Matrix ist, funktioniert das bei mir nicht. Ich hab das jetzt vorerst so gelöst, dass ich die Klasse SquareMatrix um die Methode &quot;SquareMatrix operator*(SquareMatrix &amp;M);&quot; erweitert habe, also den Rückgabetypen angepasst.<br />
Das scheint mir aber keine besonders elegante Lösung zu sein, meine Frage daher:<br />
gibt es eine Möglichkeit, die Methode in die abgeleitete Klasse zu übernehmen, sodass sie auch von den abgeleiteten Klassen ohne spezielle Anpassung verwendet werden können?</p>
<p>Danke im Voraus<br />
Lg Monkey12345</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2225364</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2225364</guid><dc:creator><![CDATA[Monkey12345]]></dc:creator><pubDate>Wed, 20 Jun 2012 12:27:12 GMT</pubDate></item><item><title><![CDATA[Reply to Vererbung on Wed, 20 Jun 2012 12:35:10 GMT]]></title><description><![CDATA[<p>Dieser Thread wurde von Moderator/in <a href="http://www.c-plusplus.net/forum/u431" rel="nofollow">Christoph</a> aus dem Forum <a href="http://www.c-plusplus.net/forum/f43" rel="nofollow">Mathematik und Physik</a> in das Forum <a href="http://www.c-plusplus.net/forum/f15" rel="nofollow">C++ (auch C++0x und C++11)</a> verschoben.</p>
<p>Im Zweifelsfall bitte auch folgende Hinweise beachten:<br />
<a href="http://www.c-plusplus.net/forum/39405" rel="nofollow">C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?</a></p>
<p><em>Dieses Posting wurde automatisch erzeugt.</em></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2225371</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2225371</guid><dc:creator><![CDATA[C++ Forumbot]]></dc:creator><pubDate>Wed, 20 Jun 2012 12:35:10 GMT</pubDate></item><item><title><![CDATA[Reply to Vererbung on Wed, 20 Jun 2012 13:28:30 GMT]]></title><description><![CDATA[<p>Warum ueberhaupt ableiten. Einfach eine allgemeine Matrixmultiplikation implementieren und schauen ob die Dimensionen stimmen. Wenn nicht beispielsweise ein std::logic_error werfen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2225397</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2225397</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Wed, 20 Jun 2012 13:28:30 GMT</pubDate></item><item><title><![CDATA[Reply to Vererbung on Wed, 20 Jun 2012 14:16:06 GMT]]></title><description><![CDATA[<p>Monkey12345 schrieb:</p>
<blockquote>
<pre><code class="language-cpp">virtual void init(); //komplette Matrix initialisieren
	virtual Matrix operator*(Matrix &amp;M); //Matrix Matrix Multiplikation
</code></pre>
</blockquote>
<p>Aua, aua, aua. Warum tust du mir das an? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2225417</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2225417</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Wed, 20 Jun 2012 14:16:06 GMT</pubDate></item><item><title><![CDATA[Reply to Vererbung on Wed, 20 Jun 2012 14:30:54 GMT]]></title><description><![CDATA[<p>Monkey12345 schrieb:</p>
<blockquote>
<p>Ich hab das jetzt vorerst so gelöst, dass ich die Klasse SquareMatrix um die Methode &quot;SquareMatrix operator*(SquareMatrix &amp;M);&quot; erweitert habe, also den Rückgabetypen angepasst.<br />
Das scheint mir aber keine besonders elegante Lösung zu sein</p>
</blockquote>
<p>Das ist überhaupt keine Lösung, weil du auch den Parametertyp anpassen müsstest. Ich mein, was willst du mit einer Methode, die SquareMatrix*Matrix berechnen kann, das Ergebnis ist ja idR nicht quadratisch. Du willst eigentlich eine SquareMatrix*SquareMatrix -&gt; SquareMatrix, und das geht nicht (oder nicht so einfach...), weil Parametertypen in der Ableitung nicht eingeschränkt werden dürfen (Kontravarianz).</p>
<p>Mich erinnert das ein bisschen an die bekannte Diskussion, ob Kreis ein Subtyp von Ellipse ist. Siehe auch Liskov Substitution Principle.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2225427</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2225427</guid><dc:creator><![CDATA[Bashar]]></dc:creator><pubDate>Wed, 20 Jun 2012 14:30:54 GMT</pubDate></item></channel></rss>