<?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[Späte Bindung etc.]]></title><description><![CDATA[<p>Moin moin,</p>
<p>ich habe 3 Datenklassen (Book, CD, DVD) in denen halt das übliche stattfindet...Konstruktor, Destruktor, getter &amp; setter halt..</p>
<p>Es gibt jeweils getTitle() und getID() welche den Titel und die Ausleihnummer(ID) returned.</p>
<p>Jetzt habe ich eine Containerklasse (Library) in der ich verschiedene Objekte dieser Klassen erstelle und in einen vector&lt;Library *&gt; packe.</p>
<pre><code class="language-cpp">vector&lt;Library *&gt; myLibVec;

	Book myBook1(&quot;A Thousand Splendid Suns&quot;, 3, &quot;Khaled Hosseini&quot;, &quot;Bloomsbury Publishing&quot;, 2008);
	Book myBook2(&quot;Harry Potter and the Deathly Hallows&quot;, 4, &quot;J.K.Rowling&quot;, &quot;Bloomsbury Publishing&quot;, 2007);

	CD myCD1(&quot;A Certain Trigger&quot;, 5, &quot;Maximo Park&quot;, 14);
	CD myCD2(&quot;WE DON'T NEED TO WHISPER&quot;, 6, &quot;Angels &amp; Airwaves&quot;, 11);

	DVD myDVD1(&quot;Heat&quot;, 1, 16, 171);
	DVD myDVD2(&quot;Unknown Identity&quot;, 2, 12, 114);

	Library *myLib;

	myLib = new Library(myBook1);
	myLibVec.push_back(myLib);

	myLib = new Library(myBook2);
	myLibVec.push_back(myLib);

	myLib = new Library(myCD1);
	myLibVec.push_back(myLib);

	myLib = new Library(myCD2);
	myLibVec.push_back(myLib);

	myLib = new Library(myDVD1);
	myLibVec.push_back(myLib);

	myLib = new Library(myDVD2);
	myLibVec.push_back(myLib);	

	vector&lt;Library *&gt;::iterator it;

	for(it = myLibVec.begin(); it != myLibVec.end(); it++) {

		(*it)-&gt;outputTitle(myLibVec); // Knackpunkt

	}
</code></pre>
<p>Wenn ich die Funktion outputTitle aufrufe, sollten eigentlich alle Titel und IDs, die im vector gespeichert sind, ausgegeben werden - das klappt noch nicht^^</p>
<pre><code class="language-cpp">void Library::outputTitle(vector&lt;Library *&gt; myLibV) {

	for(int i = 0; i &lt; myLibV.size(); i++) {

		cout &lt;&lt; *myLibV.at(i) &lt;&lt; endl;

	}

}
</code></pre>
<p>ich hatte auch den Operator &lt;&lt; überladen, damit ich das wie einen built-in-Datentyp verwenden kann, nur bin ich mir nicht sicher, wie genau ich ihn überladen soll, damit mein Vorhaben gelingt...</p>
<pre><code class="language-cpp">ostream&amp; operator &lt;&lt; (std::ostream&amp; outStream, const Library&amp; library) {

	outStream &lt;&lt; &quot;// Hier Inhalt rein^^&quot; &lt;&lt; endl;

	return outStream;

}
</code></pre>
<p>Kann mir da jemand weiterhelfen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/305417/späte-bindung-etc</link><generator>RSS for Node</generator><lastBuildDate>Sat, 08 Aug 2026 22:12:47 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/305417.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 28 Jun 2012 12:33:13 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Späte Bindung etc. on Thu, 28 Jun 2012 12:33:13 GMT]]></title><description><![CDATA[<p>Moin moin,</p>
<p>ich habe 3 Datenklassen (Book, CD, DVD) in denen halt das übliche stattfindet...Konstruktor, Destruktor, getter &amp; setter halt..</p>
<p>Es gibt jeweils getTitle() und getID() welche den Titel und die Ausleihnummer(ID) returned.</p>
<p>Jetzt habe ich eine Containerklasse (Library) in der ich verschiedene Objekte dieser Klassen erstelle und in einen vector&lt;Library *&gt; packe.</p>
<pre><code class="language-cpp">vector&lt;Library *&gt; myLibVec;

	Book myBook1(&quot;A Thousand Splendid Suns&quot;, 3, &quot;Khaled Hosseini&quot;, &quot;Bloomsbury Publishing&quot;, 2008);
	Book myBook2(&quot;Harry Potter and the Deathly Hallows&quot;, 4, &quot;J.K.Rowling&quot;, &quot;Bloomsbury Publishing&quot;, 2007);

	CD myCD1(&quot;A Certain Trigger&quot;, 5, &quot;Maximo Park&quot;, 14);
	CD myCD2(&quot;WE DON'T NEED TO WHISPER&quot;, 6, &quot;Angels &amp; Airwaves&quot;, 11);

	DVD myDVD1(&quot;Heat&quot;, 1, 16, 171);
	DVD myDVD2(&quot;Unknown Identity&quot;, 2, 12, 114);

	Library *myLib;

	myLib = new Library(myBook1);
	myLibVec.push_back(myLib);

	myLib = new Library(myBook2);
	myLibVec.push_back(myLib);

	myLib = new Library(myCD1);
	myLibVec.push_back(myLib);

	myLib = new Library(myCD2);
	myLibVec.push_back(myLib);

	myLib = new Library(myDVD1);
	myLibVec.push_back(myLib);

	myLib = new Library(myDVD2);
	myLibVec.push_back(myLib);	

	vector&lt;Library *&gt;::iterator it;

	for(it = myLibVec.begin(); it != myLibVec.end(); it++) {

		(*it)-&gt;outputTitle(myLibVec); // Knackpunkt

	}
</code></pre>
<p>Wenn ich die Funktion outputTitle aufrufe, sollten eigentlich alle Titel und IDs, die im vector gespeichert sind, ausgegeben werden - das klappt noch nicht^^</p>
<pre><code class="language-cpp">void Library::outputTitle(vector&lt;Library *&gt; myLibV) {

	for(int i = 0; i &lt; myLibV.size(); i++) {

		cout &lt;&lt; *myLibV.at(i) &lt;&lt; endl;

	}

}
</code></pre>
<p>ich hatte auch den Operator &lt;&lt; überladen, damit ich das wie einen built-in-Datentyp verwenden kann, nur bin ich mir nicht sicher, wie genau ich ihn überladen soll, damit mein Vorhaben gelingt...</p>
<pre><code class="language-cpp">ostream&amp; operator &lt;&lt; (std::ostream&amp; outStream, const Library&amp; library) {

	outStream &lt;&lt; &quot;// Hier Inhalt rein^^&quot; &lt;&lt; endl;

	return outStream;

}
</code></pre>
<p>Kann mir da jemand weiterhelfen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2228136</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2228136</guid><dc:creator><![CDATA[Zel2491]]></dc:creator><pubDate>Thu, 28 Jun 2012 12:33:13 GMT</pubDate></item><item><title><![CDATA[Reply to Späte Bindung etc. on Thu, 28 Jun 2012 12:37:05 GMT]]></title><description><![CDATA[<p>Schwer, da nicht klar ist, was du eigentlich willst. Du iterierst über alle Libraries, gibst dann der Methode Library::outputTitle die Libraries mit, über die Du gerade iterierst, und iterierst in der Methode nochmal über die Libraries? Sinn?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2228139</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2228139</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Thu, 28 Jun 2012 12:37:05 GMT</pubDate></item><item><title><![CDATA[Reply to Späte Bindung etc. on Thu, 28 Jun 2012 13:00:15 GMT]]></title><description><![CDATA[<p>Stimmt, die eine for-Schleife ist sinnlos:</p>
<pre><code class="language-cpp">void Library::outputTitle(vector&lt;Library *&gt; myLibV) {

        cout &lt;&lt; *myLibV &lt;&lt; endl;

}
</code></pre>
<p>Ich übergebe den Vector, den ich iteriere, weil ich sonst nicht auf den Vector zugreifen kann in der outputTitle Funktion - wenns da eine andere Lösung gibt, sag ich nicht nein</p>
<p>Nochmal:</p>
<p>Ich habe Objekte von meinen verschiedenen Datenklassen, die ich in meinen Vector packe, allerdings als Datentyp meiner Klasse Library.</p>
<p>Jetzt möchte ich durch den vector&lt;Library&gt; iterieren und alle Titel und IDs, die dabei gefunden werden, ausgeben - das erweist sich als problematisch, da ich nicht mehr auf meine Funktion getTitle() etc. von den 3 Datenklassen zugreifen kann.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2228143</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2228143</guid><dc:creator><![CDATA[Zel2491]]></dc:creator><pubDate>Thu, 28 Jun 2012 13:00:15 GMT</pubDate></item><item><title><![CDATA[Reply to Späte Bindung etc. on Thu, 28 Jun 2012 13:15:26 GMT]]></title><description><![CDATA[<p>Irgendwie hast du alles genau falsch rum. Ich schlage vor: CD, Buch und DVD erben von einer gemeinsamen Basisklasse (ich nenne sie mal Medium), die die Methoden get_title und get_id zur Verfügung stellt. Diese speicherst du dann in deiner Bibliothek. Eventuell auch nur Verweise auf die richtigen Objekte, dann kannst du auch Polymorphie benutzen, falls später noch nötig (hier brauchst du noch keine Polymorphie, aber die Aufgabenstellung klingt so, als solle dies später kommen). Dann kannst du durch deine Liste iterieren und bekommst die passenden Titel und IDs.</p>
<p>Noch ein paar Anmerkungen:</p>
<blockquote>
<p>ich habe 3 Datenklassen (Book, CD, DVD) in denen halt das übliche stattfindet...Konstruktor, Destruktor, getter &amp; setter halt..</p>
</blockquote>
<p>Destruktor ist bei Datenklassen eigentlich nicht üblich. getter, setter nur, sofern es wirklich reine Datenklassen sind. Meistens erwartet man von seinen Klassen, dass sie auch was tun.</p>
<blockquote>
<pre><code class="language-cpp">Library *myLib;

    myLib = new Library(myBook1);
    myLibVec.push_back(myLib);
</code></pre>
</blockquote>
<p>1. Warum immer über die Zwischenstation myLib?<br />
2. Warum überhaupt new?<br />
3. Warum überhaupt Zeiger?</p>
<p>Dein Code sieht ein bisschen so aus, als wolltest du Polymorphie versuchen, hattest es aber nicht verstanden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2228151</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2228151</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 28 Jun 2012 13:15:26 GMT</pubDate></item><item><title><![CDATA[Reply to Späte Bindung etc. on Thu, 28 Jun 2012 13:16:02 GMT]]></title><description><![CDATA[<p>Hallo.</p>
<p>Das macht man auch ne Spur anders <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<pre><code class="language-cpp">class Entity {
public:
	virtual ~Entity() {}

	virtual std::string const&amp; getTitle() const=0;
	virtual unsigned getID() const=0;
};

class Book : public Entity {
private
	std::string title;
	unsigned id;

public:
	Book(std::string const&amp; title, unsigned id) : title(title), id(id)
	{}

	std::string getTitle() const { return title; }
	unsigned getID() const { return id; }
};

class Movie : public Entity {
private
	std::string title;
	unsigned id;

public:
	Movie(std::string const&amp; title, unsigned id) : title(title), id(id)
	{}

	std::string getTitle() const { return title; }
	unsigned getID() const { return id; }
};

std::vector&lt;Entity*&gt; library;
//besser wäre: boost::ptr_vector

library.push_back(new Book(...));

for(it=library.begin(); it!=library.end(); ++it) {
   std::cout&lt;&lt; (*it)-&gt;getTitle() &lt;&lt; &quot;\n&quot;;
}
</code></pre>
<p>title und id könnten natürlich direkt von Entity verwaltet werden. Jedenfalls bietet Entity die Schnittstelle an die brauchst um mit Books und Movies umgehen zu können.</p>
<p>Deine Klasse Library ist unnötig und falsch.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2228152</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2228152</guid><dc:creator><![CDATA[Shade Of Mine]]></dc:creator><pubDate>Thu, 28 Jun 2012 13:16:02 GMT</pubDate></item><item><title><![CDATA[Reply to Späte Bindung etc. on Thu, 28 Jun 2012 15:08:57 GMT]]></title><description><![CDATA[<p>Vielen Dank für die Hilfe - hat mir bis hierhin schon viel gebracht <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>Nur hab ich jetzt ein Problem (wahrscheinlich was ganz einfaches -.-)</p>
<p>meine Library.h sieht jetzt so aus:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;string&gt;

using std::ostream;

using namespace std;

#pragma once

class Library {

public:

	Library();
	virtual ~Library();

	virtual string const&amp; getTitle() const;
        virtual double getID() const; 

	void add();
	int getCount(vector&lt;Library *&gt;) const;
	void outputTitle(vector&lt;Library *&gt;);
	void searchOutput(const string &amp;, vector&lt;Library *&gt;);

};

class Book : public Library {

private:
	string title;
	double ID;
	string author;
	string publisher;
	int yearOfPublication;

public:

	Book();
	Book(string, double, string, string, int);
	~Book();

	string getTitle() const; // Hier Fehlermeldung -&gt; siehe Unten
	double getID() const;
	string getAuthor() const;
	string getPublisher() const;
	int getYearOfPublication() const;

	void setTitle(string);
	void setID(double);
	void setAuthor(string);
	void setPublisher(string);
	void setYearOfPublication(int);

};

class DVD : public Library {

private:

	string title;
	double ID;
	int fsk;
	int duration;

public:

	DVD();
	DVD(string, double, int, int);
	virtual ~DVD();

	string getTitle() const;
	double getID() const;
	int getFsk() const;
	int getDuration() const;

	void setTitle(string);
	void setID(double);
	void setFsk(int);
	void setDuration(int);

};

class CD : public Library {

private:

	string title;
	double ID;
	string interpreter;
	int numberOfTitles;

public:

	CD();
	CD(string, double, string, int);
	virtual ~CD();

	string getTitle() const;
	double getID() const;
	string getInterpreter() const;
	int getNumberOfTitles() const;

	void setTitle(string);
	void setID(double);
	void setInterpreter(string);
	void setNumberOfTitles(int);

};

	ostream&amp; operator &lt;&lt; (std::ostream&amp; outStream, const CD&amp; CD);

	ostream&amp; operator &lt;&lt; (std::ostream&amp; outStream, const Book&amp; Book);

	ostream&amp; operator &lt;&lt; (std::ostream&amp; outStream, const DVD&amp; DVD);

	ostream&amp; operator &lt;&lt; (std::ostream&amp; outStream, const Library&amp; Library);

	void showLibrary();
</code></pre>
<p>In der Klasse Book beim getter für den Titel meckert er jetzt rum (hab die Stelle im Code mit nem Kommentar markiert) - hier mal der Text, der mir gezeigt wird:<br />
&quot;<br />
std::string Book::getTitle() const</p>
<p>Error: Der Rückgabetyp ist nicht identisch mit dem Rückgabetyp &quot;&quot;const std::string&amp;&quot;&quot; der überschriebenen virtuellen Funktion &quot;Funktion &quot;Library::getTitle&quot;&quot; oder ein Covariant-Rückgabetyp davon.&quot;</p>
<p>Was soll mir das sagen?^^</p>
<p>Edit:</p>
<p>Wieso mach ich nicht einfach:</p>
<pre><code class="language-cpp">string const&amp; getTitle() const;
</code></pre>
<p>... sorry für den dummen Post^^</p>
<p>Bzw. eine Frage hab ich doch noch:</p>
<p>ich hab ja jetzt die beiden</p>
<pre><code class="language-cpp">virtual string const&amp; getTitle() const;
virtual double getID() const;
</code></pre>
<p>was schreib ich dann in der .cpp Datei?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2228186</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2228186</guid><dc:creator><![CDATA[Zel2491]]></dc:creator><pubDate>Thu, 28 Jun 2012 15:08:57 GMT</pubDate></item><item><title><![CDATA[Reply to Späte Bindung etc. on Thu, 28 Jun 2012 15:53:11 GMT]]></title><description><![CDATA[<p>Hey,</p>
<p>die Signatur in der abgeleiteten Klasse hat sich geändert (von 'string const&amp; getTitle() const' zu 'string getTitle() const', also das erste const).</p>
<ol start="2">
<li>Was meinst du? Das?</li>
</ol>
<pre><code class="language-cpp">string const&amp; Book::getTitle() const
{
...
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2228196</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2228196</guid><dc:creator><![CDATA[Jockelx]]></dc:creator><pubDate>Thu, 28 Jun 2012 15:53:11 GMT</pubDate></item></channel></rss>