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.
Originally Posted by
manasij7479
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.