Thursday, December 8, 2011

Function with default arguments: Demo

#include 
using namespace std;

class SI
{
   float pple;
   float rate;
   float n;

   public:
      // Function with default arguments. (must be from right to left)
      void SetInput(float , float n = 3, float rate = 10);

      float  CalculateSI();
};


//
//   setInput: Function definition 
//   You may not write default argument values in definition
//
void SI::SetInput(float pple, float n, float rate)
{

//   This line is to test whether this contains address of caller object or not
//   cout << "this : <"<< this << ">"<< endl;

//
//  "this" is necessary here to resolve ambiguity,
//  Our class has the data members pple, rate, n and
//  we have the very same names as the parameters to this function
//
    this->pple = pple;
    this->rate = rate;
    this->n = n;
}

float SI::CalculateSI()
{
  return (pple *rate * n) /100;
}

int main()
{
  SI s1;
  s1.SetInput(1000,1);
  cout << endl << "\t Simple intereset = "<< s1.CalculateSI()<< endl;

//  This is to test whether "this" contains address of the caller object or not.
//  cout << "Address of s1 : <"<< &s1 << ">" << endl;

  SI s2;
  s2.SetInput(2000,4);
  cout << endl << "\t Simple intereset = "<< s2.CalculateSI()<< endl;

  SI s3;
  s3.SetInput(5000,3.5);
  cout << endl << "\t Simple intereset = "<< s3.CalculateSI()<< endl;

  SI s4;
  s4.SetInput(4538,9.32,5.7);
  cout << endl << "\t Simple intereset = "<< s4.CalculateSI()<< endl;

  SI s5;
  s5.SetInput(5000);
  cout << endl << "\t Simple intereset = "<< s5.CalculateSI()<< endl;

  return 0;
}

No comments:

Post a Comment