Thread: A Class question.

  1. #16
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You'd have to prove it to me because I know you're wrong.

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ummm, okay, that's just weird. Why the hell wouldn't a compiler warn about signed -> unsigned conversion?
    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.

  3. #18
    Novice
    Join Date
    Jul 2009
    Posts
    568
    I'd guess that it's because you're not losing any bits, just changing the interpretation of the MSB.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Which can lead to way inaccurate data, so that's not a good reason.
    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.

  5. #20
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by program View Post
    but for this assignment,my teacher said that although we can access data members that are private with member functions of said class, set and get functions are used to access them instead.

    In the first function i will take a argument from the function and then add that to the value of the data member account balance (AccBal) but since i cant access the data member directly i cant do this:

    AccBal = Amt + AccBal;
    Actually it depends on where you want to do that. Your teacher is right, since you can't access private member data, you have to use a public member function in code that uses the class. However, member functions will always have access to at least one set of data. For example:

    myAccount.setAccBal(Amt);

    Member function setAccBal will always have access to the data of the object it is called with, called this object by the implementation. Additionally, member functions will also have access to data members of other objects of the same class, if you pass them in as arguments.

  6. #21
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    ok, real sorry i didnt reply earlier but i lost my internet conenction for a while (heavy rains and stuff)

    ok to reply to you all now:

    Quote Originally Posted by manasij7479 View Post
    Looks Ok, except for some conceptual gotchas.
    The member functions have access to the private members. So you don't need to get anything there. I also noticed that you are using the get functions but not using their values.

    Also, you could consider initializing AccBal and AccHolder by a member initialization list. (...I found that Tip in Effective C++, but can't recall the reason...Anyone else with more RAM ? )

    [And w.r.t my earlier point, you don't actually need to prefix Account:: even in the implementation...(I didn't even know it was allowed except for static members...but the compiler seems to accept it.)]
    the member functions have access, but I was told to avoid accessing it directly. So i was trying to find a way to use the set and get functions to do the functions in the implementation.



    Quote Originally Posted by Elysia View Post
    In case you've learned exceptions and are allowed to use them, you should throw an exception is Amt <= 0 instead of printing an error message.
    Furthermore, you don't want to continue if this error occurs, so you have to put a return in there, as well, so you don't call setAccBal, etc.
    If an error occurs, the object will basically be in an error state, but I think you can ignore that fact for now. It is just a little something to keep in mind (that's why throwing an exception is a good idea).
    I was told to do it like this and we haven't learned exceptions yet as well so yeah

    Quote Originally Posted by whiteflags View Post
    Why is taking a negative balance and making it zero an error state? Because you say so? Do you know that's not what's supposed to happen?

    Also, about the scope operator, the class name is used for name lookup and is treated as a public member of the class. It is not a namespace.
    same as above, it was mostly structured in that we were given the basic premise, the code for the tester.cpp and we were to write the class header and cpp file to make the output the same as the one given to us.

    Actually it depends on where you want to do that. Your teacher is right, since you can't access private member data, you have to use a public member function in code that uses the class. However, member functions will always have access to at least one set of data. For example:

    myAccount.setAccBal(Amt);

    Member function setAccBal will always have access to the data of the object it is called with, called this object by the implementation. Additionally, member functions will also have access to data members of other objects of the same class, if you pass them in as arguments.
    i know the member functions of a class can access the private data members but my teacher personally prohibits us from accessing it directly for his class even though he told it it does work.


    The getAccBal here does absolutely nothing. This get function is supposed to enable the outside to access the account balance, and returns that. So you would have to store that balance in some variable to use it.
    Nevertheless, getAccBal simply returns the private member that contains the balance which you have access to since you are inside the class implementation, so you could use that instead if you want.
    @Elysia: I figured this much but how can you put the value that the get function in another variable without the direct access to the data member? I am trying not to use the data member itself but using the set and get to manipulate the value if possible. i could just do something like this:

    int X;
    Account::getAccBal();
    X = AccBal;

    but that would be accessing the data member directly? or is there another way?
    Last edited by program; 09-10-2011 at 02:03 PM.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by program View Post
    ok, i feel like i am getting a bit confused...

    in what i said before with:

    Amt = Amt + AccBal;

    would that be directly accessing the private data member "AccBal" ?

    I think you guys answered it but I am not sure lol.
    Yes, that would be accessing AccBal directly (I assume AccBal is a member variable).
    If you absolutely need to avoid accessing it directly, then substitute it for the get function.
    Something like

    Amt += GetBalance();
    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.

  8. #23
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Quote Originally Posted by Elysia View Post
    Yes, that would be accessing AccBal directly (I assume AccBal is a member variable).
    If you absolutely need to avoid accessing it directly, then substitute it for the get function.
    Something like

    Amt += GetBalance();
    thanks! this is exactly what i was looking for! but what is the "+="? i used just a "=" and it worked.

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    a += b <==> a = a + b
    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.

  10. #25
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    thanks again! and thanks to everyone to who helped me as well

  11. #26
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by Elysia View Post
    Which can lead to way inaccurate data, so that's not a good reason.
    Inaccurate,but not lost. I agree with you that a compiler should, logically, emit a warning for this, but it doesn't and I was guessing at the rationale for why.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 01-13-2008, 05:57 PM
  2. Class question.
    By Loic in forum C++ Programming
    Replies: 1
    Last Post: 09-25-2007, 12:50 AM
  3. Question about class
    By Roy01 in forum C++ Programming
    Replies: 3
    Last Post: 11-20-2006, 11:25 AM
  4. class question
    By l2u in forum C++ Programming
    Replies: 1
    Last Post: 09-29-2006, 09:41 AM
  5. class question
    By knight543 in forum C++ Programming
    Replies: 1
    Last Post: 02-20-2002, 10:36 AM