Re: I need help with "Inheritance" and "Polymorphism"
"Fao" wrote:
Sorry for the long wait on the response.
Okay I have stripped down the program to just do SUM and MAX.
My problem is I don't know how to "pull" the input data from SumType
into MaxType.
Here is what *I* meant by stripping down. Take out all the stuff that
doesn't do anything and see if the residue does what you want. I took the
liberty of demoting "number" to a place where it belongs.
------
#include <iostream>
using namespace std;
const int SENTINEL = -999;
class SumType
{
protected:
int sum; // max
int counter;
public:
SumType();
void input();
double output();
};
//.................
SumType::SumType()
{
sum = 0;
//max = 0;
counter = 0;
}
//....................
void SumType::input()
{
cout << "Enter numbers : To stop program enter"
<< " " << SENTINEL <<endl;
int number;
cin >> number;
while (number != SENTINEL)
{
sum = sum + number;
counter++;
cin >> number;
}
}
//.........................
double SumType::output()
{
return sum;
}
/////////////////////////////////////////////////////////////////
int main()
{
SumType s1;
s1.input();
s1.output();
cout << "The sum is "<< s1.output()<<endl;
cout << endl << endl;
cin.get();
cin.get();
return 0;
}
-----------------------------
Only you know the requirements. This gets some numbers and adds them. In
your main you had several calls that accomplished the same end result. It
seems to me this could do nothing but confuse you. Note that there is no
way in the world that the other function could determine the maximum, you
did not save the necessary data. I keep thinking the array you had (n) was
supposed to play some part in this but you never put any data in it. Since
you refuse to divulge what the program is supposed to do, I can only guess.
Note also that you explicitly had cin calls in MaxType. I note also you had
two variables each named max. This is a bad practice for a beginner. Use
different names for different things. The fact that you didn't even use the
one in SumType just adds to the confusion.
The only thing that MaxType could possibly inherit is sum and a count of the
numbers entered. I suggest you modify the code above as needed, get it
working again, and then add the code for MaxType.