<?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[Warum kann ich hier kein const verwenden?]]></title><description><![CDATA[<p>Hallo,<br />
ich habe ein Problem mit einem Programm, das Alter, Dienstjahre und Verdienst von 2 Angestellten ausgeben soll. Dazu soll eine Klasse &quot;Employee&quot; verwendet werden.</p>
<p>Dies ist mein Programm:</p>
<pre><code class="language-cpp">#include&lt;iostream&gt;
using namespace std;

class Employee
{
      private:
             int age;
             int years_of_service;
             int salary;
      public:
             void set_age (int a);
             int get_age() const;
             void set_years_of_service (int y_o_s);
             int get_years_of_service() const;
             void set_salary (int s);
             int get_salary() const;
};

void Employee:: set_age (int a)
{
     age=a;
}

int Employee::get_age()
{
     return age;
}

void Employee::set_years_of_service (int y_o_s)
{
     years_of_service=y_o_s;
}

int Employee::get_years_of_service()
{
     return years_of_service;
}

void Employee::set_salary (int s)
{
     salary=s;
}

int Employee::get_salary()
{
     return salary;
}

int main()
{
    Employee heini;
    heini.set_age(20);
    heini.set_years_of_service(2);
    heini.set_salary(5000);

    Employee fritz;
    fritz.set_age(50);
    fritz.set_years_of_service(10);
    fritz.set_salary(10000);

    cout&lt;&lt;&quot;Heini:\n&quot;;
    cout&lt;&lt;&quot;Alter: &quot;&lt;&lt;heini.get_age()&lt;&lt;&quot;\n&quot;;
    cout&lt;&lt;&quot;Dienstjahre: &quot;&lt;&lt;heini.get_years_of_service()&lt;&lt;&quot;\n&quot;;
    cout&lt;&lt;&quot;Verdienst: &quot;&lt;&lt;heini.get_salary()&lt;&lt;&quot;\n&quot;;

    cout&lt;&lt;&quot;Fritz:\n&quot;;
    cout&lt;&lt;&quot;Alter: &quot;&lt;&lt;fritz.get_age()&lt;&lt;&quot;\n&quot;;
    cout&lt;&lt;&quot;Dienstjahre: &quot;&lt;&lt;fritz.get_years_of_service()&lt;&lt;&quot;\n&quot;;
    cout&lt;&lt;&quot;Verdienst: &quot;&lt;&lt;fritz.get_salary()&lt;&lt;&quot;\n&quot;;

    cin.get();
}
</code></pre>
<p>Leider funktioniert es so nicht, erst wenn ich &quot;const&quot; in Zeile 12, 14 und 16 entferne. Soweit ich es sehe, verändern die entsprechenden Funktionen jedoch keine Variablen. Warum kann ich dann kein const verwenden?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/274101/warum-kann-ich-hier-kein-const-verwenden</link><generator>RSS for Node</generator><lastBuildDate>Thu, 27 Aug 2026 20:38:23 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/274101.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 19 Sep 2010 19:18:48 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Warum kann ich hier kein const verwenden? on Sun, 19 Sep 2010 19:18:48 GMT]]></title><description><![CDATA[<p>Hallo,<br />
ich habe ein Problem mit einem Programm, das Alter, Dienstjahre und Verdienst von 2 Angestellten ausgeben soll. Dazu soll eine Klasse &quot;Employee&quot; verwendet werden.</p>
<p>Dies ist mein Programm:</p>
<pre><code class="language-cpp">#include&lt;iostream&gt;
using namespace std;

class Employee
{
      private:
             int age;
             int years_of_service;
             int salary;
      public:
             void set_age (int a);
             int get_age() const;
             void set_years_of_service (int y_o_s);
             int get_years_of_service() const;
             void set_salary (int s);
             int get_salary() const;
};

void Employee:: set_age (int a)
{
     age=a;
}

int Employee::get_age()
{
     return age;
}

void Employee::set_years_of_service (int y_o_s)
{
     years_of_service=y_o_s;
}

int Employee::get_years_of_service()
{
     return years_of_service;
}

void Employee::set_salary (int s)
{
     salary=s;
}

int Employee::get_salary()
{
     return salary;
}

int main()
{
    Employee heini;
    heini.set_age(20);
    heini.set_years_of_service(2);
    heini.set_salary(5000);

    Employee fritz;
    fritz.set_age(50);
    fritz.set_years_of_service(10);
    fritz.set_salary(10000);

    cout&lt;&lt;&quot;Heini:\n&quot;;
    cout&lt;&lt;&quot;Alter: &quot;&lt;&lt;heini.get_age()&lt;&lt;&quot;\n&quot;;
    cout&lt;&lt;&quot;Dienstjahre: &quot;&lt;&lt;heini.get_years_of_service()&lt;&lt;&quot;\n&quot;;
    cout&lt;&lt;&quot;Verdienst: &quot;&lt;&lt;heini.get_salary()&lt;&lt;&quot;\n&quot;;

    cout&lt;&lt;&quot;Fritz:\n&quot;;
    cout&lt;&lt;&quot;Alter: &quot;&lt;&lt;fritz.get_age()&lt;&lt;&quot;\n&quot;;
    cout&lt;&lt;&quot;Dienstjahre: &quot;&lt;&lt;fritz.get_years_of_service()&lt;&lt;&quot;\n&quot;;
    cout&lt;&lt;&quot;Verdienst: &quot;&lt;&lt;fritz.get_salary()&lt;&lt;&quot;\n&quot;;

    cin.get();
}
</code></pre>
<p>Leider funktioniert es so nicht, erst wenn ich &quot;const&quot; in Zeile 12, 14 und 16 entferne. Soweit ich es sehe, verändern die entsprechenden Funktionen jedoch keine Variablen. Warum kann ich dann kein const verwenden?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1954475</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1954475</guid><dc:creator><![CDATA[S0S]]></dc:creator><pubDate>Sun, 19 Sep 2010 19:18:48 GMT</pubDate></item><item><title><![CDATA[Reply to Warum kann ich hier kein const verwenden? on Sun, 19 Sep 2010 19:21:29 GMT]]></title><description><![CDATA[<p>Du musst das <code>const</code> bei der Implementierung auch angeben.</p>
<p>also</p>
<pre><code class="language-cpp">int Employee::get_age() const // &lt;-- hier!
{
     return age;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1954477</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1954477</guid><dc:creator><![CDATA[rüdiger]]></dc:creator><pubDate>Sun, 19 Sep 2010 19:21:29 GMT</pubDate></item><item><title><![CDATA[Reply to Warum kann ich hier kein const verwenden? on Sun, 19 Sep 2010 19:24:18 GMT]]></title><description><![CDATA[<p>Es klappt! Vielen Dank!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1954478</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1954478</guid><dc:creator><![CDATA[S0S]]></dc:creator><pubDate>Sun, 19 Sep 2010 19:24:18 GMT</pubDate></item></channel></rss>