implementing operators

This is a discussion on implementing operators within the C++ Programming forums, part of the General Programming Boards category; i need guidance here please. i need to implement the +, = , == operators on my savingsaccount class. i ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    4

    implementing operators

    i need guidance here please. i need to implement the +, = , == operators on my savingsaccount class. i really dont know where to start and any guidance will really be appreciated. here are my codes:

    //SavingsAccount.h
    [tag]
    Code:
    #ifndef SAVINGSACCOUNT_H
    #define SAVINGSACCOUNT_H
    
    class SavingsAccount
    {
         public:
            SavingsAccount(double x)
      {
            savingsBalance = x >= 0 ? x:0;
      }
         void calculateMonthlyInterest();
         static void modifyInterestRate(double);
         void printoutBalance() const;
    
         private:
            double savingsBalance;
            static double annualInterestRate;
    
    };
    [/tag]

    //SavingsAccount.cpp
    [tag]
    Code:
    #include <iostream>
    #include <iomanip>
    #include "SavingsAccount.h"
    
    double SavingsAccount::annualInterestRate = 0;
    
    void SavingsAccount::calculateMonthlyInterest()
    { 
         savingsBalance += savingsBa lance * (annualInterestRate / 2.0);
    }
     
    void SavingsAccount::modifyInterestRate(double y)
    {
         annualInterestRate = (y >= 0  && y <= 1.0) ? y:0.03;
    }
    
    void SavingsAccount::printoutBalance() const
    {
         std::cout << '$' << savingsBalance << std::endl;
    }
    [/tag]

    //main.cpp
    [tag]
    Code:
    #include <iostream>
    #include <iomanip>
    #include "SavingsAccount.h"
    
    int main()
    {
         SavingsAccount saver1(2000.0), saver2(3000.0);
    
         SavingsAccount::modifyInterestRate(.03);
    
         std::cout << "Monthly balances with 3% annual interest"
                        << \ntBalances:\n\t\t\tSaver 1=>";
         saver1.printoutBalance();
    
         std::cout << "Saver 2=>";
         saver2.printoutBalance();
    
         for (int m = 1; m <= 12; ++m)
       {
            saver1.calculateMonthlyInterest();
            saver2.calculateMonthlyInterest();
    
            std::cout <<"\nMonth " << m << ":Saver 1=>";
            saver1.printoutBalance();
            std::cout << "Saver 2=>";
            saver2.printoutBalance();
        }
    
         SavingsAccount::modifyInterestRate(.04);
         saver1.calculateMonthlyInterest();
         saver2.calculateMonthlyInterest();
         std::cout << "\nBalances with 4% interest rate:"
                        << "\nBalances:\nSaver 1=>";
         saver1.printoutBalance();
         std::cout << "Saver 2=>";
         saver2.printoutBalance();
         std::cout << std::endl;
    
         return 0;
    }
    [/tag]
    thankyou very much in advance!!

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    85
    first I think you should at a public accesser function to return savingsBalance. then you can do something like:

    Code:
    void SavingsAccount::operator+ (SavingsAccount blah){
         return (savingsBalance+blah.getSavingsBalance() );
    }
    thats if you make it a member function, I'm sure some people will insist on overloaded operators to be non member or something. but you get the idea...

  3. #3
    Banned borko_b's Avatar
    Join Date
    Jun 2002
    Location
    Well... I live in Bulgaria :)
    Posts
    100
    Quote Originally Posted by qwertiop
    first I think you should at a public accesser function to return savingsBalance. then you can do something like:

    Code:
    void SavingsAccount::operator+ (SavingsAccount blah){
         return (savingsBalance+blah.getSavingsBalance() );
    }
    thats if you make it a member function, I'm sure some people will insist on overloaded operators to be non member or something. but you get the idea...
    this is Bad...
    look at the string class to see how its done ...
    it should be like this :

    Code:
    //file: SavingsAccount.h
    class SavingAccount
    { 
      //...
      SavingAccount operator + (SavingAccount& rAcc);
      bool operator ==  (const SavingAccount& rAcc);
      SavingAccount& operator = (const SavingAccount& rAcc);
      //...
    };
    ///:~
    
    //file:  SavingsAccount.cpp
    //...
    SavingAccount SavingAccount::operator + (SavingAccount& rAcc)
    {
      SavingAccount acc;
      //..do some stuff with (*this)
      //and return the result in acc so that the people could write:
      //
      // a1 = a2 + a3 + a4;
      //
    
      acc.savingsBalance = this->savingsBalance + rAcc.savingsBalance;
      return acc;
    }
    bool SavingAccount::operator ==  (const SavingAccount& rAcc)
    {
      bool bret = false;
      //do some stuff here 
      bret = this->savingsBalance == rAcc.savingsBalance;
      return bret;
    }
    SavingAccount& SavingAccount::operator = (const SavingAccount& rAcc)
    {
      //do some stuff for assignment
      this->savingsBalance = rAcc.savingsBalance;
      return *this;
    }
    //...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Operators in C++
    By Flecto in forum C++ Programming
    Replies: 4
    Last Post: 05-15-2009, 07:17 AM
  2. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 06:42 PM
  3. Overloading fstream's << and >> operators
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 03:17 AM
  4. operators???
    By arjunajay in forum C++ Programming
    Replies: 11
    Last Post: 06-25-2005, 04:37 AM
  5. Operators
    By George in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2003, 06:35 PM

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