I need help with "Inheritance" and "Polymorphism"
Hello, I am having some problems with inheritance. The compiler does
not not return any error messages, but when I execute the program, it
only allows me to enter the number, but nothing else happend. I think
the problem may be in my input function or in the main function.
If anyone out there can help me it woul be greatly appreciated.
Here is the code:
#include <iostream>
using namespace std;
const int SENTINEL = -999;
class SumType
{
protected:
int n[10];
int sum, max, max2, avg;
int counter;
int number;
int *ptr0, *ptr1, *ptr;
public:
SumType();
void input();
void calculation();
double output();
};
class AverageType: public SumType
{
protected:
double avg;
public:
void calculation();
double output();
};
class MaxType: public SumType
{
protected:
double max, max2;
public:
void calculation();
double output();
};
class Max2Type: public SumType
{
protected:
double max2;
public:
void calculation();
double output();
};
///////////////////////////////////////////////////////////////////////////////////
SumType::SumType()
{
sum = 0;
max = 0;
max2 = 0;
avg = 0;
counter = 0;
number = 0;
}
void SumType::input()
{
cout << "Enter your numbers: " << endl;
//ptr0 = &n[0];
while(number != SENTINEL)
{
counter++;
cin >> counter;
}
}
void SumType::calculation()
{
sum = sum + number;
}
double SumType::output()
{
return sum;
}
//////////////////////////////////////////////////////////////////////////////////////
void AverageType::calculation()
{
avg = sum / counter;
}
double AverageType::output()
{
return avg;
}
////////////////////////////////////////////////////////////////////////////////////
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];
max2 = n[0];
}
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;
}
//////////////////////////////////////////////////////////////////////////
void Max2Type::calculation()
{
int i;
cin >> n[0] >> n[1];
if (n[0] > n[1])
{
max = n[0];
max2 =n[1];
}
else
{
max = n[1];
max2 = n[0];
}
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 Max2Type::output()
{
return max2;
}
/////////////////////////////////////////////////////////////////
int main()
{
SumType s1;
AverageType a1;
MaxType m1;
MaxType m2;
s1.input();
s1.calculation();
s1.output();
a1.input();
a1.calculation();
a1.output();
m1.input();
m1.calculation();
m1.output();
m2.input();
m2.calculation();
m2.output();
cout << "The sum is "<< s1.output()<<endl;
cout << "The average is " << a1.output() << endl;
cout << "The largest number is "<< m1.output() <<endl ;
cout << "The second largest number is "<< m2.output() <<endl;
cout << endl << endl;
return 0;
}
/*
OUTPUT:
*/