Help with constructors and classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tommy Lang

    Help with constructors and classes

    Hi !!

    I am a c++ novice and I would appreciate any help with the following:

    I have created a class called "Number". In the main function of my
    program I call the class constructor twice like this(at start up)...
    int main(){
    Number no1;
    Number no2;
    .......

    Everything is fine so far.
    Then in the class constructor I want to ask for two numbers and add
    them to the private member variables m_no1 and m_no2...
    Number::Number( )
    {
    if (first_time) //first-time is a private boolean variable (how to
    init it??)
    {cout << "Enter number: ";
    cin >> m_no1; cin.get();}
    else
    {cout << "Enter next number: ";
    cin >> m_no2; cin.get();}

    }

    I have tried this approach but I couldn't get it to work properly. And
    when I got it to work it NEVER got to the else statement. First time I
    want to get m_no1 next time m_no2. Any pointers???

    Thanks,
    Thomas
  • Ron Natalie

    #2
    Re: Help with constructors and classes


    "Tommy Lang" <mums_se@yahoo. se> wrote in message news:78dde37d.0 309231038.4cb8d 694@posting.goo gle.com...[color=blue]
    > Hi !!
    >
    > I am a c++ novice and I would appreciate any help with the following:
    >
    > I have created a class called "Number". In the main function of my
    > program I call the class constructor twice like this(at start up)...[/color]

    You're not really calling the constructor. You're creating two objects. The
    constructor is called as part of the entire object creation.
    [color=blue]
    > if (first_time) //first-time is a private boolean variable (how to[/color]

    It is ALWAYS important to provide all the requisite information. Your example
    doesn't show the declaration of first_time. I presume from your description it
    looks like this:
    bool first_time;

    Which yes, creates an uninitialized variable first_time. You could initialize it
    but it still wouldn't be right. The above is a non-static member. This means that
    every instance of your Number class gets it's own copy. Even if you initialize
    it to true, it would always test true in the constructor (the constructor never runs
    more than once on any single object).

    What you want to do is declare it as a static member:

    static bool first_time;

    This is what in other languages is called a class variable. It's shared between
    all instnaces of the same class. You must also define and initialize the varable.
    Outsdie the class definition put:

    bool Number::first_t ime = true;



    Comment

    • Gianni Mariani

      #3
      Re: Help with constructors and classes

      Tommy Lang wrote:[color=blue]
      > Hi !!
      >
      > I am a c++ novice and I would appreciate any help with the following:
      >
      > I have created a class called "Number". In the main function of my
      > program I call the class constructor twice like this(at start up)...
      > int main(){
      > Number no1;
      > Number no2;
      > ......
      >
      > Everything is fine so far.
      > Then in the class constructor I want to ask for two numbers and add
      > them to the private member variables m_no1 and m_no2...
      > Number::Number( )
      > {
      > if (first_time) //first-time is a private boolean variable (how to
      > init it??)
      > {cout << "Enter number: ";
      > cin >> m_no1; cin.get();}
      > else
      > {cout << "Enter next number: ";
      > cin >> m_no2; cin.get();}
      >
      > }
      >
      > I have tried this approach but I couldn't get it to work properly. And
      > when I got it to work it NEVER got to the else statement. First time I
      > want to get m_no1 next time m_no2. Any pointers???[/color]


      It sounds like you want first_time to be a static value.

      Secondly, it seems like you never make it false.

      Post a compilable chunk-o-code so we can give you better advice.

      Comment

      • Kevin Goodsell

        #4
        Re: Help with constructors and classes

        Tommy Lang wrote:
        [color=blue]
        > Hi !!
        >
        > I am a c++ novice and I would appreciate any help with the following:
        >
        > I have created a class called "Number". In the main function of my
        > program I call the class constructor twice like this(at start up)...
        > int main(){
        > Number no1;
        > Number no2;
        > ......
        >
        > Everything is fine so far.
        > Then in the class constructor I want to ask for two numbers and add
        > them to the private member variables m_no1 and m_no2...
        > Number::Number( )
        > {
        > if (first_time) //first-time is a private boolean variable (how to
        > init it??)[/color]

        You are right to worry about initializing this. If you execute this 'if'
        without first initializing first_time, your program's behavior is
        undefined (could do anything, including crashing, behaving
        unpredictably, working as expected, making long-distance phone calls, etc.)

        The thing is, this is a constructor. Initialization is what it *does*.
        So whatever you set 'first_time' to, that's what it's going to be. It
        doesn't make much sense to branch based on it, because the result will
        always be the same:

        first_time = true;
        if (first_time)
        {
        // well, obviously this will be executed.
        }
        else
        {
        // this will never be executed.
        }
        [color=blue]
        > {cout << "Enter number: ";
        > cin >> m_no1; cin.get();}[/color]

        You might have to explain why you think you need cin.get() here.
        [color=blue]
        > else
        > {cout << "Enter next number: ";
        > cin >> m_no2; cin.get();}
        >
        > }
        >
        > I have tried this approach but I couldn't get it to work properly. And
        > when I got it to work it NEVER got to the else statement. First time I
        > want to get m_no1 next time m_no2. Any pointers???[/color]

        You don't want to branch. You want 2 numbers, unconditionally , right?

        cout << "Enter the first number: ";
        cin >> m_no1;
        cout << "Enter the second number: ";
        cin >> m_no2;

        No 'if' is necessary.

        ....

        OK, but from the sound of it, this isn't what you *really* want to do.
        You need to understand that the constructor will be invoked
        *independently* for each object. In general, the constructor cannot (and
        should not) know if it's already been invoked, or how many times. What
        you really want is something like this:

        #include <iostream>

        using namespace std;

        class Number
        {
        public:
        Number() { cout << "Enter number: "; cin >> m_no; }
        private
        int m_no;
        };

        int main()
        {
        Number no1;
        Number no2;

        return 0;
        }

        This will work, but you can't change the text of the prompt, unless you
        want to pass it in as a parameter to the constructor, but basically
        doing I/O is a constructor is a bit screwy anyway (you can do it if you
        really want to, but it's very unusual), so you should probably do
        something like this instead:

        #include <iostream>

        using namespace std;

        class Number
        {
        public:
        void Read() { cin >> m_no; }
        private:
        m_no;
        };

        int main()
        {
        Number no1, no2;
        cout << "Enter the first number: ";
        no1.Read();
        cout << "Enter the second number: ";
        no2.Read();

        return 0;
        }

        -Kevin
        --
        My email address is valid, but changes periodically.
        To contact me please use the address from a recent posting.

        Comment

        • jeffc

          #5
          Re: Help with constructors and classes


          "Tommy Lang" <mums_se@yahoo. se> wrote in message
          news:78dde37d.0 309231038.4cb8d 694@posting.goo gle.com...[color=blue]
          > Hi !!
          >
          > I am a c++ novice and I would appreciate any help with the following:
          >
          > I have created a class called "Number". In the main function of my
          > program I call the class constructor twice like this(at start up)...
          > int main(){
          > Number no1;
          > Number no2;
          > ......
          >
          > Everything is fine so far.
          > Then in the class constructor I want to ask for two numbers and add
          > them to the private member variables m_no1 and m_no2...
          > Number::Number( )
          > {
          > if (first_time) //first-time is a private boolean variable (how to
          > init it??)[/color]

          OK, I can tell already that you have a misunderstandin g about how objects
          are created. You have a serious design problem here. You do NOT want to
          ask for both numbers at the same time in the constructor. BAD BAD. Forget
          that first_time variable. You want to design your program more like this:

          int main()
          {
          cout << "Enter number:";
          cin >> m_no1;
          // at this point you want to create no1, but you want to use a constructor
          // with which you can pass in m_no1

          cout << "Enter next number: ";
          cin >> m_no2;
          // at this point you want to create no2, but you want to use a constructor
          // with which you can pass in m_no2
          }

          You will have to write a new constructor for Number that can take that
          number you pass in. Put that in your pipe and smoke it for awhile and then
          come back for a second round of questions.


          Comment

          • Tommy Lang

            #6
            Re: Help with constructors and classes

            Thanks Ron. Your help solved my problem. I needed to use a static bool
            variable. And setting it to true outside the class def. just like you
            said.
            (bool Number::first_t ime = true;)

            Thanks again






            "Ron Natalie" <ron@sensor.com > wrote in message news:<3f70964f$ 0$51795$9a6e19e a@news.newshost ing.com>...[color=blue]
            > "Tommy Lang" <mums_se@yahoo. se> wrote in message news:78dde37d.0 309231038.4cb8d 694@posting.goo gle.com...[color=green]
            > > Hi !!
            > >
            > > I am a c++ novice and I would appreciate any help with the following:
            > >
            > > I have created a class called "Number". In the main function of my
            > > program I call the class constructor twice like this(at start up)...[/color]
            >
            > You're not really calling the constructor. You're creating two objects. The
            > constructor is called as part of the entire object creation.
            >[color=green]
            > > if (first_time) //first-time is a private boolean variable (how to[/color]
            >
            > It is ALWAYS important to provide all the requisite information. Your example
            > doesn't show the declaration of first_time. I presume from your description it
            > looks like this:
            > bool first_time;
            >
            > Which yes, creates an uninitialized variable first_time. You could initialize it
            > but it still wouldn't be right. The above is a non-static member. This means that
            > every instance of your Number class gets it's own copy. Even if you initialize
            > it to true, it would always test true in the constructor (the constructor never runs
            > more than once on any single object).
            >
            > What you want to do is declare it as a static member:
            >
            > static bool first_time;
            >
            > This is what in other languages is called a class variable. It's shared between
            > all instnaces of the same class. You must also define and initialize the varable.
            > Outsdie the class definition put:
            >
            > bool Number::first_t ime = true;[/color]

            Comment

            Working...