I saw a code in sololearn platform(C++ course) and it is about defining overloading for operator +. you can see the code below:
#include <iostream>
using namespace std;
class Account {
private:
int balance=0;
int interest=0;
public:
Account() {}
Account(int a): balance(a)
{
interest += balance/10;
}
int getTotal() {
return balance+interest;
}
Account operator+(Account &obj){
Account OBJ2;
OBJ2.balance=this->balance + obj.balance;
OBJ2.interest=this->interest +obj.interest;
return OBJ2;
}
};
int main() {
int n1, n2;
cin >> n1 >> n2;
Account a(n1);
Account b(n2);
Account res = a+b;
cout << res.getTotal();
}
My question is: why do we need to keep two constructors in class "Account"? I don't understand the reason of having the default constructor( the first one without any parameter). I tried to delete it but then the code does not work.
Thank you