0

You can see in main Func im passing different objects at function, which is overloaded and have different kind of objects, but when i run same account transfer function only run, anyone can guide what im doing wrong, whats need to be done.
Easy words there are 3 functions made in class ACI Transfer(double balance, class object) im trying to run all these 3 functions by passing their respected object

#include<iostream>
using namespace std;

class Account {
    private:    
        double balance;
    public:
        Account(){}
        Account(double balance){ this->balance=balance; }
        void Deposit(double balance){
            cout<<endl<<"Account()"<<endl;
            this->balance=balance;
        }
        void WithDraw(double balance)
        {
            cout<<endl<<"Account()"<<endl;
            if(this->balance>balance){
                this->balance=this->balance-balance;
            }
            else cout<<endl<<"Error"<<endl;
        }
        void CheckBalance(void)
        {
            cout<<"Balance: "<<balance;
        }
        void setBalance(double balance) { this->balance=balance; }
        double getBalance(){ return balance; }
};

class InterestAccount : public Account {
    private:
        double Interest;
    public:
        InterestAccount(double Interest,double balance) : Account(balance){ this->Interest=Interest; }
        InterestAccount(){ Interest=0.30; }
        void Deposit(double balance)
        {
            cout<<endl<<"InterestAcc()"<<endl;
            balance=balance*Interest;
            setBalance(balance);
        }
};
    
class ChargingAccount :  public Account{
    private:
        double fee;
    public:
        ChargingAccount(){}
        ChargingAccount(double fee,double balance) : Account(balance) { this->fee=fee; }
        void WithDraw(double balance){
            cout<<endl<<"ChargingAcc()"<<endl;
            if(getBalance()+3>balance){
                balance=getBalance()-balance;
                setBalance(balance);
            }
            else cout<<endl<<" ERROR "<<endl;
        }
};

class ACI :   public InterestAccount, public ChargingAccount
{   
    public:
        ACI(){
        }
        ACI(double fee,double balance,double Interest) : InterestAccount(Interest,balance), ChargingAccount(fee,balance) { }
        void transfer(double balance,Account ACC){
            cout<<"Account";
            ACC.setBalance(balance);
        }
        void transfer(double balance,double fee,InterestAccount IG){
            cout<<"InterestAcc";
            IG.setBalance(balance); 
        }
        void transfer(double balance,double fee,ChargingAccount CG){
            cout<<"ChargingACC OBJ";
            CG.setBalance(balance);
        }
        
};

int main()
{
    double balance=10000;
//  cout<<"Enter Balance to transfer: ";
//  cin>>balance;
    ChargingAccount CG;
    InterestAccount IR;
    ACI obj;
    obj.transfer(balance,CG);
    obj.transfer(balance,IR);
}
        
2
  • Any overload except for the first one takes 2 double parameters as the first 2 parameters and a total of 3 parameters, none of them defaulted. None of those are viable options for the parameters you pass. Commented Jul 24, 2022 at 16:46
  • You claim there are three functions of the form transfer(double balance, SomeClass object), but in fact there is only one function of that form. Commented Jul 24, 2022 at 16:47

1 Answer 1

1

trying to run all these 3 functions by passing their respected object

The other two overloaded functions have 3 parameters but you're passing only two arguments when calling the function transfer. Thus, the one ACI::transfer(double ,Account) with 2 parameters is the only viable candidate and will be called both times.

To solve this you've to pass exactly 3 arguments so that the respective functions can be called as shown below:

int main()
{
    double balance=10000;
//  cout<<"Enter Balance to transfer: ";
//  cin>>balance;
    ChargingAccount CG;
    InterestAccount IR;
    ACI obj;
//------------------------v----->pass some number here, this calls ACI::transfer(double, double, ChargingAccount)
    obj.transfer(balance, 5, CG);
//------------------------v----->pass some number here, this calls ACI::transfer(double, double, InterestAccount)
    obj.transfer(balance, 6, IR);
}

Demo

Now the output will be:

ChargingACC OBJInterestAcc

Method 2

You can also solve this by removing the second parameter from the 2nd and 3rd overload as shown below:

class ACI :   public InterestAccount, public ChargingAccount
{   
    public:
        ACI(){
        }
        ACI(double fee,double balance,double Interest) : InterestAccount(Interest,balance), ChargingAccount(fee,balance) { }
        void transfer(double balance,Account ACC){
            cout<<"Account";
            ACC.setBalance(balance);
        }
//-----------------------------------v----------------------->removed 2nd parameter from here
        void transfer(double balance,InterestAccount IG){
            cout<<"InterestAcc";
            IG.setBalance(balance); 
        }
//-----------------------------------v----------------------->removed 2nd parameter from here
        void transfer(double balance,ChargingAccount CG){
            cout<<"ChargingACC OBJ";
            CG.setBalance(balance);
        }
        
};

Demo

Sign up to request clarification or add additional context in comments.

3 Comments

thats really weird behaviour, why do we want to pass three arguments, it actually fixed the issue btw thank you.
@mrc Note carefully, the 2nd and 3rd overload in your program has 3 parameters. That is why to call them we must pass 3 arguments. You're welcome.
@mrc There is another way to solve this. You can just remove the 2nd parameter named fee from the 2nd and 3rd overloaded functions. And then you'll just have to pass 2 arguments instead of 3. See method 2 of my updated answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.