Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ overloaded operators help

Options
  • 13-05-2008 10:33am
    #1
    Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,098 Mod ✭✭✭✭


    Here is the question that I have below:
    Prototype and implement a BankLoan class which has:
    The following member data:
    (1) initial loan amount,
    (2) year in which the loan was taken out,
    (3) amount paid off the loan to date;
    The following public member functions:
    (1) to pay an amount of money off the loan,
    (2) to enquire the current balance - this function requires as input the current year, and should apply interest at 10% per year that has elapsed since the loan was taken out (3) to setup a new loan

    This works fine. Now I want to do this:
    Overload the + (plus) operator to pay money off the loan, and the << (stream output) operator to print out the current balance.
    I'm not sure how to do this, any help appreciated.
    #include <iostream>
    using namespace std;
    
    class BankLoan
    {
          private:
                  float initial;
                  int year;
                  float paid;
          public:
                  void pay (float amount);
                  float enquire (int yearnow);
                  void setup (float amount, int yearnow);
    };
    
    
    
    int main()
    {
         BankLoan b;
         int yr, choice = 0;
         float amt;
         BankLoan balance;
          
         cout<<"\nBankLoan Program";
         
         while (choice != 4)
         {
               cout<<"\n\n1. New Bank Loan";
               cout<<"\n2. Pay into Loan";
               cout<<"\n3. Enquire Balance";
               cout<<"\n4. Exit";
               cout<<"\nWhich option do you want: ";
               
               do 
               {
                           cin>>choice;
               }
               while (choice <1 || choice > 4);
               switch (choice)
               {
                      case 1:
                           cout<<"\nEnter Year and Initial Amount: ";
                           cin>>yr>>amt;
                           b.setup(amt, yr);
                           break;
                      case 2:
                           cout<<"\nAmount to pay off? ";
                           cin>>amt;
                           b.pay(amt);
                           break;
                      case 3:
                           cout<<"Enter current year: ";
                           cin>>yr;
                           cout<<"Including interest, balance is now "<<b.enquire(yr);
                           break;
                      case 4:
                           cout<<"\n\nBye!!";
               }
         }
    }
    
    void BankLoan::pay(float amount)
    {
         paid += amount;
    }
    
    float BankLoan::enquire( int yearnow)
    {
          int i;
          float total = initial;
          for (i=year; i<yearnow; i++)
              total *= 1.1;
          return total-paid;
    }
    
    void BankLoan::setup(float amount, int yearnow)
    {
          initial = amount;
          year = yearnow;
          paid = 0.0;
    }
    


    I know how to do overloaded functions generally, ie one to concatenate strings below but I can't seem to get the program working.

    friend mystring operator+(const mystring& a, const mystring& b) at the top then....

    mystring operator+(const mystring& a, const mystring& b)
    {
    mystring tmp;
    if (a.len + b.len < max_len)
    {
    strcpy(tmp.s, a.s);
    strcat(tmp.s, b.s);
    tmp.len = a.len + b.len;
    }
    else
    {
    cerr << "\nERROR: Max length exceeded
    in concatenation in operator+() -bye!\n\n";
    exit(1);
    }
    return tmp;
    }
    


Comments

  • Registered Users Posts: 981 ✭✭✭fasty


    Here's an example I put together for you.
    class OverloadTest
    {
    public:
       OverloadTest(int num);
       OverloadTest& operator+(int numbertoadd);
       int GetNumber();
    private:
       int number;
    };
    
    OverloadTest::OverloadTest(int num) 
    {
       number = num;
    }
    
    int OverloadTest::GetNumber() 
    {
       return number;
    }
    
    OverloadTest& OverloadTest::operator +(int numbertoadd)
    {
       number += numbertoadd;
       return *this;
    }
    

    Or for your class...
    BankLoan& operator+(float amount);
    

    in the source
    BankLoan& BankLoan::operator +(float amount)
    {
       pay(amount);
       return *this;
    }
    

    Overloading + to to pay money off a loan, i.e. subtract money from the outstanding balance, is a poor way of using operator overloading. I strongly suggest you overload the minus operator for this instead.


  • Registered Users Posts: 2,379 ✭✭✭toiletduck


    mystring operator+(const mystring& a, const mystring& b)
    {
    [B]mystring tmp;[/B]
    if (a.len + b.len < max_len)
    {
    strcpy(tmp.s, a.s);
    strcat(tmp.s, b.s);
    tmp.len = a.len + b.len;
    }
    else
    {
    cerr << "\nERROR: Max length exceeded
    in concatenation in operator+() -bye!\n\n";
    exit(1);
    }
    return tmp;
    }
    

    I'd be inclined to have a "static mystring tmp" there. And what did you think of that exam yesterday? :pac:


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,098 Mod ✭✭✭✭Tar.Aldarion


    Thanks guys. Did the exam with this. ;)

    Went very well toiletduck, had covered it all, and for you?
    ou had vb too?


  • Registered Users Posts: 2,379 ✭✭✭toiletduck


    Went very well toiletduck, had covered it all, and for you?
    ou had vb too?

    Yup, the C++ part was great but VB.... well it's hard to remember all the methods and functions of it! Overall happy though.


  • Registered Users Posts: 1,435 ✭✭✭iUseVi


    toiletduck wrote: »
    Yup, the C++ part was great but VB.... well it's hard to remember all the methods and functions of it! Overall happy though.

    VB? You poor poor people.


  • Advertisement
  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    iUseVi wrote: »
    VB? You poor poor people.

    Heh. I suppose it was VB6. Bit legacy at this stage is it not? I'm amazed they're still teaching it, I would have thought .Net would have overtaken it and they would teach C# at that.


  • Registered Users Posts: 2,379 ✭✭✭toiletduck


    Evil Phil wrote: »
    Heh. I suppose it was VB6. Bit legacy at this stage is it not? I'm amazed they're still teaching it, I would have thought .Net would have overtaken it and they would teach C# at that.

    Nope t'was VB.NET we were doing. It must be the most frustrating language to try and get down on paper!

    Oh well, it's done and dusted hopefully never to raise its head again...


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Ugh, and on paper! C# is a much more elegant language. Have a look at that if you're looking for something to do. It very like java so if you've done java you'll get up to speed suprisingly quick. Even with you C++ knowledge you've covered a lot.


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,098 Mod ✭✭✭✭Tar.Aldarion


    I liked not having to answer the vb section. :)


Advertisement