Good day!

I have started a little inheritance on my simple class that takes the addition function of two numbers on the other class but the sum is always zero even if it is not supposed to be. Here is my code so far:

#include<iostream>
using namespace std;

class Addition
{
      public:
             float num1, num2, sum;
             Addition(){};
             ~Addition(){};

};

class Subtraction: public Addition
{
      public:
             void ExecuteAddtion()
             {
                   sum=num1 + num2;
             }
             Subtraction(){};
             ~Subtraction(){};

};

Addition Addme;
Subtraction SubtractMe;

int main()
{
    cout<<"Enter first number: ";
    cin>>Addme.num1;
    cout<<"Enter second number: ";
    cin>>Addme.num2;
    SubtractMe.ExecuteAddtion();
    cout<<"The sum of "<<Addme.num1<<" and "<<Addme.num2<<" is "<<Addme.sum<<endl;
    system("pause");
    return 0;
}

Thank you for helping!

Thank you deceptikon!

The addition I beleive is executed but the sum result was not pass in Addition class sum variable. Even if Addition class is inherited as public? Where should I put the Addition function in a class?

The addition I beleive is executed but the sum result was not pass in Addition class sum variable.

Yup.

Even if Addition class is inherited as public?

I think inheritance might be confusing things, but you can see what's going on by removing that variable entirely. What happens if you use two Addition objects instead?

Addition Addme;
Addition Addme2;
int main()
{
    cout<<"Enter first number: ";
    cin>>Addme.num1;
    cout<<"Enter second number: ";
    cin>>Addme.num2;
    Addme2.ExecuteAddtion();
    cout<<"The sum of "<<Addme.num1<<" and "<<Addme.num2<<" is "<<Addme.sum<<endl;
    system("pause");
    return 0;
}

Thank you deceptikon!

removing that variable entirely.

This is confusing but I dont want to remove either one of the two class. The problem here is the function ExecuteAddtion is not passing the result to the main class variable sum.

Whats the best way to implement this?

Edited 4 Years Ago by blocker

The problem here is the function ExecuteAddtion is not passing the result to the main class variable sum.

No, the problem is that you're expecting the two completely separate objects to magically communicate just because their types are related. It's no different from creating two integers, incrementing one, and expecting the other to be incremented too even though you did nothing with it.

Comments
Quantum programming? It's real, isn't it?

Thank you deceptikon!

So what is the best place to put the void ExecuteAddtion() so it can pass the result to the main class?

I Agree to what decepticon said.

As the object you've created is for Addition class hence Addme.num1 and Addme.num2 has a seperate box (group) for storing values (which is different from Subtractme.num1 and Subtractme.num2). Hence, the sum will be given by Addme.ExecuteAddition() because you've provided values for Addme.num1 and num2. Subtractme.ExecuteAddition() will give you a garbage value as you've not provided values of Subtractme.num1 & Subtractme.num2

Subtraction is derived from Addition so it can just USE the variables of Addition class. It doesn't transfer the values.

Thank you Vish0203!
Is almost clear to me now..But how can I use the value of class Addition variable num1,num2 to execute ExecuteAddition() in class Subtraction?

But how can I use the value of class Addition variable num1,num2 to execute ExecuteAddition() in class Subtraction?

Firstly, your design is non-intuitive...

Ideally, you should have a class Calculator with public member functions add(), subtract() etc.

Within that class at the top you would declare a protected variable which is what you would set an initial value to.

So something like:

Calculator test;
test.add(10);
test.subtract(3);

test.printValue();

I fail to see the purpose of using inheritence here unless it is for purely academic reasons?

Edited 4 Years Ago by iamthwee

Thank you iamthwee!

But where is inheritance if I only have one class? The main purpose of this example is to execute inheritance from the main class which is Addition inherited by Subtraction.

This will run with this code, but look at the Addition class, it did nothing on the execution.

#include<iostream>
using namespace std;

class Addition
{
      public:
             Addition(){};
             ~Addition(){};

};

class Subtraction: public Addition
{
      public:
             float num1, num2;
             float ExecuteAddtion()
             {
                   return num1 + num2;
             }
             Subtraction(){};
             ~Subtraction(){};

};

Addition Addme;
Subtraction SubtractMe;

int main()
{
    cout<<"Enter first number: ";
    cin>>SubtractMe.num1;
    cout<<"Enter second number: ";
    cin>>SubtractMe.num2;
    cout<<"The sum of "<<SubtractMe.num1<<" and "<<SubtractMe.num2<<" is "<<SubtractMe.ExecuteAddtion()<<endl;
    system("pause");
    return 0;
}

Any modification of the code to execute inheritance is highy appreciated.

Thank you!

Edited 4 Years Ago by blocker

these small programs doesn't really need inheritence, Still you can use the derived class to access the variables in base class and further executing the sum from derived class.
have a look at this.

class Addition
{
    public:
        float num1,num2;
        Addition(){};
        ~Addition(){};
};

class Subtraction: public Addition
{
    public:
       float ExecuteAddtion()
       {
         return num1 + num2;
       }
       Subtraction(){};
       ~Subtraction(){};
};

variables num1 and num2 are declared in base class. Use the int main() you used in your program to get the output.

system("pause");

And one more thing, not related to your problem but system("pause") is not a standard way to hold the output on screen. Instead, use cin.get() which is a standard way of holding output.

in your above code, replace system("pause") by

fflush(stdin);  
cin.get();

dont forget to include the header "cstdio"

these small programs doesn't really need inheritence,

It does if

The main purpose of this example is to execute inheritance

It's how we learn.

Thank you Vish0203, WaltP!

Declaring the num1,num2 in the base class seems inherance present..And Vish0203, it seems getline(cin,num1); doesnt work for me. does getline only for string, char, how to use it with int, float, double?

#include<iostream>
using namespace std;

class Addition
{
      public:
             float num1, num2;
             Addition(){};
             ~Addition(){};

};

class Subtraction: public Addition
{
      public:
             //float num1, num2,sum;
             float ExecuteAddtion()
             {
                   return num1 + num2;
             }
             Subtraction(){};
             ~Subtraction(){};

};

Addition Addme;
Subtraction SubtractMe;

int main()
{
    cout<<"Enter first number: ";
    cin>>SubtractMe.num1;
    cout<<"Enter second number: ";
    cin>>SubtractMe.num2;
    cout<<"The sum of "<<SubtractMe.num1<<" and "<<SubtractMe.num2<<" is "<<SubtractMe.ExecuteAddtion()<<endl;
    system("pause");
    return 0;
}

Thank you!

getline is used for strings.

If you want you could convert the strings to a different datatype later. Using sstream is a good idea.

This question has already been answered. Start a new discussion instead.