Like Tree1Likes

A Question/problem with classes and sending a variable to an object

This is a discussion on A Question/problem with classes and sending a variable to an object within the C++ Programming forums, part of the General Programming Boards category; Hi there, sorry if this is really stupid but I am trying as hard as I can to understand classes ...

  1. #1
    Trainee Geek Draylath's Avatar
    Join Date
    Nov 2011
    Location
    Cambridge
    Posts
    24

    A Question/problem with classes and sending a variable to an object

    Hi there,

    sorry if this is really stupid but I am trying as hard as I can to understand classes from a book. I have declared my class and put in the accessor functions and declared everything private/public as I have been told.

    I want to write a program that keeps a employee record. It asks the user for the data and then prints it back out to confirm it has all worked and has been stored correctly.

    I have cut down the code attached to just one peice of data (the employee number) and two accessor methods untill I get this right. The problem is that when I run the program it just flicks up the console screen and then it vanishes despite the system("PAUSE"); after return 0; (I know, I know it's bad and I proboly shouldn't use it).

    Can anyone tell me what I'm doing wrong and how to fix it. Many thanks

    Code:
    #include <iostream>using namespace std;
    
    
    class record
    {
    private:
            int p_number;
           
            
    public:
           void setp_number(int number);
           
           int getp_number();
          
    };
    //accessor functions//
    //1
    void record::setp_number(int number)
    {
         p_number = number;
    }
    
    
    //get1
    int record::getp_number()
    {
        return p_number;
    }
    
    
    int main()
    {
        int number;
        
        record Clive;
        cout << "set employee number: ";
        cin >> number;
        
        
        cout << "Thanks! To confirm:...." << endl;
        cout << "Clive is number: " << Clive.getp_number();
        
        
        
        
        return 0;
        system("PAUSE");
    }

  2. #2
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    255
    I suggest you read up on what return does. (Hint: Move your system( "Pause"); line before the return 0; line).

  3. #3
    Trainee Geek Draylath's Avatar
    Join Date
    Nov 2011
    Location
    Cambridge
    Posts
    24
    oh crap I feel a bit silly now,

    thanks antred for clearing that up with the system("PAUSE"). I did know about that just my tired mind

    I'm lost on your comment on reading what return does but I'll do it. I put it in because it said to do so in my book.

    Anyways thanks

  4. #4
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    255
    Quote Originally Posted by Draylath View Post
    I'm lost on your comment on reading what return does but I'll do it. I put it in because it said to do so in my book.
    I figured you didn't know that the return statement basically terminates control flow in the current function, but apparently it was merely a brainfart so disregard my previous suggestion.

  5. #5
    Trainee Geek Draylath's Avatar
    Join Date
    Nov 2011
    Location
    Cambridge
    Posts
    24
    Actually I am seriously lost now. Seems I've cut one head off the hydra

    no matter what I input to the p_number it always comes back at me as 2 not what I inputed

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,915
    Where are you setting p_number to any value?

    What is the purpose of your setp_number() function?

    Jim

  7. #7
    Trainee Geek Draylath's Avatar
    Join Date
    Nov 2011
    Location
    Cambridge
    Posts
    24
    I'm trying to pass the value of int number in the main part of the program into setp_number so that it will set the user input number into Clive and then be retrieved with getp_number. In all my books this is done in the code of main itself i.e Cat Frisky(5) but I don't want to do this. I want to set the value of p_number by user input.

    Thanks fro your reply by the way I think I might have seriously missed something

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    2,915
    Please show the line where you assign a value to p_number, anywhere in your code. I see you using a variable named number, but no where do I actually see you assigning a value to your class variable p_number. Again I ask what is the purpose of your class member function setp_number()?

    Jim
    Last edited by jimblumberg; 06-24-2012 at 02:51 PM.

  9. #9
    Trainee Geek Draylath's Avatar
    Join Date
    Nov 2011
    Location
    Cambridge
    Posts
    24
    That'll be the problem then, thanks Jim. You're quite right. I haven't assigned it a value at all have I? Apologies for me getting you to point out the obvious. Thanks again I'll keep bashing away at it but this is the closest I've ever got with classes so it's going ok

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    2,915
    That is what the function I keep asking about is for, maybe you want to try using it.

    Jim
    Last edited by jimblumberg; 06-24-2012 at 03:29 PM.

  11. #11
    Trainee Geek Draylath's Avatar
    Join Date
    Nov 2011
    Location
    Cambridge
    Posts
    24
    I think I am closer now but it is still not working. I'm getting compiler errors now. I've done the following:
    Code:
    int main(){
        int number;
        
        record Clive;
        cout << "set employee number: ";
        cin >> number;
        Clive.setp_number(int number);
        
        cout << "Thanks! To confirm:...." << endl;
        cout << "Clive is number: " << Clive.getp_number();
    this gives me an error saying expected primary expression before int in Clive.Setp_number etc line

  12. #12
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    255
    Code:
    cout << "set employee number: ";
    cin >> number;
    Clive.setp_number(int number);
    Lose the red part.

  13. #13
    Trainee Geek Draylath's Avatar
    Join Date
    Nov 2011
    Location
    Cambridge
    Posts
    24
    mmm, now it's back to saying 2 again regardless of input. sorry to keep on about this I just want to get this right

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    2,915
    Post your current code.

    Jim

  15. #15
    Trainee Geek Draylath's Avatar
    Join Date
    Nov 2011
    Location
    Cambridge
    Posts
    24
    Code:
    #include <iostream>
    using namespace std;
    
    
    class record
    {
    private:
            int p_number;
           
            
    public:
           void setp_number(int number);
           
           int getp_number();
          
    };
    //accessor functions//
    //1
    void record::setp_number(int number)
    {
         number = p_number;
    }
    
    
    //get1
    int record::getp_number()
    {
        return p_number;
    }
    
    
    int main()
    {
        int number;
        
        record Clive;
        cout << "set employee number: ";
        cin >> number;
        Clive.setp_number(number);
        
        cout << "Thanks! To confirm:...." << endl;
        cout << "Clive is number: " << Clive.getp_number();
        
        
        
         system("PAUSE");
        return 0;
       
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sending a structure variable through MPI in C
    By shrek in forum C Programming
    Replies: 8
    Last Post: 07-01-2011, 10:57 AM
  2. Pointers- Difference From Sending a Copy of the Object?
    By Shokwav in forum C++ Programming
    Replies: 7
    Last Post: 05-31-2011, 02:28 PM
  3. Global variable vs parameter 'sending'.
    By MipZhaP in forum C++ Programming
    Replies: 16
    Last Post: 04-03-2011, 10:58 AM
  4. Replies: 12
    Last Post: 02-12-2009, 02:39 PM
  5. Creating an object in a dll & sending it to an exe file
    By SteveRodrigue in forum C++ Programming
    Replies: 6
    Last Post: 12-14-2006, 02:01 PM

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