Once you get it compiling I suggest some changes:
- Balance should be a member of class two. Initialize it in the constructor.
- balance() should print the remaining balance, not simply "1000.00"
- Deposit() and Withdrawal() should not print the balance. It's always good to separate "computation" and "presentation". Instead, call two.balance() in main() after calling two.Deposit() and two.Withdrawal().
The compiler creates a default constructor, but only if you don't create any other constructors. In this case you have a constructor that takes the balance and amount.
The easiest way to fix this is to give default arguments to the constructor that you have:
Note also that I've changed the names of the arguments. You were using the Balance and amount, the same names as the class members. as a result the arguments hid the same-named class members and the constructor didn't initialize the class at all.
#include <iostream>
usingnamespace std;
double Balance;
double amount;
class one{
public:
one();
void balance();
virtualdouble Deposit(double amount);
virtualdouble Withdrawl(double amount);
};
class two: public one{
public:
two(double b=0.0, double a=0.0){
Balance =b;
amount = a;
};
void checkBalance();
double Deposit(double);
double Withdrawl(double);
};
void two::checkBalance(){
cout <<"Balance :: $1000.00/n";
};
double two::Deposit(double amount){
double Balance;
Balance = Balance + amount;
return Balance;
};
double two::Withdrawl(double amount){
double Balance;
Balance = Balance - amount;
return Balance;
};
int main(){
int choice;
double amount;
two obj;
cout << "Wlecome to the ATM/n";
cout << "What would you like to do/n";
cout << "1. Check Balance./n";
cout << "2. Deposit./n";
cout << "3. Withdrawl./n";
cout << "4. Exit./n";
if (choice == 1){
obj.checkBalance();
}
elseif(choice ==2){
cout << "How much would you like to deposit?/n";
cin >> amount;
cout << "New Balance: " << obj.Deposit(amount);
}
elseif (choice ==3){
cout << "How much would you like to deposit?/n";
cin >> amount;
cout << "New Balance: " << obj.Withdrawl(amount);
}
elseif (choice ==4){
cout << "Goodbye!/n";
}
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
In member function 'virtual double two::Deposit(double)': 32:31: warning:
'Balance' is used uninitialized in this function [-Wuninitialized] In member
function 'virtual double two::Withdrawl(double)': 38:31: warning: 'Balance' is
used uninitialized in this function [-Wuninitialized] In function 'int main()': 55:5:
warning: 'choice' may be used uninitialized in this function [-Wuninitialized]
/tmp/ccCxwXwM.o: In function `two::two(double, double)': :
(.text._ZN3twoC2Edd[_ZN3twoC5Edd]+0x1e): undefined reference to
`one::one()' /tmp/ccCxwXwM.o:(.rodata._ZTI3two[_ZTI3two]+0x10): undefined
reference to `typeinfo for one' collect2: error: ld returned 1 exit status
remove lines 31 and 37. They define a local variable that hides the class member Balance. The compiler is complaining because you use this local variable before initializing it.
At line 55 you use choice, but you never read it from the user. After line 53 you should probably have cin >> choice;.
As for the last error message, since two is derived from one, when you construct a two object, it also constructs the one that is its base class. You have declared a one() constructor at line 9 so that's the one it calls, but the constructor isn't defined. So you have an "undefined reference to one::one()."
To get rid of this, just delete line 9 and let the compiler generate a default constructor.
This brings up an the question of why you have the one class in the first place. It doesn't do anything at all.
#include <iostream>
usingnamespace std;
double Balance;
double amount;
class one{
public:
void balance();
virtualdouble Deposit(double amount);
virtualdouble Withdrawl(double amount);
};
class two: public one{
public:
two(double b=0.0, double a=0.0){
Balance =b;
amount = a;
};
void checkBalance();
double Deposit(double amount);
double Withdrawl(double amount);
};
void two::checkBalance(){
cout <<"Balance :: $1000.00/n";
};
double two::Deposit(double amount){
Balance = Balance + amount;
return Balance;
};
double two::Withdrawl(double amount){
Balance = Balance - amount;
return Balance;
};
int main(){
int choice;
double amount;
two obj;
cout << "Wlecome to the ATM/n";
cout << "What would you like to do/n";
cout << "1. Check Balance./n";
cout << "2. Deposit./n";
cout << "3. Withdrawl./n";
cout << "4. Exit./n";
cin >> choice;
if (choice == 1){
obj.checkBalance();
}
elseif(choice ==2){
cout << "How much would you like to deposit?/n";
cin >> amount;
cout << "New Balance: " << obj.Deposit(amount);
}
elseif (choice ==3){
cout << "How much would you like to deposit?/n";
cin >> amount;
cout << "New Balance: " << obj.Withdrawl(amount);
}
elseif (choice ==4){
cout << "Goodbye!/n";
}
return 0;
}
1 2 3 4 5 6 7
/tmp/ccm9diVF.o: In function `one::one()': :
(.text._ZN3oneC2Ev[_ZN3oneC5Ev]+0xf): undefined reference to `vtable for
one' /tmp/ccm9diVF.o:(.rodata._ZTI3two[_ZTI3two]+0x10): undefined reference
to `typeinfo for one' collect2: error: ld returned 1 exit status