We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,831 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Whats wrong with this class?

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!

5
Contributors
16
Replies
1 Day
Discussion Span
8 Months Ago
Last Updated
17
Views
Question
Answered
blocker
Posting Whiz
308 posts since Jan 2009
Reputation Points: 7
Solved Threads: 1
Skill Endorsements: 0

Why do you think modifying SubtractMe's copy of sum should affect AddMe's copy of sum?

deceptikon
Challenge Accepted
Administrator
3,435 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 56

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?

blocker
Posting Whiz
308 posts since Jan 2009
Reputation Points: 7
Solved Threads: 1
Skill Endorsements: 0

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;
}
deceptikon
Challenge Accepted
Administrator
3,435 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 56

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?

blocker
Posting Whiz
308 posts since Jan 2009
Reputation Points: 7
Solved Threads: 1
Skill Endorsements: 0

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.

deceptikon
Challenge Accepted
Administrator
3,435 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 56

Thank you deceptikon!

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

blocker
Posting Whiz
308 posts since Jan 2009
Reputation Points: 7
Solved Threads: 1
Skill Endorsements: 0

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.

Vish0203
Junior Poster
144 posts since Apr 2012
Reputation Points: -6
Solved Threads: 9
Skill Endorsements: 0

[deleted]

iamthwee
Posting Genius
6,254 posts since Aug 2005
Reputation Points: 1,567
Solved Threads: 476
Skill Endorsements: 34

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?

blocker
Posting Whiz
308 posts since Jan 2009
Reputation Points: 7
Solved Threads: 1
Skill Endorsements: 0

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?

iamthwee
Posting Genius
6,254 posts since Aug 2005
Reputation Points: 1,567
Solved Threads: 476
Skill Endorsements: 34

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!

blocker
Posting Whiz
308 posts since Jan 2009
Reputation Points: 7
Solved Threads: 1
Skill Endorsements: 0

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.

Vish0203
Junior Poster
144 posts since Apr 2012
Reputation Points: -6
Solved Threads: 9
Skill Endorsements: 0

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"

Vish0203
Junior Poster
144 posts since Apr 2012
Reputation Points: -6
Solved Threads: 9
Skill Endorsements: 0

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.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

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!

blocker
Posting Whiz
308 posts since Jan 2009
Reputation Points: 7
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 8 Months Ago by deceptikon, Vish0203, iamthwee and 1 other

getline is used for strings.

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

iamthwee
Posting Genius
6,254 posts since Aug 2005
Reputation Points: 1,567
Solved Threads: 476
Skill Endorsements: 34

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1207 seconds using 2.78MB