I need some help understanding the basics of constructors, however, I cannot find any material concerning them on this site. Can someone please teach me the basics of constructors and how you use them? Thank you very much for all the help in advance!!!
Constructors are like member functions for any class.You can find out if the member function is constructor by looking its name.The name of the constructor should be exactly as the name of the class.The other thing about constructor is that it should not return anything.
#include <iostream>
#include <cstdlib>
usingnamespace std;
//Class for a counter:
class CounterType
{
public:
CounterType(int count); //Allows the user to input their own value
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;
};
int main( )
{
CounterType counter1(2), counter2;
cout << "counter1 initialized as follows: " << endl;
counter1.display(cout);
cout << "counter2 initialized as follows: " << endl;
counter2.display(cout);
cin.get();
return 0;
}
int CounterType::count_up()
{
if (count < 0)
{
cout << "Illegal values for counter.";
exit(1);
}
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);
}
count -= count;
}
CounterType::CounterType(int count)
{
if (count < 0)
{
cout << "Illegal values for counter.";
exit(1);
}
count = count;
}
void CounterType::display(ostream& show)
{
show << "Current value of the counter is: " << count << endl;
}
I want this program to use every single member function and output to user, can someone help me out please?! Thank you!
line 15, you declare the member varialbe as 'count'. Then in all of your member functions, you also declare the passed parameter as 'count'. For example, see line 61:
count = count;
This basically does nothing. You're telling it to set 'count' to itself.
The problem is that there are multiple variables named 'count', so the compiler defaults to the most local version of 'count'. Here, this means you're changing the passed value 'count', and not the member variable 'count'.
The solution to this is to rename your member variable to something else. Some people like to put 'm_' before member variables to show that they're member vars. For example instead of calling your member var 'count' you could call it 'm_count', and then do:
m_count = count;
Alternatively, to distinguish between local and member var versions of count, you can use the 'this' pointer to indicate you want to use the member var:
#include <iostream>
#include <cstdlib>
usingnamespace 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;
}
I am assuming you are talking about bad value of count.
You are getting bad answer because in CounterType count1(2), count2 ;
For count1(2), Parameterized constructor is called. i.e. it calls
1 2 3 4 5 6 7 8 9 10
CounterType::CounterType(int count)
{
if (count < 0)
{
cout << "Illegal values for counter.";
exit(1);
}
m_count = count;
}
And in this constructor you are initializing m_count and not count. So count still has garbage value in it when you output count in display function.