operator overloading problem

+1 Aman Sepat · June 25, 2015
#include <iostream>

using namespace std;

void amount(float princ, int time, float rate)
    cout << "\nPrincipal Amount:"<<princ;
    cout << "\tTime:"<<time;
    cout << "\tRate:"<<rate;
    cout << "\n Amount:"<<(princ*time*rate)<< "\n" <<endl;

void amount(float princ, int time){
    cout << "\nPrincipal Amount:"<<princ;
    cout << "\tTime:"<<time;
    cout << "\tRate:0.08";
    cout << "\n Amount:"<<(princ*time*0.08)<< "\n" <<endl;

void amount (float princ, float rate){
    cout << "\nPrincipal Amount:"<<princ;
    cout << "\tTime:2 years";
    cout << "\tRate:"<<rate;
    cout << "\n Amount:"<<(princ*2*rate)<< "\n" <<endl;

void amount(int time, float rate){
    cout << "\nPrincipal Amount:2000";
    cout << "\tTime:"<<time;
    cout << "\tRate:"<<rate;
    cout << "\n Amount:"<<(2000*time*rate)<< "\n" <<endl;

void amount(float princ){
    cout << "\nPrincipal Amount:"<<princ;
    cout << "\tTime:2years";
    cout << "\tRate:  0.08";
    cout << "\n Amount:"<<(princ*2*0.08)<< "\n" <<endl;
int main()
    cout << "case3";
    amount(6.054, 0.07);

    return 0;

why is the code not working with case 3 and not matching with third amount functions with both values float

Post a Reply


Oldest  Newest  Rating
0 An Ho · June 29, 2015
Hi there :),

One thing that you can do is to change all the "float" to "double" type in your functions. In this case, you have 3 overloaded functions: amount (float, int), amount (int, float) and amount (float, float) that if given the values of the variables, they will take every single value in as they please. For example, if you pass the values (6.054, 0.07) in, the program will use all 3 functions at once, because even though you pass in a decimal, it can still get assigned to an int value e.g. 0.07 can be truncated into 0, an integer, and 6.054 can be truncated into 6, an integer. So the safe way is to use double, not float. Otherwise, you would get some error messages like "call of overloaded function is ambiguous" and the compiler will list out the functions in some way such as "Candidates are blah blah blah". Double is also more useful than float because it has greater precision.
This is my opinion, it may or may not work, but I hope it does solve your problem.
0 ashiq sultan · July 1, 2015
always try to paste the codes inside the code box provided.
  • 1



Used in many types of software including music players, video games, and many large scale applications.

Bucky Roberts Administrator