problems with variable in class decleration prog.

This is a discussion on problems with variable in class decleration prog. within the C++ Programming forums, part of the General Programming Boards category; ok, i'm new to c++ and trying to learn it on my own. i've been doing not that bad...until i ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    3

    problems with variable in class decleration prog.

    ok, i'm new to c++ and trying to learn it on my own. i've been doing not that bad...until i reached the point of classes. i copied the code from "teach yourself c++ in 21 days" and decided to augment it to ask the user for a certain piece of input. here is the augmented code:

    Code:
    #include <cstdlib>
    #include <iostream>
    
    class cat
    {
          public:
                 int itsAge;
                 int itsWeight;
    };
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int x;
        cat frisky;
        frisky.itsAge = x;
        cout << "How old is frisky?\n";
        cin >> x;
        cout << "Frisky is a cat who is ";
        cout << frisky.itsAge << " years old.\n";
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    i hope i did that right. any road....when i run it, well here is a sample output:

    How old is frisky?
    5
    Frisky is a cat who is 2 years old
    press any key to continue....

    ok, where the duce is the 2 coming from. i never declared x to = 2, it was supposed to be user defined. help please? thank you in advance....

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    When you set Frisky's age to x, you are taking the value of x and copying it to Frisky. Any subsequent changes to x will not affect Frisky. Ask the user for x and then assign x to Frisky's age.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    3
    i thought that's what i was doing by declaring "frisky.itsAge = x;" i guess i am not understanding what you meant. pardon my density....but is there another way you could put it?

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,037
    The key part of CodeMonkey's post is here:
    Ask the user for x and then assign x to Frisky's age.

    Ask first, then assign.

    Alternatively you can make itsAge a pointer, and use &x to assign, and *frisky.itsAge to access. This design is less typical, but it seem to be how you are expecting the code above to work.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    3
    that worked... i put

    cat frisky;
    frisky.itsAge = x;

    AFTER the cin >> x; and it worked perfectly. thanks for your help and patience

  6. #6
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    it's an important concept to differentiate between the locations of variables and the values contained at those locations.

    you're copying values here, but when you get into pointers and references, they will work in the way you're expecting here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access protected base class template variable
    By pjotr in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2007, 04:34 AM
  2. Linking problems, class problems
    By Kheila in forum C++ Programming
    Replies: 12
    Last Post: 11-22-2005, 12:47 AM
  3. Problems with my custom string class
    By cunnus88 in forum C++ Programming
    Replies: 15
    Last Post: 11-15-2005, 07:21 PM
  4. Trouble with a class variable
    By TravisDane in forum C++ Programming
    Replies: 9
    Last Post: 12-07-2002, 11:59 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

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