Could somebody give me an exercise to do that envolves simple classes with maybe a bit of inheritance please, as I can't think of any programs to write that could encorporate this.
Could somebody give me an exercise to do that envolves simple classes with maybe a bit of inheritance please, as I can't think of any programs to write that could encorporate this.
Guessing that you just want to try out inheritance, a good one
is a bank account. Implement some getters and setters for
customer details and account balance. Then inherit from this
to produce more specific types of accounts such as a savings
account, current account and so on. Try implementing different
interest methods and so on. That would be a good starting point
for inheritance.
No No's:
fflush (stdin); gets (); void main ();
Goodies:
Example of fgets (); The FAQ, C/C++ Reference
My Gear:
OS - Windows XP
IDE - MS Visual C++ 2008 Express Edition
ASCII stupid question, get a stupid ANSI
Any way a few virtual functions could be put in?
Well yes - your base class could calculate interest using a straight
line method, using the original principle amount. Then in derived
classes, you could use compound interest methods. Different
types of accounts can allow different overdraft limits, charges,
that kind of stuff would be virtual.
No No's:
fflush (stdin); gets (); void main ();
Goodies:
Example of fgets (); The FAQ, C/C++ Reference
My Gear:
OS - Windows XP
IDE - MS Visual C++ 2008 Express Edition
ASCII stupid question, get a stupid ANSI
Yeah I thought of that...thanks...I will give it a go then post the code.
Here is my rather strip down bank account class:
Code:#include <iostream.h> using namespace std; class Account { public: float GetInterest () const { return itsinterest; }; float SetInterest (float interest) { itsinterest = interest; }; virtual float CalcInterest (float balance) { return balance/100 * 4.5; }; private: float itsinterest; }; class Saver : public Account { public : float CalcInterest (float balance) { return balance/100 * 6.5; }; }; int main() { float amount; float intamount; Account *pAccount; int choice; float balance; cout << "So you would like a new bank account!\n\n"; cout << "(1) Normal Account (2) Saver Account\n\n"; cout << "Make Your Choice: "; cin >> choice; switch (choice) { case 1: pAccount = new Account; break; case 2: pAccount = new Saver; break; default: return 1; } cout << "\nPlease Enter Your Balance: " << char(156); cin >> balance; intamount = pAccount->CalcInterest(balance); pAccount->SetInterest(intamount); amount = pAccount->GetInterest(); cout << "\nYour Interest Would Be " << char(156) << amount; balance+=amount; cout<< "\n\nSo Your Balance After Annum1 Would Be " << char(156) << balance; cin.ignore(); cin.get(); return 0; }
That is basically the idea. You should use double instead of float and <iostream> instead of <iostream.h>, though. Also, you should make the balance a member of the Account class.
Ok thanks...I keep putting iostream.h...dammit...its cos my compiler always puts it in at teh start.
What is a double?
/noobish question
A double is the default type for floating point values. There is float, double, and long double. You use double unless you have a reason not to, it has a higher precision than plain float.
You can probably change your compiler's settings to put <iostream> instead of <iostream.h>.
Could you also show me my code with balance in the Account class?
Pleasey Pleasey
You could include private variable for PIN and account number,
account holders name and address, you can make a number
of derived classes that, also the account's interest rate should
be part of the class. There's a lot you can do to expand on this
idea, you can make it as detailed as you like - you could even
develop an account that allows you to represent an actual bank
account, so that lodgements and withdrawals are possible, the
"date" and "time" of these transactions could be stored in a file.
No No's:
fflush (stdin); gets (); void main ();
Goodies:
Example of fgets (); The FAQ, C/C++ Reference
My Gear:
OS - Windows XP
IDE - MS Visual C++ 2008 Express Edition
ASCII stupid question, get a stupid ANSI
although you may want to put these under protected insteadCode:private: double itsinterest; double rate; double accbalance;
of private - that way derived classes will be able to have the
same basic properties - thats what inheritance is about
No No's:
fflush (stdin); gets (); void main ();
Goodies:
Example of fgets (); The FAQ, C/C++ Reference
My Gear:
OS - Windows XP
IDE - MS Visual C++ 2008 Express Edition
ASCII stupid question, get a stupid ANSI
>> Could you also show me my code with balance in the Account class?
In your classes, everywhere you see "interest" change it to "balance". Now change CalcBalance back to CalcInterest, but remove the balance parameter (so the function has no parameters). Do that in both classes. Now, inside the code for CalcInterest, instead of using balance, use GetBalance().
finally, in main, change the SetInterest and GetInterest calls to SetBalance and GetBalance, and of course pass in the balance instead of the interest. Then output the result of CalcInterest.
Follow that step by step, and your example will be a better use of classes, because you will be encapsulating the balance property of the Account and using the class properties to calculate the interest.
>> although you may want to put these under protected instead of private - that way derived classes will be able to have the same basic properties - thats what inheritance is about
They should really be private. That's what encapsulation is about. Derived classes can still use those values if you expose them as needed through an appropriate interface.
Is this right:
I also changed all my floats to double.Code:#include <iostream> using namespace std; class Account { public: double GetBalance () const { return itsbalance; }; double SetBalance (double balance) { itsbalance = balance; }; virtual float CalcInterest () { double balance2 = GetBalance(); return balance2/100 * 5; }; private: double itsbalance; }; class Saver : public Account { public : float CalcInterest () { float balance2 = GetBalance(); return balance2/100 * 6.5; }; }; int main() { double amount; Account *pAccount; int choice; double balance; cout << "So you would like a new bank account!\n\n"; cout << "(1) Normal Account (2) Saver Account\n\n"; cout << "Make Your Choice: "; cin >> choice; switch (choice) { case 1: pAccount = new Account; break; case 2: pAccount = new Saver; break; default: return 1; } cout << "\nPlease Enter Your Balance: " << char(156); cin >> balance; pAccount->SetBalance(balance); pAccount->GetBalance(); amount = pAccount->CalcInterest(); cout << "\nYour Interest Would Be " << char(156) << amount; balance+=amount; cout<< "\n\nSo Your Balance After Annum1 Would Be " << char(156) << balance; cin.ignore(); cin.get(); return 0; }
Edit: Damn iostream.h
Just changed my environment options so it doesn't do that.