Re: I need help with "Inheritance" and "Polymorphism"
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.
The program compiles fine, but after I enter my data, it makes me enter
5 more times
and the Output from sum adds up the total sum from all of the last five
inputs.
How do I stop/fix this?
Note: I am not computer science major, this was my minor up I
encountered Inheritance and polymorphism. The reason I need to know
where I am messing up at and how to fix it, is because my final for
this course will cover this topic. If anyone on this forum could help
me it would be greatly appreciated.
Here is the code and output:
#include <iostream>
using namespace std;
const int SENTINEL = -999;
class SumType
{
protected:
int n[10];
int sum, max;
int counter;
int number;
int *ptr0, *ptr1, *ptr;
public:
SumType();
void input();
double output();
};
class MaxType: public SumType
{
protected:
double max, max2;
public:
void calculation();
double output();
};
///////////////////////////////////////////////////////////////////////////////////
SumType::SumType()
{
sum = 0;
max = 0;
counter = 0;
number = 0;
}
void SumType::input()
{
cout << "Enter numbers : To stop program enter"
<< " " << SENTINEL <<endl;
//ptr0 = &n[0];
cin >> number;// input statement
while (number != SENTINEL)// checks whether number is equal to
SENTINEL
{
sum = sum + number;// calculates sum
counter++;// increments count by 1
cin >> number;
}
}
double SumType::output()
{
return sum;
}
//////////////////////////////////////////////////////////////////////////////////////
void MaxType::calculation()
{
int i;
cin >> n[0] >> n[1];
if (n[0] > n[1])
{
max = n[0];
max2 = n[1];
}
else
{
max = n[1];
}
i = 2;
cin >> n[i];
while (n[i] != -999)
{
if (n[i] > max)
{
max2 = max;
max = n[i];
}
if (n[i] < max &&n[i] > max2)
{
max2 = n[i];
}
if (n[i] < max2)
{ }
i++;
cin >> n[i];
}
}
double MaxType::output()
{
return max;
}
/////////////////////////////////////////////////////////////////
int main()
{
SumType s1;
MaxType m1;
SumType * psum1 = &s1;
SumType * pmax1 = &m1;
psum1 -> input();
pmax1 -> input();
s1.input();
m1.calculation();
s1.input();
s1.output();
m1.calculation();
m1.output();
cout << "The sum is "<< s1.output()<<endl;
cout << "The largest number is "<< m1.output() << endl ;
cout << endl << endl;
return 0;
}
/*
OUTPUT:
Enter numbers : To stop program enter -999
1 2 3 4 5 -999
Enter numbers : To stop program enter -999
1 2 3 4 5 -999
Enter numbers : To stop program enter -999
1 2 3 4 5 -999
1 2 3 4 5 -999
Enter numbers : To stop program enter -999
1 2 3 4 5 -999
1 2 3 4 5 -999
The sum is 45
The largest number is 5
Press any key to continue
----------------------------------------------------
Enter numbers : To stop program enter -999
2 4 6 8 -999
Enter numbers : To stop program enter -999
2 4 6 8 -999
Enter numbers : To stop program enter -999
2 4 6 8 -999
2 4 6 8 -999
Enter numbers : To stop program enter -999
2 4 6 8 -999
2 4 6 8 -999
The sum is 60
The largest number is 8
Press any key to continue
*/