unexpected return value

Discussion in 'C++' started by Yin99, Aug 4, 2005.

  1. Yin99

    Yin99 Guest

    I'm expecting to see itsAge = 0 in the output of the following code.
    However, in the main() function, itsAge = 1 . Can someone explain??
    thanks,

    Yin99

    ===== begin code ======
    #include <iostream>
    using namespace std;


    class Cat
    {
    public:
    Cat(){
    itsAge = 0;
    cout << "constuctor age is: " << itsAge; //zero here!
    }

    void setAge(int age){
    itsAge = age;
    }

    int getAge(){
    return itsAge;
    }


    private:
    int itsAge;
    };


    int main ()
    {

    Cat *c = new Cat();
    //c->getAge returns 1 here, Why and Where does 1 come from?
    cout << "Cat's Age is: " << c->getAge;
    return 0;

    }
     
    Yin99, Aug 4, 2005
    #1
    1. Advertising

  2. Yin99

    Mike Wahler Guest

    "Yin99" <> wrote in message
    news:...
    >
    > I'm expecting to see itsAge = 0 in the output of the following code.
    > However, in the main() function, itsAge = 1 . Can someone explain??
    > thanks,
    >
    > Yin99
    >
    > ===== begin code ======
    > #include <iostream>
    > using namespace std;
    >
    >
    > class Cat
    > {
    > public:
    > Cat(){
    > itsAge = 0;
    > cout << "constuctor age is: " << itsAge; //zero here!
    > }


    This is not part of your reported problem, but a constructor
    should use an initializer list instead of assigning values to
    members after the fact:

    Cat() : itsAge(0) {
    cout << "constuctor age is: " << itsAge << '\n';
    }

    >
    > void setAge(int age){
    > itsAge = age;
    > }
    >
    > int getAge(){
    > return itsAge;
    > }


    Again, not part of your problem, but more correct is:

    int getAge() const {
    return itsAge;
    }

    >
    >
    > private:
    > int itsAge;
    > };
    >
    >
    > int main ()
    > {
    >
    > Cat *c = new Cat();


    Yet again, not part of your problem, but why are you dynamically
    allocating your object? Why not just define it, e.g.:

    Cat my_cat;

    > //c->getAge returns 1 here, Why and Where does 1 come from?
    > cout << "Cat's Age is: " << c->getAge;


    Try:

    cout << "Cat's Age is: " << c->getAge();


    > return 0;
    >
    > }


    I suspect the output of 1 is because you were passing the address
    of a function, for which theres no << overload, so it's probably
    being converted to a bool, to which assigning any nonzero value
    will result in a value of one.

    -Mike
     
    Mike Wahler, Aug 4, 2005
    #2
    1. Advertising

  3. or it's a simple mistake. Instead of

    > cout << "Cat's Age is: " << c->getAge;


    it should be

    cout << "Cat's Age is: " << c->getAge();

    -haro


    Mike Wahler wrote:
    > "Yin99" <> wrote in message
    > news:...
    >
    >>I'm expecting to see itsAge = 0 in the output of the following code.
    >>However, in the main() function, itsAge = 1 . Can someone explain??
    >>thanks,
    >>
    >>Yin99
    >>
    >>===== begin code ======
    >>#include <iostream>
    >>using namespace std;
    >>
    >>
    >>class Cat
    >>{
    >>public:
    >>Cat(){
    >>itsAge = 0;
    >>cout << "constuctor age is: " << itsAge; //zero here!
    >>}

    >
    >
    > This is not part of your reported problem, but a constructor
    > should use an initializer list instead of assigning values to
    > members after the fact:
    >
    > Cat() : itsAge(0) {
    > cout << "constuctor age is: " << itsAge << '\n';
    > }
    >
    >
    >>void setAge(int age){
    >>itsAge = age;
    >>}
    >>
    >>int getAge(){
    >>return itsAge;
    >>}

    >
    >
    > Again, not part of your problem, but more correct is:
    >
    > int getAge() const {
    > return itsAge;
    > }
    >
    >
    >>
    >>private:
    >>int itsAge;
    >>};
    >>
    >>
    >>int main ()
    >>{
    >>
    >> Cat *c = new Cat();

    >
    >
    > Yet again, not part of your problem, but why are you dynamically
    > allocating your object? Why not just define it, e.g.:
    >
    > Cat my_cat;
    >
    >
    >> //c->getAge returns 1 here, Why and Where does 1 come from?
    >> cout << "Cat's Age is: " << c->getAge;

    >
    >
    > Try:
    >
    > cout << "Cat's Age is: " << c->getAge();
    >
    >
    >
    >> return 0;
    >>
    >>}

    >
    >
    > I suspect the output of 1 is because you were passing the address
    > of a function, for which theres no << overload, so it's probably
    > being converted to a bool, to which assigning any nonzero value
    > will result in a value of one.
    >
    > -Mike
    >
    >
     
    Haro Panosyan, Aug 4, 2005
    #3
  4. Yin99

    Yin99 Guest

    thanks! (I should have seen that)
     
    Yin99, Aug 4, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greenhorn
    Replies:
    15
    Views:
    882
    Keith Thompson
    Mar 6, 2005
  2. Replies:
    4
    Views:
    375
    Roland Pibinger
    Nov 10, 2006
  3. Nathaniel Talbott

    'unexpected return' caused by extension

    Nathaniel Talbott, Oct 11, 2003, in forum: Ruby
    Replies:
    1
    Views:
    144
    Yukihiro Matsumoto
    Oct 20, 2003
  4. Eric Chiasson
    Replies:
    1
    Views:
    423
    Eric Chiasson
    Jan 5, 2007
  5. Hans Sjunnesson
    Replies:
    2
    Views:
    103
    Stefano Crocco
    Dec 4, 2007
Loading...

Share This Page