Vererbung - Konstruktor
-
Hallo,
bei dem folgendem Quelltext sollen zwei Klassen deklariert werden. Kreis ist eine abgeleitete Klasse von punkt.
Wenn ich den Quelltext compiliere kommt die Fehlermeldung "cannot generate default constructor to initialize 'punkt' since constructors were declared".
Kann damit jemand was anfangen.#include<iostream.h>
class punkt
{
protected:
float x, y;public:
punkt (float m, float n);
};punkt::punkt (float m, float n)
{x = m; y = n;}class kreis: public punkt
{
protected:
float radius;
punkt mitte;public:
kreis(float m, float n, float r) : punkt(m,n) {mitte(m,n);radius=r;}
};void main ()
{
punkt p1(2.3, 3.6);
kreis k1(2.3, 3.6, 3.2);}
-
class punkt { protected: float x, y; public: punkt (float m, float n) : x(m), y(n) { } }; class kreis: public punkt { protected: float radius; public: kreis(float m, float n, float r) : punkt(m,n), radius(r) { } }; int main () { punkt p1(2.3f, 3.6f); kreis k1(2.3f, 3.6f, 3.2f); }Muss leider weg keine Zeit dir das Modell der OOP noch zu erklären... IMO gibt die ableitung keinen Sinn. Aber entweder Member, oder Vererbung.
-
Vielen Dank für die schnelle Hilfe.
Könnte ich den Konstruktor auch so formulieren:class kreis: public punkt { protected: float radius; punkt mitte; public: kreis(float m, float n, float r) : mitte(m,n), radius(r) { } };
-
Warum willst du das unbedingt?? Wenn du Kreis von Punkt ableitest hat es ja schon die floats x/y du brauchst net noch nen Punkt...