-
Constructors
For some odd reason, I'm getting totally bogus answers (like answers in the 2 billions when it should output 2)
so I believe I'm initializing my term1 object correctly (initializing to 2) but I am obviously not getting it. Can someone please look this over and let me know what I'm doing wrong?! Thanks for the help!!!
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
class CounterType
{
public:
CounterType(int term); //Allows the user to change initial starting count in main() function.
CounterType(); //Sets the counter to zero
int term_up(); //Adds one to the count
int term_down(); //Subtracts one from the count
void display(ostream& show); //Displays results to the user
private:
int term;
};
int main( )
{
CounterType term1(2), term2;
cout << "counter1 initialized as follows: " << endl;
term1.display(cout);
cout << "counter2 initialized as follows: " << endl;
term2.display(cout);
cin.get();
return 0;
}
int CounterType::term_up()
{
if (term < 0)
{
cout << "Illegal values for counter.";
exit(1);
}
term = term++;
}
CounterType::CounterType(): term(0)
{ /* Left blank intentionally to reset value to 0*/ }
int CounterType::term_down()
{
if (term < 0)
{
cout << "Illegal values for counter.";
exit(1);
}
term = term--;
}
CounterType::CounterType(int count)
{
if (term < 0)
{
cout << "Illegal values for counter.";
exit(1);
}
term = term;
}
void CounterType::display(ostream& show)
{
show << "Current value of the counter is: " << term << endl;
}
-
Code:
CounterType::CounterType(int count)
{
if (term < 0)
{
cout << "Illegal values for counter.";
exit(1);
}
term = term;
}
Take a good look at this constructor, and take a guess as to why term is not being initialized to count.
-
I'm 99.9% sure that types like int, double, float, char etc need to explicitly initialized to zero if you want to set the value to zero.
--
Mats
-
@ bithub, I fixed this (oops, I definately overlooked that), but I still get the same kind of value outputted.
-
post your fixed constructor.
-
sure:
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
class CounterType
{
public:
CounterType(int count); //Allows the user to change initial starting count in main() function.
CounterType(); //Sets the counter to zero
int count_up(); //Adds one to the count
int count_down(); //Subtracts one from the count
void display(ostream& show); //Displays results to the user
private:
int count, m_count;
};
int main( )
{
CounterType count1(2), count2;
cout << "counter1 initialized as follows: " << endl;
count1.display(cout);
cout << "counter2 initialized as follows: " << endl;
count2.display(cout);
cin.get();
return 0;
}
int CounterType::count_up()
{
if (count < 0)
{
cout << "Illegal values for counter.";
exit(1);
}
m_count = count++;
}
CounterType::CounterType(): count(0)
{ /* Left blank intentionally to reset value to 0*/ }
int CounterType::count_down()
{
if (count < 0)
{
cout << "Illegal values for counter.";
exit(1);
}
m_count = count--;
}
CounterType::CounterType(int count)
{
if (count < 0)
{
cout << "Illegal values for counter.";
exit(1);
}
m_count = count;
}
void CounterType::display(ostream& show)
{
show << "Current value of the counter is: " << count << endl;
}
-
The constructor is setting m_count, but the display function is printing count.
-
I'm an idiot. Thanks for your extra set of eyes, I had a late night ;)
-
The question is, why you added another counter in the class?
Also, statements like this have an undefined result:
What you really want to do is to increment the variable, which is done like this:
Code:
++term;
//or
term++;
-
you're absolutely right anon. Thanks!