Home > database >  how to overload function with different class object c
how to overload function with different class object c

Time:07-25

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);
}
        

CodePudding user response:

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

  •  Tags:  
  • c
  • Related