private variables, functions help

This is a discussion on private variables, functions help within the C++ Programming forums, part of the General Programming Boards category; Hey all i am having some problems with an assignment of mine. This is the code it reffences to: Code: ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    16

    Post private variables, functions help

    Hey all i am having some problems with an assignment of mine. This is the code it reffences to:
    Code:
    class xClass
    {
    public:
         void func();
         void print() const;
         xClass ();
         xClass (int, double);
    
    private:
         int u;
         double w;
    {;
    The questions are:
    1) Write the definition of the member function func so that u is set to 10 and w is set to 15.3.
    2) Write the definition of the member function print that prints the contents of u and w.
    3) Write the definition of the default constructor of the class xClass so the private member variables are initialized to 0.
    So far this is what i have for the answers-but im not quite sure i am correct.
    Code:
    1)
    void xClass::func()
    {
         xClass exmpD;
         exmpD.u = 10;
         exmpD.w = 15.3;
    }
    
    2)
    void xClass::print() const
    {
         cout << u << " " << w << end1;
    }
    
    3)
    public:
    set2Zero()
    {
         u = 0;     
         w = 0;  
    }
    So, how do my answers look? Am i wayyyy off?

    David

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Decently close.

    Remember that you are writing a member function of the class, so everything that is also a member of that class will be available to you. Thus in your func function you can just write u = 10, and w = 15.3. The way you have it now, you are creating an object of the class, assigning variables, and it just so happens that in this fashion your object will be destroyed at the end of the call.

    Code:
    void xClass::func()
    {
         u = 10;
         w = 15.3;
    }
    Also a destructor has a special syntax of ~ClassName() in c++. You should put your set to zero calls as the assignment states in this function. Or just call your set2Zero function from the destructor.

    Code:
    xClass::~xClass()
    {
       u = 0;
       w = 0;
    }

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by valaris View Post
    Also a destructor has a special syntax of ~ClassName() in c++. You should put your set to zero calls as the assignment states in this function. Or just call your set2Zero function from the destructor.

    Code:
    xClass::~xClass()
    {
       u = 0;
       w = 0;
    }
    Why would you bother to change inert state inside a destructor?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Quote Originally Posted by brewbuck View Post
    Why would you bother to change inert state inside a destructor?
    Mostly because I quickly read the post and his assignment requirements and could have sworn it wanted the destructor to do this.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Quote Originally Posted by StealthRT View Post
    Code:
    1)
    void xClass::func()
    {
         xClass exmpD;
         exmpD.u = 10;
         exmpD.w = 15.3;
    }
    I'll add an analogy to this.
    Suppose you tell your car to start the engine. This should call Car::StartEngine.
    Then in StartEngine, the car creates another car and tries to that that cars engine (which it possibly cannot do, nor should it).
    Makes sense? I didn't think so.
    The car would want to start its own engine in this case, just as xClass::func would set its own member variables to values and not another instance's.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    For 3, you need to write a constructor, not just another method. The syntax looks like writing a function with the name of the class. Also, for constructors, you can use an initializer list to initialize variables. Look that one up.

    On a different note, it is convention to start class names with capital letters.
    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.

  7. #7
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Don't waste your time, he's already received his help at other forums where he posted, like here, here, and here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. about private class variables
    By bulletbutter in forum C++ Programming
    Replies: 5
    Last Post: 04-18-2008, 12:13 PM
  2. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 09:54 AM
  3. Public vs. Private variables in classes
    By blankstare77 in forum C++ Programming
    Replies: 13
    Last Post: 08-31-2005, 05:43 PM
  4. passing variables between functions
    By owi_just in forum C Programming
    Replies: 6
    Last Post: 05-08-2005, 09:12 AM
  5. Naming variables, functions...
    By Ariod in forum Tech Board
    Replies: 9
    Last Post: 08-19-2003, 12:17 PM

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