<?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[Konstruktor &amp;amp; Vererbung]]></title><description><![CDATA[<p>Hi,<br />
ich hab ein Problem mit meinem C++ Programm und komme gerade gar nicht weiter.<br />
Kann mir da jemand bitte helfen ich bin grade echt am verzweifeln <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>
<p>Das sind meine Erros:</p>
<p>sparschwein.h(6): error C2011: 'Sparschwein': 'class' Typneudefinition<br />
sparschwein.h(6): Siehe Deklaration von 'Sparschwein'<br />
buntessparschwein.h(8): error C2504: 'Sparschwein': Basisklasse undefiniert<br />
quelle.cpp(12): error C2079: 's1' verwendet undefiniertes class 'Sparschwein'<br />
quelle.cpp(17): error C2228: Links von &quot;.einwerfen&quot; muss sich eine Klasse/Struktur/Union befinden.<br />
quelle.cpp(18): error C2228: Links von &quot;.printGuthaben&quot; muss sich eine Klasse/Struktur/Union befinden.<br />
buntessparschwein.h(16): error C2512: 'Sparschwein': Kein geeigneter Standardkonstruktor verfügbar</p>
<pre><code>#include &lt;iostream&gt;
#include &quot;Sparschwein.h&quot;
#include &quot;BuntesSparschwein.h&quot;
using namespace std;

int main() 
{ 

	Sparschwein s1(1.0f); 
	BuntesSparschwein s2(2.0f, 35 ,35 ,35) ; 
	//SprechendesSparschwein s3(3.0f, &quot; May the force be with you &quot;); 

	cout &lt;&lt; &quot; ---- Schwein 1 ----&quot; &lt;&lt; endl ; 
	s1.einwerfen(5.0f); 
	s1.printGuthaben(); 
	cout &lt;&lt; &quot; ---- Schwein 2 ----&quot; &lt;&lt; endl ; 
	s2.printColor(); 
	cout &lt;&lt; &quot; ---- Schwein 3 ----&quot; &lt;&lt; endl ; 
	//s3. einwerfen (10.0f); 
	//s3. sprich (); 

	/* 
	Sparschwein s4; 

	cout &lt;&lt; &quot;---- Schweineaddition ----&quot; &lt;&lt; endl ; 
	s4 = s1 + s2; 
	s1. printGuthaben (); 
	s2. printGuthaben (); 
	s4. printGuthaben (); 
	*/ 

	system (&quot; PAUSE &quot;); 
	return 0; 
}
</code></pre>
<p>Sparschwein.cpp</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt; 
#include &quot;Sparschwein.h&quot;
using namespace std; 

//Einwerffunktion Sparschwein
int Sparschwein::einwerfen(float in)
{ 
        guthaben += in; 
	cout &lt;&lt; &quot;Du hast &quot; &lt;&lt; guthaben&lt;&lt; &quot; Euro in dein Sparschwein geworfen&quot; &lt;&lt;endl; 
	return 0; 
}; 

//Sparschwein Guthaben Ausgabe
int Sparschwein::printGuthaben()
{ 
	cout &lt;&lt; &quot;In deinem Sparschwein sind nun &quot;&lt;&lt; guthaben &lt;&lt; &quot; Euro&quot; &lt;&lt;endl; 
        return 0; 
};

Sparschwein::~Sparschwein() 
{ 
}
</code></pre>
<p>Sparschwein.h</p>
<pre><code>#include &lt;iostream&gt; 
using namespace std; 
#include &lt;string&gt; 

class Sparschwein
{ 
protected: 
	float guthaben;

public: 
Sparschwein (float guthaben)
 : guthaben(guthaben)
{ 
} 
	~Sparschwein(); 
	int einwerfen(float in); 
	int printGuthaben(); 

};
</code></pre>
<p>Sparschwein.cpp</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt; 
#include &quot;BuntesSparschwein.h&quot;
using namespace std; 

int BuntesSparschwein::printColor()
{ 
	cout &lt;&lt; r &lt;&lt; g &lt;&lt; b &lt;&lt;endl; 

	return 0; 
};
</code></pre>
<p>BuntesSparschwein.h</p>
<pre><code>#include &lt;iostream&gt; 
using namespace std; 
#include &lt;string&gt; 
#include &quot;Sparschwein.h&quot;

class BuntesSparschwein : public Sparschwein
{ 
protected: 
	float guthaben; 

public: 
	int r,g,b;
	BuntesSparschwein (float guthaben, int r, int g, int b)
	: guthaben(g), r(r), g(g), b(b)
{ 
}
	int printColor(); 
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/333438/konstruktor-amp-vererbung</link><generator>RSS for Node</generator><lastBuildDate>Sun, 26 Apr 2026 21:17:25 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/333438.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 06 Jul 2015 16:04:13 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Konstruktor &amp;amp; Vererbung on Mon, 06 Jul 2015 16:04:13 GMT]]></title><description><![CDATA[<p>Hi,<br />
ich hab ein Problem mit meinem C++ Programm und komme gerade gar nicht weiter.<br />
Kann mir da jemand bitte helfen ich bin grade echt am verzweifeln <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>
<p>Das sind meine Erros:</p>
<p>sparschwein.h(6): error C2011: 'Sparschwein': 'class' Typneudefinition<br />
sparschwein.h(6): Siehe Deklaration von 'Sparschwein'<br />
buntessparschwein.h(8): error C2504: 'Sparschwein': Basisklasse undefiniert<br />
quelle.cpp(12): error C2079: 's1' verwendet undefiniertes class 'Sparschwein'<br />
quelle.cpp(17): error C2228: Links von &quot;.einwerfen&quot; muss sich eine Klasse/Struktur/Union befinden.<br />
quelle.cpp(18): error C2228: Links von &quot;.printGuthaben&quot; muss sich eine Klasse/Struktur/Union befinden.<br />
buntessparschwein.h(16): error C2512: 'Sparschwein': Kein geeigneter Standardkonstruktor verfügbar</p>
<pre><code>#include &lt;iostream&gt;
#include &quot;Sparschwein.h&quot;
#include &quot;BuntesSparschwein.h&quot;
using namespace std;

int main() 
{ 

	Sparschwein s1(1.0f); 
	BuntesSparschwein s2(2.0f, 35 ,35 ,35) ; 
	//SprechendesSparschwein s3(3.0f, &quot; May the force be with you &quot;); 

	cout &lt;&lt; &quot; ---- Schwein 1 ----&quot; &lt;&lt; endl ; 
	s1.einwerfen(5.0f); 
	s1.printGuthaben(); 
	cout &lt;&lt; &quot; ---- Schwein 2 ----&quot; &lt;&lt; endl ; 
	s2.printColor(); 
	cout &lt;&lt; &quot; ---- Schwein 3 ----&quot; &lt;&lt; endl ; 
	//s3. einwerfen (10.0f); 
	//s3. sprich (); 

	/* 
	Sparschwein s4; 

	cout &lt;&lt; &quot;---- Schweineaddition ----&quot; &lt;&lt; endl ; 
	s4 = s1 + s2; 
	s1. printGuthaben (); 
	s2. printGuthaben (); 
	s4. printGuthaben (); 
	*/ 

	system (&quot; PAUSE &quot;); 
	return 0; 
}
</code></pre>
<p>Sparschwein.cpp</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt; 
#include &quot;Sparschwein.h&quot;
using namespace std; 

//Einwerffunktion Sparschwein
int Sparschwein::einwerfen(float in)
{ 
        guthaben += in; 
	cout &lt;&lt; &quot;Du hast &quot; &lt;&lt; guthaben&lt;&lt; &quot; Euro in dein Sparschwein geworfen&quot; &lt;&lt;endl; 
	return 0; 
}; 

//Sparschwein Guthaben Ausgabe
int Sparschwein::printGuthaben()
{ 
	cout &lt;&lt; &quot;In deinem Sparschwein sind nun &quot;&lt;&lt; guthaben &lt;&lt; &quot; Euro&quot; &lt;&lt;endl; 
        return 0; 
};

Sparschwein::~Sparschwein() 
{ 
}
</code></pre>
<p>Sparschwein.h</p>
<pre><code>#include &lt;iostream&gt; 
using namespace std; 
#include &lt;string&gt; 

class Sparschwein
{ 
protected: 
	float guthaben;

public: 
Sparschwein (float guthaben)
 : guthaben(guthaben)
{ 
} 
	~Sparschwein(); 
	int einwerfen(float in); 
	int printGuthaben(); 

};
</code></pre>
<p>Sparschwein.cpp</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt; 
#include &quot;BuntesSparschwein.h&quot;
using namespace std; 

int BuntesSparschwein::printColor()
{ 
	cout &lt;&lt; r &lt;&lt; g &lt;&lt; b &lt;&lt;endl; 

	return 0; 
};
</code></pre>
<p>BuntesSparschwein.h</p>
<pre><code>#include &lt;iostream&gt; 
using namespace std; 
#include &lt;string&gt; 
#include &quot;Sparschwein.h&quot;

class BuntesSparschwein : public Sparschwein
{ 
protected: 
	float guthaben; 

public: 
	int r,g,b;
	BuntesSparschwein (float guthaben, int r, int g, int b)
	: guthaben(g), r(r), g(g), b(b)
{ 
}
	int printColor(); 
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2458859</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2458859</guid><dc:creator><![CDATA[Matthew_the_1st]]></dc:creator><pubDate>Mon, 06 Jul 2015 16:04:13 GMT</pubDate></item><item><title><![CDATA[Reply to Konstruktor &amp;amp; Vererbung on Mon, 06 Jul 2015 16:13:28 GMT]]></title><description><![CDATA[<p>Du benutzt keine Include Guards daher wird deine Sparschwein Klasse zweimal definiert. Einmal durch das <code>#include &quot;Sparschwein.h&quot;</code> in der quelle.cpp und einmal über die BuntesSparschwein.h wo noch ein Include steht. Du musst ein <code>#pragma once</code> an den Anfang deiner Header Dateien packen (ja ich weiß ist nicht Standard, kann aber effektiv jeder vernünftige Compiler). Oder wenn du Standardkonform bleiben möchtest müssen die Header ein</p>
<pre><code>#ifndef MY_HEADER_HPP
#define MY_HEADER_HPP
...
#endif
</code></pre>
<p>Konstruk kriegen. Google am Besten mal nach Include Guard.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2458861</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2458861</guid><dc:creator><![CDATA[sebi707]]></dc:creator><pubDate>Mon, 06 Jul 2015 16:13:28 GMT</pubDate></item><item><title><![CDATA[Reply to Konstruktor &amp;amp; Vererbung on Mon, 06 Jul 2015 16:22:46 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>als erstes solltest du dir <a href="https://de.wikipedia.org/wiki/Include-Guard" rel="nofollow">Include-Guards</a> anschauen.</p>
<blockquote>
<p>buntessparschwein.h(16): error C2512: 'Sparschwein': Kein geeigneter Standardkonstruktor verfügbar</p>
</blockquote>
<p>Der Fehler sagt eigentlich klar was los ist:<br />
Um ein BuntesSparschwein zu erzeugen, muss zunächst der Konstruktor der Basisklasse Sparschwein aufgerufen werden.<br />
Wenn du dem Kompiler in der Initialisierungsliste nichts sagst, nimmt er den Standardkonstruktor (den es nicht (mehr -wegen eigenem Konstruktor) gibt).</p>
<p>Unabhängig von den Fehlern noch mal googeln, warum float für 'Geld' schlecht ist und warum using namespace in Headern nicht gut ist und warum Destruktoren in Basisklassen virtual oder protected sein sollten.</p>
<p>ps: guthaben in Sparschwein und Buntessparschwein willst du mit Sicherheit nicht haben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2458862</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2458862</guid><dc:creator><![CDATA[Jockelx]]></dc:creator><pubDate>Mon, 06 Jul 2015 16:22:46 GMT</pubDate></item><item><title><![CDATA[Reply to Konstruktor &amp;amp; Vererbung on Mon, 06 Jul 2015 17:57:59 GMT]]></title><description><![CDATA[<p>Super danke <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<p>jetzt habe ich nur folgendes Problem:</p>
<p>Er nimmt jetzt immer den Standardkonstruktor. Ich möchte aber einen Wert übergeben.</p>
<p>Sparschwein.h</p>
<pre><code>...
class BuntesSparschwein : public Sparschwein
{ 
protected: 
	float guthaben; 

public: 
	int r,g,b;
	BuntesSparschwein (float gut, int x, int y, int z)
	{
		guthaben=gut;
		r=x;
		g=y;
		b=z;
	}
...
</code></pre>
<p>Sparschwein.h</p>
<pre><code>...
class Sparschwein
{ 
protected: 
	float guthaben;

public: 
	Sparschwein()
	{
		guthaben=0;
	}
	Sparschwein (float gut)
	{
		guthaben=gut;
	}
	~Sparschwein(); 
	int einwerfen(float in); 
	int printGuthaben(); 

};
</code></pre>
<p>und Quelle.cpp</p>
<pre><code>...
BuntesSparschwein s2(1.0f, 35 ,35 ,35) ;
...
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2458868</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2458868</guid><dc:creator><![CDATA[Matthew_the_1st]]></dc:creator><pubDate>Mon, 06 Jul 2015 17:57:59 GMT</pubDate></item><item><title><![CDATA[Reply to Konstruktor &amp;amp; Vererbung on Mon, 06 Jul 2015 18:27:28 GMT]]></title><description><![CDATA[<p>Welchen Standarkonstrutor nimmt &quot;er&quot;?<br />
Woher weisst du das?<br />
Dir ist schon klar, dass du in Buntes... zwei verschiedene Guthaben hast?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2458878</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2458878</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Mon, 06 Jul 2015 18:27:28 GMT</pubDate></item><item><title><![CDATA[Reply to Konstruktor &amp;amp; Vererbung on Wed, 08 Jul 2015 09:59:55 GMT]]></title><description><![CDATA[<p>Du musst einfach wie Jockelx es bereits gesagt hat, den Konstruktor der Basisklasse in der Initialisierungliste deines abgeleiteten Konstruktors aufrufen. Dann kannst du dort auswählen, welcher Konstruktor aus der Basisklasse tatsächlich benutzt wird, da ansonsten einfach der Default-Konstruktor benutzt wird.<br />
Google mal nach &quot;Initialisierungsliste Konstruktor&quot;.</p>
<p>Für dein Beispiel könnte es etwas so aussehen:</p>
<pre><code>BuntesSparschwein (float gut, int x, int y, int z) : Sparschwein(gut)
    {
        guthaben=gut;
        r=x;
        g=y;
        b=z;
    }
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2459068</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2459068</guid><dc:creator><![CDATA[Repeater]]></dc:creator><pubDate>Wed, 08 Jul 2015 09:59:55 GMT</pubDate></item><item><title><![CDATA[Reply to Konstruktor &amp;amp; Vererbung on Wed, 08 Jul 2015 10:03:09 GMT]]></title><description><![CDATA[<p>Repeater schrieb:</p>
<blockquote>
<p>Google mal nach &quot;Initialisierungsliste Konstruktor&quot;.</p>
<p>Für dein Beispiel könnte es etwas so aussehen:</p>
<pre><code>BuntesSparschwein (float gut, int x, int y, int z) : Sparschwein(gut)
    {
        guthaben=gut;
        r=x;
        g=y;
        b=z;
    }
</code></pre>
</blockquote>
<p>Wenn shcon Initialisierungsliste, dann bitte auch konsequent!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2459069</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2459069</guid><dc:creator><![CDATA[daddy_felix]]></dc:creator><pubDate>Wed, 08 Jul 2015 10:03:09 GMT</pubDate></item></channel></rss>