<?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[Klausurfrage]]></title><description><![CDATA[<p>Hallo zusammen!</p>
<p>Ich lerne gerade für eine Klausur und komme bei folgendem Übungsbeispiel einfach nicht weiter. Ich soll 5 Fehler finden, beschreiben worin genau der Fehler besteht, sowie Korrekturvorschläge erbringen. Jedoch schon beim Finden der Fehler scheitere ich. Vielleicht könnt ihr mir helfen.<br />
Der Code lautet:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;
struct Point {
    Point() : x(0.0), y(0.0) {}
    Point(double px, double py) : x(px), y(py) {}
    double x;
    double y;
};

class Shape {
public:
    virtual void move(double deltax, double deltay) = 0;
    virtual void print() = 0;
};

class Polygon : public Shape {
public:
    Polygon(int vertices) {
        m_ptrVertices = new Point[vertices];
        m_nvertices = vertices;
    }
    Polygon(const Polygon&amp; from) : m_nvertices(from.m_nvertices) {
        m_ptrVertices = new Point[m_nvertices];
        for(int i = 0; i &lt; m_nvertices; ++i) {
            m_ptrVertices[i] = from.m_ptrVertices[i];
        }
    }
    Polygon&amp; operator=(const Polygon&amp; from) {
        if(&amp;from != this) {
            m_ptrVertices = new Point[m_nvertices];
            for(int i = 0; i &lt; m_nvertices; ++i) {
                m_ptrVertices[i] = from.m_ptrVertices[i];
            }
        }
    }

    ~Polygon() {delete[] m_ptrVertices;}

    void setPoint(int i, const Point&amp; point) {
        if (i &gt;=0 &amp;&amp; i&lt;m_nvertices) {
            m_ptrVertices[i] = point;
        }
    }

    void print() {
        for(int i = 0; i &lt; m_nvertices; ++i) {
        std::cout &lt;&lt; m_ptrVertices[i].x &lt;&lt; &quot;, &quot; &lt;&lt; m_ptrVertices[i].y &lt;&lt;  std::endl;
        }
        std::cout &lt;&lt; std::endl;
    }

private:
    int m_nvertices;
    Point* m_ptrVertices;
};

int main() {
    Polygon triangle(3);
    triangle.setPoint(0, Point(0,0));
    triangle.setPoint(1, Point(1,0));
    triangle.setPoint(2, Point(0.5,1));

    triangle.print();

    Polygon triangle2(triangle);
    triangle2.print();

    Shape* pshape = new Polygon(1);
    *pshape = triangle;
    pshape-&gt;print();
    delete pshape;

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/323844/klausurfrage</link><generator>RSS for Node</generator><lastBuildDate>Wed, 15 Jul 2026 13:54:49 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/323844.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 19 Feb 2014 14:21:37 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Klausurfrage on Wed, 19 Feb 2014 14:21:37 GMT]]></title><description><![CDATA[<p>Hallo zusammen!</p>
<p>Ich lerne gerade für eine Klausur und komme bei folgendem Übungsbeispiel einfach nicht weiter. Ich soll 5 Fehler finden, beschreiben worin genau der Fehler besteht, sowie Korrekturvorschläge erbringen. Jedoch schon beim Finden der Fehler scheitere ich. Vielleicht könnt ihr mir helfen.<br />
Der Code lautet:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;
struct Point {
    Point() : x(0.0), y(0.0) {}
    Point(double px, double py) : x(px), y(py) {}
    double x;
    double y;
};

class Shape {
public:
    virtual void move(double deltax, double deltay) = 0;
    virtual void print() = 0;
};

class Polygon : public Shape {
public:
    Polygon(int vertices) {
        m_ptrVertices = new Point[vertices];
        m_nvertices = vertices;
    }
    Polygon(const Polygon&amp; from) : m_nvertices(from.m_nvertices) {
        m_ptrVertices = new Point[m_nvertices];
        for(int i = 0; i &lt; m_nvertices; ++i) {
            m_ptrVertices[i] = from.m_ptrVertices[i];
        }
    }
    Polygon&amp; operator=(const Polygon&amp; from) {
        if(&amp;from != this) {
            m_ptrVertices = new Point[m_nvertices];
            for(int i = 0; i &lt; m_nvertices; ++i) {
                m_ptrVertices[i] = from.m_ptrVertices[i];
            }
        }
    }

    ~Polygon() {delete[] m_ptrVertices;}

    void setPoint(int i, const Point&amp; point) {
        if (i &gt;=0 &amp;&amp; i&lt;m_nvertices) {
            m_ptrVertices[i] = point;
        }
    }

    void print() {
        for(int i = 0; i &lt; m_nvertices; ++i) {
        std::cout &lt;&lt; m_ptrVertices[i].x &lt;&lt; &quot;, &quot; &lt;&lt; m_ptrVertices[i].y &lt;&lt;  std::endl;
        }
        std::cout &lt;&lt; std::endl;
    }

private:
    int m_nvertices;
    Point* m_ptrVertices;
};

int main() {
    Polygon triangle(3);
    triangle.setPoint(0, Point(0,0));
    triangle.setPoint(1, Point(1,0));
    triangle.setPoint(2, Point(0.5,1));

    triangle.print();

    Polygon triangle2(triangle);
    triangle2.print();

    Shape* pshape = new Polygon(1);
    *pshape = triangle;
    pshape-&gt;print();
    delete pshape;

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2384052</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384052</guid><dc:creator><![CDATA[chir00]]></dc:creator><pubDate>Wed, 19 Feb 2014 14:21:37 GMT</pubDate></item><item><title><![CDATA[Reply to Klausurfrage on Wed, 19 Feb 2014 14:31:28 GMT]]></title><description><![CDATA[<p>Welche Fehler findest du denn? Ansonsten ein Tipp: 4 von 5 haben mit der Zuweisung zu tun.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2384056</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384056</guid><dc:creator><![CDATA[Bashar]]></dc:creator><pubDate>Wed, 19 Feb 2014 14:31:28 GMT</pubDate></item><item><title><![CDATA[Reply to Klausurfrage on Wed, 19 Feb 2014 14:37:26 GMT]]></title><description><![CDATA[<p>So auf die ersten paar Blicke:</p>
<p>Falsch: Verwendung von ungarischer Notation</p>
<pre><code class="language-cpp">Point* m_ptrVertices;
</code></pre>
<p>Falsch: C-Array (richtig: std::vector)</p>
<pre><code class="language-cpp">class Shape {
public:
    virtual void move(double deltax, double deltay) = 0;
    virtual void print() = 0;
};
</code></pre>
<p>Falsch: kein virtueller Destruktor</p>
<pre><code class="language-cpp">Polygon(int vertices)
</code></pre>
<p>Falsch: nicht &quot;explicit&quot;</p>
<pre><code class="language-cpp">class Polygon : public Shape {
</code></pre>
<p>Falsch: &quot;move&quot; nicht implementiert</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2384058</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384058</guid><dc:creator><![CDATA[daddy_felix]]></dc:creator><pubDate>Wed, 19 Feb 2014 14:37:26 GMT</pubDate></item><item><title><![CDATA[Reply to Klausurfrage on Wed, 19 Feb 2014 14:44:37 GMT]]></title><description><![CDATA[<p>Fragwürdigen oder veralteten Stil würde ich nicht als Fehler im eigentlichen Sinne bezeichnen.</p>
<p>Aber immerhin sind es jetzt 6, das mit dem virtuellen Destruktor hatte ich nicht gesehen. Ist das wirklich eine Klausuraufgabe? Kommt mir sagen wir mal untypisch vor.</p>
<p>Meine Liste:<br />
move nicht implementiert, dadurch ist Polygon nicht instanziierbar<br />
Zuweisung *pshape = ... nicht möglich, weil Shape keinen Zuweisungsoperator hat<br />
Zuweisungsoperator von Polygon: hat kein return, gibt den alten Speicher nicht frei, übernimmt die Anzahl der Punkte nicht von from<br />
virtueller Destruktor fehlt</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2384062</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384062</guid><dc:creator><![CDATA[Bashar]]></dc:creator><pubDate>Wed, 19 Feb 2014 14:44:37 GMT</pubDate></item><item><title><![CDATA[Reply to Klausurfrage on Wed, 19 Feb 2014 15:26:35 GMT]]></title><description><![CDATA[<p>Bashar schrieb:</p>
<blockquote>
<p>Zuweisung *pshape = ... nicht möglich, weil Shape keinen Zuweisungsoperator hat</p>
</blockquote>
<p>Und wenn er ihn hätte, würde es zu Slicing kommen. Muss kein Fehler sein, aber i.d.R. will man bei sowas polymorphes Verhalten, schließlich wird anschließend eine rein virtuelle Methode aufgerufen (pshape-&gt;print()). Dies würde dann einen &quot;pure virtual function call&quot; ergeben, weil pshape auf ein Objekt vom Typ &quot;Shape&quot; zeigt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2384076</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384076</guid><dc:creator><![CDATA[daddy_felix]]></dc:creator><pubDate>Wed, 19 Feb 2014 15:26:35 GMT</pubDate></item><item><title><![CDATA[Reply to Klausurfrage on Wed, 19 Feb 2014 15:37:28 GMT]]></title><description><![CDATA[<p>daddy_felix schrieb:</p>
<blockquote>
<p>Bashar schrieb:</p>
<blockquote>
<p>Zuweisung *pshape = ... nicht möglich, weil Shape keinen Zuweisungsoperator hat</p>
</blockquote>
<p>Und wenn er ihn hätte, würde es zu Slicing kommen. Muss kein Fehler sein, aber i.d.R. will man bei sowas polymorphes Verhalten, schließlich wird anschließend eine rein virtuelle Methode aufgerufen (pshape-&gt;print()). Dies würde dann einen &quot;pure virtual function call&quot; ergeben, weil pshape auf ein Objekt vom Typ &quot;Shape&quot; zeigt.</p>
</blockquote>
<p>Nein, das Objekt ist vom Typ Polygon, und das wird sich durch die Zuweisung nicht ändern.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2384080</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384080</guid><dc:creator><![CDATA[Bashar]]></dc:creator><pubDate>Wed, 19 Feb 2014 15:37:28 GMT</pubDate></item><item><title><![CDATA[Reply to Klausurfrage on Wed, 19 Feb 2014 15:43:22 GMT]]></title><description><![CDATA[<p>oh, das wusste ich nicht. Aber Slicing tritt trotzdem auf, oder?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2384083</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384083</guid><dc:creator><![CDATA[daddy_felix]]></dc:creator><pubDate>Wed, 19 Feb 2014 15:43:22 GMT</pubDate></item><item><title><![CDATA[Reply to Klausurfrage on Wed, 19 Feb 2014 15:46:08 GMT]]></title><description><![CDATA[<p>Wenn der Operator nicht virtuell ist, ja. Zuweisung und Polymorphie passen allgemein nur schlecht zusammen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2384084</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384084</guid><dc:creator><![CDATA[Bashar]]></dc:creator><pubDate>Wed, 19 Feb 2014 15:46:08 GMT</pubDate></item><item><title><![CDATA[Reply to Klausurfrage on Wed, 19 Feb 2014 15:49:06 GMT]]></title><description><![CDATA[<pre><code>#include &lt;iostream&gt;
//Warnung: unnötiges include
#include &lt;vector&gt;
struct Point {
    Point() : x(0.0), y(0.0) {}
    Point(double px, double py) : x(px), y(py) {}
    double x;
    double y;
};

class Shape {
public:
    //Fehler**: virtueller Destruktor fehlt
    virtual void move(double deltax, double deltay) = 0;
    virtual void print() = 0; //Fehler: const fehlt
};

class Polygon : public Shape {
public:
    //Warnung: Standardkonstruktor fehlt

    Polygon(int vertices) {
        //Fehler: explicit fehlt
        //Fehler: int ist der falsche Typ
        //Fehler: Initialisierungsliste fehlt
        m_ptrVertices = new Point[vertices];
        m_nvertices = vertices;
    }
    Polygon(const Polygon&amp; from) : m_nvertices(from.m_nvertices) {
        //Fehler: Initialisierungsliste fehlt
        m_ptrVertices = new Point[m_nvertices];
        for(int i = 0; i &lt; m_nvertices; ++i) {
            m_ptrVertices[i] = from.m_ptrVertices[i];
        }
    }
    Polygon&amp; operator=(const Polygon&amp; from) {
        if(&amp;from != this) {
            //Fehler**: m_nvertices wird nicht kopiert
            m_ptrVertices = new Point[m_nvertices]; //Fehler: Speicherleck
            for(int i = 0; i &lt; m_nvertices; ++i) {
                m_ptrVertices[i] = from.m_ptrVertices[i];
            }
        }
        //Fehler**: return fehlt
    }

    ~Polygon() {delete[] m_ptrVertices;}

    void setPoint(int i, const Point&amp; point) {
        if (i &gt;=0 &amp;&amp; i&lt;m_nvertices) { //Fehler: Benutzungsfehler wird verschleiert
            m_ptrVertices[i] = point;
        }
    }

    void print() {
        //Warnung: der ostream kann nicht als Argument angegeben werden
        for(int i = 0; i &lt; m_nvertices; ++i) {
        std::cout &lt;&lt; m_ptrVertices[i].x &lt;&lt; &quot;, &quot; &lt;&lt; m_ptrVertices[i].y &lt;&lt;  std::endl; //Fehler: unnötiges flush
        }
        std::cout &lt;&lt; std::endl; //Warnung: unnötige Leerzeile
    }

    //Fehler**: move-Methode fehlt

private:
    int m_nvertices;
    Point* m_ptrVertices; //Fehler: ein besitzender, roher Zeiger
};

int main() {
    Polygon triangle(3);
    triangle.setPoint(0, Point(0,0));
    triangle.setPoint(1, Point(1,0));
    triangle.setPoint(2, Point(0.5,1));

    triangle.print();

    Polygon triangle2(triangle);
    triangle2.print();

    Shape* pshape = new Polygon(1); //Fehler: ein besitzender, roher Zeiger
    *pshape = triangle; //Fehler**: Zuweisung ergibt so keinen Sinn
    pshape-&gt;print(); //Fehler: Speicherleck falls das wirft
    delete pshape;

    return 0; //Warnung: unnötig
}
</code></pre>
<p>Mit <code>Fehler**</code> sind die fünf markiert, die womöglich in der Aufgabe gemeint sind.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2384085</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2384085</guid><dc:creator><![CDATA[TyRoXx]]></dc:creator><pubDate>Wed, 19 Feb 2014 15:49:06 GMT</pubDate></item></channel></rss>