Exercise

This is a discussion on Exercise within the C++ Programming forums, part of the General Programming Boards category; Could somebody give me an exercise to do that envolves simple classes with maybe a bit of inheritance please, as ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    545

    Exercise

    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.

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    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

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Any way a few virtual functions could be put in?

  4. #4
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    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

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Yeah I thought of that...thanks...I will give it a go then post the code.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    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;
    }

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    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.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    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

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    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>.

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Could you also show me my code with balance in the Account class?

    Pleasey Pleasey

  11. #11
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    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

  12. #12
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Code:
    private:
    
    double itsinterest;
    double rate;
    double accbalance;
    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
    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

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> 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.

  14. #14
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Is this right:
    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;
    }
    I also changed all my floats to double.

    Edit: Damn iostream.h

  15. #15
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Just changed my environment options so it doesn't do that.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. programming exercise
    By mashour06 in forum C Programming
    Replies: 1
    Last Post: 06-01-2009, 06:22 AM
  2. Line of data input method
    By larry_2k4 in forum C Programming
    Replies: 2
    Last Post: 04-28-2009, 11:34 PM
  3. Can I get some idea for exercise?
    By Rokemet in forum C++ Programming
    Replies: 6
    Last Post: 11-22-2007, 08:10 AM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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