i wnt to know the use of constructor initialization to 0(zero) as in the following program
What I have tried:
when I run the following program, sa is always 0 which should not be
class bank
{
private:
double a,sa;
char ac,c,s;
public:
void deposit(double amount, char account);
};
void bank::deposit(double amount, char account)
{
a=amount,
ac=account;
if (ac=='s')
{
sa=sa+a;
cout<<sa;
}
}
main()
{
char account;
double amount;
clrscr();
bank b;
cout<<"enter account type: ";
cin>>account;
cout<<"enter amount:" ;
cin>>amount;
b.deposit(amount, account);
getch();
}
but when i add
as in the following program, it shows correct sa i.e. the following program shows the correct result.
class bank
{
private:
double a,sa;
char ac,c,s;
public:
bank()
{
sa=0;
}
void deposit(double amount, char account);
};
void bank::deposit(double amount, char account)
{
a=amount,
ac=account;
if (ac=='s')
{
sa=sa+a;
cout<<sa;
}
}
main()
{
char account;
double amount;
clrscr();
bank b;
cout<<"enter account type: ";
cin>>account;
cout<<"enter amount:" ;
cin>>amount;
b.deposit(amount, account);
getch();
}
so,
can anyone enlighten me, what is the use of:-
bank()