Inheritence problem

Program isnt compiling. I think its a problem with my calls to two.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
using namespace std;

double Balance = 1000;
double amount;

class one{
public:
    one();
    void balance();
    virtual double Deposit(double amount);
    virtual double Withdrawl(double amount);
};

class two: public one{
public:
    two();
    void balance(){
        cout <<"Balance :: $1000.00/n";
    }
    double Deposit(double amount){
        Balance = Balance + amount;
        cout << "New Balance: " << Balance;
        return Balance;
    }
    double Withdrawl(double amount){
        Balance = Balance - amount;
        cout << "New Balance: " << Balance;
        return Balance;
    }
};

int main(){
    
    int choice;
    two two;
    
    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){
        two.balance();
    }
    else if(choice ==2){
        cout << "How much would you like to deposit?/n";
        cin >> amount;
        two.Deposit(amount);
    }
    else if (choice ==3){
        cout << "How much would you like to deposit?/n";
        cin >> amount;
        two.Withdrawl(amount);
    }
    else if (choice ==4){
        cout << "Goodbye!/n";
    }
    
    return 0;
}
Program isnt compiling.
Is there any errors? If so, post them here.
I think its a problem with my calls to two.
Why do you think that? What kind of problem you are talking about?
OP, here's a hint..
if (choice == 1)

what do you think the value of 'choice' is when the code gets to this line?
Last edited on
On line 9/17: you did neither implement the constructor on() nor two()

for choice you need a cin
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().
I changed it but its now saying no matching constructor for initialization of two
line 43.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
using namespace std;

class one{
public:
    one();
    void balance();
    virtual double Deposit(double amount);
    virtual double Withdrawl(double amount);
};

class two: public one{
public:
    two(double Balance, double amount){
        Balance =1000;
        amount = 0;
    };
    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();
    }
    else if(choice ==2){
        cout << "How much would you like to deposit?/n";
        cin >> amount;
        cout << "New Balance: " << obj.Balance(amount);
    }
    else if (choice ==3){
        cout << "How much would you like to deposit?/n";
        cin >> amount;
        cout << "New Balance: " << obj.Withdrawl(amount);
    }
    else if (choice ==4){
        cout << "Goodbye!/n";
    }
    
    return 0;
}
Last edited on
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:
1
2
3
4
    two(double b=0.0, double a=0.0){
        Balance = b;
        amount = a;
    };

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.
I made the changes but am still getting errors. Im uploading the code and error message. Im using the compiler c++ shell from the website.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
using namespace std;

double Balance;
double amount;

class one{
public:
    one();
    void balance();
    virtual double Deposit(double amount);
    virtual double 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();
    }
    else if(choice ==2){
        cout << "How much would you like to deposit?/n";
        cin >> amount;
        cout << "New Balance: " << obj.Deposit(amount);
    }
    else if (choice ==3){
        cout << "How much would you like to deposit?/n";
        cin >> amount;
        cout << "New Balance: " << obj.Withdrawl(amount);
    }
    else if (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.
I made those changes and get another error about type info. One of my problems is understanding what the errors mean. This is what i got
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
using namespace std;

double Balance;
double amount;

class one{
public:
    void balance();
    virtual double Deposit(double amount);
    virtual double 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();
    }
    else if(choice ==2){
        cout << "How much would you like to deposit?/n";
        cin >> amount;
        cout << "New Balance: " << obj.Deposit(amount);
    }
    else if (choice ==3){
        cout << "How much would you like to deposit?/n";
        cin >> amount;
        cout << "New Balance: " << obj.Withdrawl(amount);
    }
    else if (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 
I figured it out. I needed to add a "=0" at the end of line 10 and 11. Thanks for the help.
Topic archived. No new replies allowed.