Why do you think modifying SubtractMe's copy of sum should affect AddMe's copy of sum?
deceptikon
Challenge Accepted
3,435 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 56
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
3,435 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 56
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
3,435 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 56
iamthwee
Posting Genius
6,254 posts since Aug 2005
Reputation Points: 1,567
Solved Threads: 476
Skill Endorsements: 34
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
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
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37
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