Constructors

This is a discussion on Constructors within the C++ Programming forums, part of the General Programming Boards category; For some odd reason, I'm getting totally bogus answers (like answers in the 2 billions when it should output 2) ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75

    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;
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    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.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    @ bithub, I fixed this (oops, I definately overlooked that), but I still get the same kind of value outputted.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    post your fixed constructor.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    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;
    }

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    The constructor is setting m_count, but the display function is printing count.

  8. #8
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    I'm an idiot. Thanks for your extra set of eyes, I had a late night

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The question is, why you added another counter in the class?

    Also, statements like this have an undefined result:

    Code:
    term = term++;
    What you really want to do is to increment the variable, which is done like this:
    Code:
    ++term;
    //or 
    term++;
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    you're absolutely right anon. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays and Constructors
    By Verdagon in forum C++ Programming
    Replies: 1
    Last Post: 07-20-2005, 08:20 PM
  2. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 05:24 PM
  3. constructors, arrays, and new
    By Thantos in forum C++ Programming
    Replies: 6
    Last Post: 05-30-2004, 07:21 PM
  4. constructors in classes
    By Kenman in forum C++ Programming
    Replies: 16
    Last Post: 07-28-2003, 08:35 AM
  5. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 10:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21