Why I am getting c=1;

Why I am getting c=1,, When c was in base class .
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
#include<iostream>
using namespace std;

class A
     {
       public:
                  int a;
                  int b;
                  int c;
        public:
            int  add (int x , int y)
                 {
                    a=x;
                    b=y;
                    c=a+b;
                    return c;
                       }
                           };
class B : public A
 {
    public:
             int third, total;
    public:
             void three()
                  {
                   cout << "\nNote I have c = " << c;
                   cout <<"\nEnter third digit= ";
                   cin >> third;
                    total = c+third;
                       cout << "\nSum of three digit= " <<total << "\n\n";
                               }

                                 };

int main()
{
int fi,se,sum,choice;
A two_digit;
B three_digit;

cout << "Enter First Digit= ";
cin >> fi;
cout << "Enter Second Digit= ";
cin >> se;

cout << "\nPress 1 to add to digits \n Press 2 to enter three digit\n ";
cin >> choice;

switch (choice)
     {
        case 1:
         
          sum=two_digit.add(fi,se);
            cout << "\nTwo digit Sum= "  << sum;
              break;
  
       case 2 :
          
            three_digit.three();
            break;
            
       default:
               cout << "\nENTER PROPER CHOICE \n";
                  }

return 0;
     }

What is the logic of Inheritance which I am missing out ??
You have two different objects here: two_digit and three_digit.

Each object has their own set of variables. That is... two_digit has its own copies of 'a', 'b' and 'c'.

three_digit, being a separate object, has a different set of variables 'a', 'b', and 'c'... also because it's a 'B' object it also has 'third' and 'total' variables.

Changes made to two_digit's 'a' variable do not change three_digit's 'a' variable because they are different variables.

Therefore... nowhere in this code do you set three_digit's values for 'a', 'b', or 'c'. So any values you read from those vars will be garbage because they haven't been initialized.
closed account (28poGNh0)
First off all ,In this case ,you dont need to add two publics inside your classes.

Second of all,You should notice that after taking the first and the second digit your program expects to get a choice and if you choose the choice 2 the base class will never get the value fi and si so never expect c = something

allthough if you realy incest to get the the value of c add this line three_digit.add(fi,se); at 58th line of your code or as I did in mine

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
# include <iostream>
using namespace std;

class A
{
    public:
        int a,b,c;
        int  add (int x , int y)
        {
            a=x;
            b=y;
            c=a+b;
            return c;
        }
};

class B : public A
{
    public:
        int third, total;
        void three()
        {
            cout << "\nNote I have c = " << c;
            cout <<"\nEnter third digit= ";
            cin >> third;
            total = c+third;
            cout << "\nSum of three digit= " <<total << "\n\n";
        }
};

int main()
{
    int fi,se,sum,choice;
    A two_digit;
    B three_digit;

    cout << "Enter First Digit= ";
    cin >> fi;
    cout << "Enter Second Digit= ";
    cin >> se;

    cout << "\nPress 1 to add to digits \n Press 2 to enter three digit\n ";
    cin >> choice;

    switch (choice)
    {
        case 1:
            sum=two_digit.add(fi,se);
            cout << "\nTwo digit Sum= "  << sum;
        break;
        case 2 :
            three_digit.add(fi,se);
            three_digit.three();
        break;
        default:
            cout << "\nENTER PROPER CHOICE \n";
    }

    return 0;
}


What is the logic of Inheritance

I said you inherit the function add(,); of the base class in this exemple

Finaly if you want to get the values from the base class maybe the better way to do it is by using constructors.

hope it helps
Topic archived. No new replies allowed.