<?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[Häufiges Konto-Beispiel, Überweisung von KontoA nach KontoB funktioniert nicht!]]></title><description><![CDATA[<p>Hallo,<br />
vielleicht kann hier jemand helfen, es geht um ein häufiges Beispiel, was man in der Literatur über OOP aufgreift, das Konto-Beispiel.</p>
<p>Leider funktioniert die Überweisung bei mir nicht, das Konto1 hat den korrekten Kontostand von 50 , allerdings verändert sich der Kontostand2 nicht (auf gewollte 5050).</p>
<pre><code class="language-cpp">#include &lt;cstdlib&gt;
#include &lt;iostream&gt;

using namespace std;

//____________________________Basisklasse Konto_________________________________
class Konto
{
      protected:
        int iKontonummer;
        float fKontostand;

      public:
        void Konto_Anzeigen();
        void Konto_Bearbeiten();
        void Konto_Loeschen();  
        void Konto_Einzahlen(float fBetrag);
        void Konto_Auszahlen(float fBetrag);
        void Konto_Ueberweisen(Konto zielkonto, float fBetrag); 
        float Konto_Kontostand();  

        Konto(int i1, float f1) {iKontonummer = i1; fKontostand = f1;}
        Konto();
};

void Konto::Konto_Anzeigen()
{
     cout &lt;&lt; &quot;Kontonummer: &quot; &lt;&lt; iKontonummer &lt;&lt; endl;
     cout &lt;&lt; &quot;Kontostand: &quot; &lt;&lt; fKontostand &lt;&lt; endl;
}

void Konto::Konto_Bearbeiten()
{
     cout &lt;&lt; &quot;Kontonummer: &quot; &lt;&lt; iKontonummer &lt;&lt; endl;
     cout &lt;&lt; &quot;Kontonummer: &quot; ;
     cin &gt;&gt; iKontonummer;
}

void Konto::Konto_Loeschen()
{
     iKontonummer =0;
     fKontostand = 0.0;
}

void Konto::Konto_Einzahlen(float fBetrag)
{
     if (fBetrag&gt;=0)
     { 
          fKontostand = fKontostand + fBetrag;                             
     }
     else;
}

void Konto::Konto_Auszahlen(float fBetrag)
{
     if (fBetrag&gt;=0)
     { 
          fKontostand = fKontostand - fBetrag;                             
     }
     else;
}

void Konto::Konto_Ueberweisen(Konto zielkonto, float fBetrag)
{
   Konto_Auszahlen(fBetrag);
   zielkonto.Konto_Einzahlen(fBetrag);
}

float Konto::Konto_Kontostand()
{
      return fKontostand;
}

int main()
{
 Konto Konto1(411553, 100);
 Konto Konto2(411552, 5000);

 cout &lt;&lt; &quot;Vor der Ueberweisung:&quot; &lt;&lt; endl;
 Konto1.Konto_Anzeigen();
 Konto2.Konto_Anzeigen();
 Konto1.Konto_Ueberweisen(Konto2, 50);

 cout &lt;&lt; endl &lt;&lt; endl&lt;&lt; &quot;Nach der Ueberweisung: (Es sollen 50 euro von Konto1 nach Konto 2 ueberwiesen werden)&quot; &lt;&lt; endl;

 Konto1.Konto_Anzeigen();
 Konto2.Konto_Anzeigen();

    system(&quot;PAUSE&quot;);
}
</code></pre>
<p>Wenn man dieMethode Überweisung um eine Ausgabe erweitert</p>
<pre><code class="language-cpp">void Konto::Konto_Ueberweisen(Konto zielkonto, float fBetrag)
{
   Konto_Auszahlen(fBetrag);
   zielkonto.Konto_Einzahlen(fBetrag);
   zielkonto.Konto_Anzeigen();
}
</code></pre>
<p>erhält man in dieser lokalen Ausgabe den richtigen Wert von 1050.<br />
Dieser verändert sich jedoch wieder <strong>(UND DAS IST DAS PROBLEM)</strong> beim Aufruf von</p>
<pre><code class="language-cpp">Konto2.Konto_Anzeigen();
</code></pre>
<p>Vielleicht hat jemand einen Tipp, wofür ich sehr dankbar wäre!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/238930/häufiges-konto-beispiel-überweisung-von-kontoa-nach-kontob-funktioniert-nicht</link><generator>RSS for Node</generator><lastBuildDate>Tue, 22 Sep 2026 15:45:17 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/238930.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 17 Apr 2009 12:03:59 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Häufiges Konto-Beispiel, Überweisung von KontoA nach KontoB funktioniert nicht! on Fri, 17 Apr 2009 12:03:59 GMT]]></title><description><![CDATA[<p>Hallo,<br />
vielleicht kann hier jemand helfen, es geht um ein häufiges Beispiel, was man in der Literatur über OOP aufgreift, das Konto-Beispiel.</p>
<p>Leider funktioniert die Überweisung bei mir nicht, das Konto1 hat den korrekten Kontostand von 50 , allerdings verändert sich der Kontostand2 nicht (auf gewollte 5050).</p>
<pre><code class="language-cpp">#include &lt;cstdlib&gt;
#include &lt;iostream&gt;

using namespace std;

//____________________________Basisklasse Konto_________________________________
class Konto
{
      protected:
        int iKontonummer;
        float fKontostand;

      public:
        void Konto_Anzeigen();
        void Konto_Bearbeiten();
        void Konto_Loeschen();  
        void Konto_Einzahlen(float fBetrag);
        void Konto_Auszahlen(float fBetrag);
        void Konto_Ueberweisen(Konto zielkonto, float fBetrag); 
        float Konto_Kontostand();  

        Konto(int i1, float f1) {iKontonummer = i1; fKontostand = f1;}
        Konto();
};

void Konto::Konto_Anzeigen()
{
     cout &lt;&lt; &quot;Kontonummer: &quot; &lt;&lt; iKontonummer &lt;&lt; endl;
     cout &lt;&lt; &quot;Kontostand: &quot; &lt;&lt; fKontostand &lt;&lt; endl;
}

void Konto::Konto_Bearbeiten()
{
     cout &lt;&lt; &quot;Kontonummer: &quot; &lt;&lt; iKontonummer &lt;&lt; endl;
     cout &lt;&lt; &quot;Kontonummer: &quot; ;
     cin &gt;&gt; iKontonummer;
}

void Konto::Konto_Loeschen()
{
     iKontonummer =0;
     fKontostand = 0.0;
}

void Konto::Konto_Einzahlen(float fBetrag)
{
     if (fBetrag&gt;=0)
     { 
          fKontostand = fKontostand + fBetrag;                             
     }
     else;
}

void Konto::Konto_Auszahlen(float fBetrag)
{
     if (fBetrag&gt;=0)
     { 
          fKontostand = fKontostand - fBetrag;                             
     }
     else;
}

void Konto::Konto_Ueberweisen(Konto zielkonto, float fBetrag)
{
   Konto_Auszahlen(fBetrag);
   zielkonto.Konto_Einzahlen(fBetrag);
}

float Konto::Konto_Kontostand()
{
      return fKontostand;
}

int main()
{
 Konto Konto1(411553, 100);
 Konto Konto2(411552, 5000);

 cout &lt;&lt; &quot;Vor der Ueberweisung:&quot; &lt;&lt; endl;
 Konto1.Konto_Anzeigen();
 Konto2.Konto_Anzeigen();
 Konto1.Konto_Ueberweisen(Konto2, 50);

 cout &lt;&lt; endl &lt;&lt; endl&lt;&lt; &quot;Nach der Ueberweisung: (Es sollen 50 euro von Konto1 nach Konto 2 ueberwiesen werden)&quot; &lt;&lt; endl;

 Konto1.Konto_Anzeigen();
 Konto2.Konto_Anzeigen();

    system(&quot;PAUSE&quot;);
}
</code></pre>
<p>Wenn man dieMethode Überweisung um eine Ausgabe erweitert</p>
<pre><code class="language-cpp">void Konto::Konto_Ueberweisen(Konto zielkonto, float fBetrag)
{
   Konto_Auszahlen(fBetrag);
   zielkonto.Konto_Einzahlen(fBetrag);
   zielkonto.Konto_Anzeigen();
}
</code></pre>
<p>erhält man in dieser lokalen Ausgabe den richtigen Wert von 1050.<br />
Dieser verändert sich jedoch wieder <strong>(UND DAS IST DAS PROBLEM)</strong> beim Aufruf von</p>
<pre><code class="language-cpp">Konto2.Konto_Anzeigen();
</code></pre>
<p>Vielleicht hat jemand einen Tipp, wofür ich sehr dankbar wäre!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1697202</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1697202</guid><dc:creator><![CDATA[brightknight]]></dc:creator><pubDate>Fri, 17 Apr 2009 12:03:59 GMT</pubDate></item><item><title><![CDATA[Reply to Häufiges Konto-Beispiel, Überweisung von KontoA nach KontoB funktioniert nicht! on Fri, 17 Apr 2009 12:12:09 GMT]]></title><description><![CDATA[<p>Du übergibst eine KOPIE des Kontos an die Methode (call-by-value). Stattdessen musst du eine Referenz auf das Objekt (Konto) übergeben (call-by-reference).</p>
<p>Die Methode muss also so aussehen:</p>
<pre><code class="language-cpp">void Konto_Ueberweisen(Konto&amp; zielkonto, float fBetrag);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1697209</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1697209</guid><dc:creator><![CDATA[_matze]]></dc:creator><pubDate>Fri, 17 Apr 2009 12:12:09 GMT</pubDate></item><item><title><![CDATA[Reply to Häufiges Konto-Beispiel, Überweisung von KontoA nach KontoB funktioniert nicht! on Fri, 17 Apr 2009 12:18:07 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">void Konto::Konto_Ueberweisen(Konto &amp;zielkonto, float fBetrag)
{
   Konto_Auszahlen(fBetrag);
   zielkonto.Konto_Einzahlen(fBetrag);
}
</code></pre>
<p>Erklärung: Das &amp; sorgt dafür dass das Zielkonto als sogenannte Referenz (Begriff nachschlagen um mehr zu lernen) übergeben wird, ansonsten wird beim Aufruf von Konto::Konto_Ueberweisen eine Kopie von zielkonto angelegt und deren Wert verändert. Beim Verlassen der Funktion wird diese veränderte Kopie zerstört und das Programm arbeitet mit dem unveränderten Original weiter. Bei der Übergabe als Referenz arbeitet die Methode mit dem Originalobjekt.</p>
<p>edit: Immer bin ich zu spät, ich muss echt mal schneller Tippen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1697213</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1697213</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Fri, 17 Apr 2009 12:18:07 GMT</pubDate></item><item><title><![CDATA[Reply to Häufiges Konto-Beispiel, Überweisung von KontoA nach KontoB funktioniert nicht! on Fri, 17 Apr 2009 12:18:28 GMT]]></title><description><![CDATA[<p>Danke, es klappt!<br />
Vielen Dank für die schnelle Hilfe!</p>
<p>Ja, hätte ich im Unterricht beim Thema Zeiger etwas besser aufpassen müssen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1697214</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1697214</guid><dc:creator><![CDATA[brightknight]]></dc:creator><pubDate>Fri, 17 Apr 2009 12:18:28 GMT</pubDate></item><item><title><![CDATA[Reply to Häufiges Konto-Beispiel, Überweisung von KontoA nach KontoB funktioniert nicht! on Fri, 17 Apr 2009 12:38:40 GMT]]></title><description><![CDATA[<p>brightknight schrieb:</p>
<blockquote>
<p>Danke, es klappt!<br />
Vielen Dank für die schnelle Hilfe!</p>
<p>Ja, hätte ich im Unterricht beim Thema Zeiger etwas besser aufpassen müssen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /></p>
</blockquote>
<p>Referenzen und Zeiger sind nochmal was anderes (wenn auch ähnlich). Am besten du liest dir das hier mal durch:</p>
<p><a href="http://www.c-plusplus.net/forum/viewtopic-var-t-is-124532-and-sid-is-020003572b5b5b7eeb85ba841efd46f4.html" rel="nofollow">http://www.c-plusplus.net/forum/viewtopic-var-t-is-124532-and-sid-is-020003572b5b5b7eeb85ba841efd46f4.html</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1697231</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1697231</guid><dc:creator><![CDATA[_matze]]></dc:creator><pubDate>Fri, 17 Apr 2009 12:38:40 GMT</pubDate></item></channel></rss>