Thread: A Class question.

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    8

    A Class question.

    hello, I am currently doing an university assignment and have completed it but unsure of a little part. Its a homework for practicing to write classes with a header file and two cpp files instead of all in one.

    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.

    the question now:
    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;

    so i have been trying to find a way and ended up doing this:

    Account::getAccBal();
    Amt = Amt + AccBal;
    Account::setAccBal(Amt);

    I am not changing the value of the private data member directly so that is fine, right?

    I am sorry that it is so confusing, i can post the whole code so you can understand it better.

    Thanks in advance

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    >Account::getAccBal();

    call getAccBal() from an object with the dot operator.
    Like:
    account.getAccBal();

  3. #3
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Getter and setter methods are member functions. That's why you can use them to access private fields. But yes, your idea is correct. If you'd post your implementation of the Account class we could comment on whether that is correct too.
    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. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    thanks for the replies!

    @manasij7479:
    I cant do that because I am writing the implementation of the class not the actual program if i understood what you meant.

    @msh
    thanks, here is the implementation as well:

    Code:
    // Account.cpp
    #include <iostream>
    #include "Account.h"
    #include <string>
    #include "stdafx.h"
    
    using namespace std;
    
    Account::Account(double Amt, string name)
    {
    	if(Amt <= 0)
    	{
    		Amt = 0;
    		cout << "The initial Balance was invalid" << endl;
    	}
    	Account::setAccBal(Amt);
    	Account::setAccHolder(name);
    
    
    }
    
    void Account::setAccBal(double Amt)
    {
    	
    	AccBal = Amt;
    	
    }
    
    double Account::getAccBal()
    {
    	return AccBal;
    }
    
    void Account::setAccHolder(string name)
    {
    	AccHolder = name;
    	
    }
    
    string Account::getAccHolder()
    {
    	return AccHolder;
    }
    
    void Account::creditUpdate(double Amt)
    {
    	Account::getAccBal();
    	Amt = Amt + AccBal;
    	
    	Account::setAccBal(Amt);
    }
    
    void Account::debitUpdate(double Amt)
    {
    		if(Amt > Account::AccBal)
    	{
    		cout << "The Debit amount of $" << Amt << " exceeded your current account balance" << endl;
    	}
    	else
    	{
    		Account::getAccBal();
    		Amt = AccBal - Amt;
    		Account::setAccBal(Amt);
    	}
    }
    
    void Account::interestCal()
    {
    	Account::getAccBal();
    	int intcal;
    	intcal = (AccBal*0.1) + AccBal;
    	Account::setAccBal(intcal);
    }
    
    void Account::printInfo()
    {
    	cout << "Name:      " << Account::getAccHolder() << endl;
    	cout << "Balance :   " << Account::getAccBal() << endl;
    	cout << "*****************************************************************************" << endl;
    }

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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.)]

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    There is nothing technically wrong with the way he wrote the Account constructor, but the point of the initialization list is to call the constructors of all the data members to initialize. It's simply the most straightforward way of setting initial values most of the time. Consider for example objects that can't be default constructed. You must use the initialization list then.

    If you need to do more than set initial values, that is what the constructor body is for.
    Last edited by whiteflags; 09-09-2011 at 11:02 PM.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    Account::Account(double Amt, string name)
    {
    	if(Amt <= 0)
    	{
    		Amt = 0;
    		cout << "The initial Balance was invalid" << endl;
    	}
    	Account::setAccBal(Amt);
    	Account::setAccHolder(name);
    }
    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).

    Also, since we are inside the Account class, you can remove the "Account::" prefix. So simply..

    setAccBal(Amt);
    setAccHolder(name);

    ...will do.

    Code:
    void Account::creditUpdate(double Amt)
    {
    	Account::getAccBal();
    	Amt = Amt + AccBal;
    	
    	Account::setAccBal(Amt);
    }
    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.

    Quote Originally Posted by manasij7479 View Post
    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 ? )
    The initializer list calls the constructor for the objects (otherwise the default constructor will be called). Assignment in the body will call the assignment operator.
    For built-in types, you don't get any advantage, but for other objects, you can save time by calling an appropriate constructor instead of calling the default constructor and then the assignment operator.

    [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.)]
    It tells the compiler to look inside a specific namespace. Classes are namespaces too, in a sense. Static member functions appear inside the class's namespace, so they can be called this way. Normal member functions belongs to an instance and cannot be called this way. However, when you create a member function pointer, you must take the address of a function in a class, which means getting one from the class namespace and not an instance.
    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. #8
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Classes are namespaces too, in a sense.
    In a technical sense or a conceptual sense ?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    In the very least, a conceptual sense. Don't know what the standard says.
    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. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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.
    [...]
    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).
    Are you trying to compare regular objects to stream objects and the way that they normally handle their errors? If an exception is thrown in the constructor then the object doesn't exist because the memory of the member data is cleaned up. It's not an "error state". It's why this answer treats error states as an alternative instead of saying it's the same thing. Read the link for proper information.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, what I meant was that simply printing an error and allow the constructor to continue will put the object into an error state.
    Throwing an exception will "fix" this.
    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.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Because the OP defined it to be an error. To be fair, I actually didn't see that the code sets the balance to 0, but it's still a good advice. Another good advice is to take an unsigned int instead of a signed int to make it impossible to enter a negative balance.
    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.

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    That would give clients trillions in currency when they owe money. I am all for the unsigned dollar amount idea!

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    At the very least, you should get a warning if you tried to do that...
    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.

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