By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,208 Members | 1,072 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,208 IT Pros & Developers. It's quick & easy.

I need help with "Inheritance" and "Polymorphism"

P: n/a
Fao
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:
*/

Apr 27 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
Fao wrote:
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.

[...]
void SumType::input()
{
cout << "Enter your numbers: " << endl;
//ptr0 = &n[0];
while(number != SENTINEL)
{
counter++;
cin >> counter;
So, you enter a number and which variable gets it? Where is it stored?
Don't you think you need to somehow fill the array your object has? What
happens if I enter more than 10 numbers?
}
}
[..]


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 27 '06 #2

P: n/a
Fao
when you say "Fill the array", you mean I need a function similar to
this:

void fillarray(int n[10], int listsize)
{
int index;
for (index = 0; index < listsize; index++)
cin >> list[index];
}

Apr 27 '06 #3

P: n/a
Fao wrote:
when you say "Fill the array", you mean I need a function similar to
this:

void fillarray(int n[10], int listsize)


In terms of style, I think that 10 all but does not exist. I don't think
even a template trick, inside fillarray(), can get to the 10. So this
suggests we should not pass "pretend array" parameters into functions, and
so we might ought to only write this:

void fillarray(int * n, int listsize)

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Apr 27 '06 #4

P: n/a

"Fao" writes:
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.
Your immediate problem has nothing to do with inheritance or polymorphism
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;
// This class does all kinds of wonderful things. (Expand on that)
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;


There are two ways to change the value of count. One by the count++ above
and another by getting input from the user. Does this make sense? I can
only guess at what you are trying to do, but if you are trying to fill an
array you shouldn't ask the user to provide a count. Presumably that is
what SENTINEL is for. I would make a wild guess that you are trying to fill
an array.
This might be a simple brain fart but if it isn't you might make more
progress by throwing away all that advanced polymorphism stuff and strip
this down to a class without inheritance. You have done too much typing and
too little testing. And you use so much white space that my head hurts.

<snip>
Apr 27 '06 #5

P: n/a
Fao
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

*/

Apr 29 '06 #6

P: n/a
"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.
Apr 29 '06 #7

P: n/a
I V
On Fri, 28 Apr 2006 20:28:17 -0700, 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.


As osmium says, it's hard to help you because we don't know exactly what
you're trying to accomplish. Are you working off a specific assignment or
exam question? If so, post the assignment. If you don't have an
assignment, try and state simply what you're trying to do - don't say
(yet) how you're trying to accomplish it, because your difficulties may
be due to you approaching the problem in the wrong way.

To give you an idea of the sort of information we need, and because this
may help you anyway, I'll take a guess at what you're trying to do and
show you how I would approach the problem.

Let's say you want to input a list of numbers and then print out the
maximum and the sum (this is the statement of what I'm trying to
accomplish - my assignment, if you like).

To begin, think about how you would do this if you were working it out
manually, rather than using a computer. What you would do is write down
all the numbers, then go through them looking for the maximum, then go
through them again and work out the sum.

Now, we can try and break down this method into parts that we can convert
into a program. What you have is one object (the place where you write
down the numbers) and three operations (input the numbers, find the
maximum and calculate the sum). For the place you are writing down the
numbers, you can use an array:

const int max_numbers=10;
int numbers[max_numbers];

For the three operations, you can use functions:

/* Input up to |max| numbers until the user inputs |sentinel|, storing the
numbers in |store| and returning how many numbers where input */

int input(int store[], int max, int sentinel);

/* Return the maximum from the array |the_numbers| of |length| numbers */

int find_maximum(int the_numbers[], int length);

/* Return the sum of array |the_numbers| of |length| numbers */

int calculate_sum(int the_numbers[], int length);

Putting all this together in a program:

void main()
{
const int sentinel = -999
const int max_numbers=10;
int numbers[max_numbers];
int how_many;

how_many = input(numbers, max_numbers, sentinel);
std::cout << "Maximum: " << find_maximum(numbers, how_many) << std::endl;
std::cout << "Sum: " << calculate_sum(numbers, how_many) << std::endl;
}

Note that I'm not using inheritance or polymorphism here - the assignment
I set myself doesn't require them. Maybe you don't need to use them either?
Apr 29 '06 #8

P: n/a
Fao
Okay here are the directions That I received from my professor, word
for word:

"Write a program where the user inputs several integers.
Output sum,max,secmax,and average in object oriented class.
Implement Inheritance and Polymorphism."

I have just recently been able to get the max and secmax to work, but I
can't get the sum function to work properly with the inheritance ,and
thus can't get the average to work.

Sum Func:
void SumType::input()
{
cout << "Enter several numbers, to stop"
<< " program enter -999 " << endl;
i=0;

int number;
cin >> number;
while (number != SENTINEL)
{
sum = sum + number;
counter++;
cin >> number;
}
}

This is the output:
OUTPUT:
Enter several numbers, to stop program enter -999
2 8 6 4 -999
The sum is -858993460
The average is -858993460
The largest number is 8
The Second largest is 6

I V wrote:
On Fri, 28 Apr 2006 20:28:17 -0700, 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.


As osmium says, it's hard to help you because we don't know exactly what
you're trying to accomplish. Are you working off a specific assignment or
exam question? If so, post the assignment. If you don't have an
assignment, try and state simply what you're trying to do - don't say
(yet) how you're trying to accomplish it, because your difficulties may
be due to you approaching the problem in the wrong way.

To give you an idea of the sort of information we need, and because this
may help you anyway, I'll take a guess at what you're trying to do and
show you how I would approach the problem.

Let's say you want to input a list of numbers and then print out the
maximum and the sum (this is the statement of what I'm trying to
accomplish - my assignment, if you like).

To begin, think about how you would do this if you were working it out
manually, rather than using a computer. What you would do is write down
all the numbers, then go through them looking for the maximum, then go
through them again and work out the sum.

Now, we can try and break down this method into parts that we can convert
into a program. What you have is one object (the place where you write
down the numbers) and three operations (input the numbers, find the
maximum and calculate the sum). For the place you are writing down the
numbers, you can use an array:

const int max_numbers=10;
int numbers[max_numbers];

For the three operations, you can use functions:

/* Input up to |max| numbers until the user inputs |sentinel|, storing the
numbers in |store| and returning how many numbers where input */

int input(int store[], int max, int sentinel);

/* Return the maximum from the array |the_numbers| of |length| numbers */

int find_maximum(int the_numbers[], int length);

/* Return the sum of array |the_numbers| of |length| numbers */

int calculate_sum(int the_numbers[], int length);

Putting all this together in a program:

void main()
{
const int sentinel = -999
const int max_numbers=10;
int numbers[max_numbers];
int how_many;

how_many = input(numbers, max_numbers, sentinel);
std::cout << "Maximum: " << find_maximum(numbers, how_many) << std::endl;
std::cout << "Sum: " << calculate_sum(numbers, how_many) << std::endl;
}

Note that I'm not using inheritance or polymorphism here - the assignment
I set myself doesn't require them. Maybe you don't need to use them either?


Apr 30 '06 #9

P: n/a
"Fao" wrote:
Okay here are the directions That I received from my professor, word
for word:

"Write a program where the user inputs several integers.
Output sum,max,secmax,and average in object oriented class.
Implement Inheritance and Polymorphism."

I have just recently been able to get the max and secmax to work, but I
can't get the sum function to work properly with the inheritance ,and
thus can't get the average to work.

<snip>

I think that assignment is seriously flawed. It is hard to get a simple
student assignment that uses polymorphism but how any one could do some
sensible polymorphism with that assignment is beyond me. I think you have
done about as good as anyone could do, under the circumstances.

Here is a pretty darn good definition of polymorphism from Wikipedia.
Wiki snip:
---------------------------
In object-oriented programming theory, polymorphism is the ability of
objects belonging to different types to respond to method calls of methods
of the same name, each one according to the right type-specific behaviour.
The programmer (and the program) does not have to know the exact type of the
object in advance, so this behavior can be implemented at run time (this is
called late binding or dynamic binding).

The different objects involved only need to present a compatible interface
to the clients (the calling routines). That is, there must be public methods
with the same name and the same parameter sets in all the objects. In
principle, the object types may be unrelated, but since they share a common
interface, they are often implemented as subclasses of the same parent.
Though it is not required, it is understood that the different methods will
also produce similar results (for example, returning values of the same
type).

-------------------- end snip -------------------

Thus really one wants three classes, a base class and at least two derived
classes. As I said, you have done good considering what you had to work
with. Here is the code you posted Friday patched up and tested. I didn't
test as seriously as if this were important but it passes some casual tests.
It does exhibit polymorphism , albeit in a kludgy form. Note also that
max2() sticks out like a goiter. Silk purses and pig's ears and all that.

As I scan this for a final time, I see things that should have been changed
for purity. For example, number should be demoted to a member function
rather than being part of the object. But I am nervous about changing
things after testing without a retest. The call on calculation() could be
embedded in MaxType, that might be neater. The indentation is a mix of my
style and your style.

If you feel a need to contact me, you can send me e-mail by subtracting 100
from my posted address.

-------------------------
/* cin.get() added here and there are a peculiarity needed by my compiler.
Feel free to remove.*/
#include <iostream>
#include <cfloat> // for DBL_MIN

using namespace std;

const int SENTINEL = -999;

class SumType
{
protected:
double n[10]; // note changed type
int sum;
int counter;
int number;
// int *ptr0, *ptr1, *ptr; I never did figure out what you had in mind
here
public:
SumType();
void input();
double output();
};
//------------------------
class MaxType: public SumType
{
protected:
double max, max2;
public:
MaxType() :max(0), max2(0) { }
void calculation();
double output();
double get_max2() {return max2;} // new
private:
void swap(int i, int j); // new code
int c_max(void); // new code
};
///////////////////////////////////////////////////////////////////////////////////
SumType::SumType()
{
sum = 0;
counter = 0;
number = 0;
}
//............................
// get user input and save it for MaxType
void SumType::input()
{
cout << "Enter numbers : To stop program enter" << " " << SENTINEL <<endl;
cin >> number;
while (number != SENTINEL)
{
sum = sum + number;
n[counter] = number; // this lack may have been your biggest problem.
counter++;
cin >> number;
}
}
//.......................
double SumType::output()
{
return sum;
}
//..............
// swap n[i] nd n[j]
void MaxType::swap(int i, int j)
{
double temp = n[i];
n[i] = n[j];
n[j] = temp;
}
//.....................
// find and return the index of the current maximum
int MaxType::c_max(void)
{
double maxx = n[0];
int ix = 0;
for(int i=1; i<counter; i++)
if(n[i] > maxx)
{
maxx = n[i];
ix = i;
}
return ix;
}
//////////////////////////////////////////////////////////////////////////////////////
// compute and save max and max2
// technique: find and remove max from contention.
// This is inefficient but easier to write and debug

void MaxType::calculation()
{
if(counter<2)
{
cout << "Not enough input\n";
cin.get();
exit(1); // abort
}
int idx = c_max();
max = n[idx];
n[idx] = DBL_MIN; // won't find *him* again!
idx = c_max();
max2 = n[idx];
}
//.................
double MaxType::output()
{
return max;
}
//----------------------
void test_sum()
{
// test SumType
SumType s1;
s1.input();
s1.output();
cout << "The sum is "<< s1.output()<<endl;
cout << "end sum test\n";
}
//--------------
void test_max()
{
MaxType m1;
m1.input();
m1.calculation();
m1.output();
cout << "The largest number is "<< m1.output() << endl ;
cout << "The second largest is " << m1.get_max2() << endl;
cout << "End MaxTest\n";
}

/////////////////////////////////////////////////////////////////
int main()
{
//test_sum();
//test_max();

// both types work by themselves. now try the polymorhic thing

SumType ss;
MaxType mm;
SumType * psum1 = &ss;
MaxType * pmax1 = &mm;
psum1 -> input();
cout << "The output of SumType is " << psum1->output() << endl;
pmax1->input();
pmax1->calculation();
cout << "The output of MaxType is " << pmax1->output() << endl;

cin.get();
cin.get();
return 0;
}

Apr 30 '06 #10

P: n/a
Fao
Hey,Thanks alot for all the help. I am glad that I wasn't the only who
the thought directions were vague at best.
To tell the truth I, did not think the program was possible to
complete, I worked on it for 7 days and only got
as far you saw.

The only problem I have now is that I have to enter the data twice to
get the program to run correctly
shouldn't the "input( )" from "SumType" be pulled to other functions
due to the inheritance? I tried a few a
things but nothing worked.

Here is the output:

/***************************
OUTPUT:
Enter numbers : To stop program enter -999
2 4 6 8 1 10 -999
The sum is 31
end sum test
Enter numbers : To stop program enter -999
2 4 6 8 1 10 -999
The largest number is 10
The second largest is 8
End MaxTest
Press any key to continue
*******************************/

Is there a quick way to modify the program so that I only have to enter
the data once?

Again thanks for all the help, I really appreciate it.

May 1 '06 #11

P: n/a
I V
On Sun, 30 Apr 2006 19:46:30 -0700, Fao wrote:
The only problem I have now is that I have to enter the data twice to
get the program to run correctly
shouldn't the "input( )" from "SumType" be pulled to other functions
due to the inheritance? I tried a few a


No - inheritance is a relationship between _classes_, but objects of
these classes remain separate. Just as if you create two ints, they
have separate values, and when you create two SumType objects, they will
each have a separate list of numbers, when you you create a SumType object
and a MaxType object, they will each have separate members.

A class is kind of like a blueprint or specification, it says what members
will exist in any object of that class (that is, any object built to that
specification). But it does not itself have any members (just like a
blueprint for a house does not have actual walls), and the members of all
the objects of that class are separate (just as two houses built from the
same blueprint will have separate walls). This is a pretty basic
distinction which is really vital for understanding object-oriented
programming, so you might want to reread the relevant sections in your
textbook or notes until you're sure you understand it.

So, you can't have one object do the input, because then the numbers
won't be available to the other objects. What you should do is separate
the input from the calculation. For instance:

class Calculator
{
public:
std::vector<int> numbers_;

// The constructor makes a copy of a
// vector of numbers passed to it.
Calculator(const std::vector<int>& numbers)
: numbers_(numbers)
{ }

virtual int calculate();

// Note that there's no input function here.
// Instead, make input a non-member function and call it
// before you create objects of any of the Calculator
// subclasses
};

// The base class calculate function doesn't perform a
// calculation but prints an error message. It doesn't
// really make sense to provide an implementation
// for the function here. To find out the best practice in this case,
// look up "pure virtual functions"
int Calculator::calculate()
{
std::cerr << "Warning! No calculation performed\n";
return 0;
}

class SumCalculator : public Calculator
{
public:
virtual int calculate();
};
int SumCalculator::calculate()
{
// You can put your implementation of the
// sum calculation here.
}

Then, in your main function, first input the numbers, then pass the
numbers to the constructor when you create each object that will do the
different sort of calculations.

int main()
{
std::vector<int> numbers;

input(numbers);

SumCalculator sum(numbers);
std::cout << "Sum: " << sum.calculate() << std::endl;
}

May 1 '06 #12

P: n/a
"Fao" writes:
Okay here are the directions That I received from my professor, word
for word:

"Write a program where the user inputs several integers.
Output sum,max,secmax,and average in object oriented class.
Implement Inheritance and Polymorphism."


I think he meant: Write a program that outputs the sum, max, secmax, and
average. I finally see where you got the notion of an actual output
*function*. Anyway, I would continue with what you have. I note,
belatedly that there is no average function but I am sure you can write one
now. Put it in the SumType and both types can use it. You instructor has a
rather fluid view of the world. He uses the vague term "average" instead of
"mean" and uses "secmax" as though that meant something to a whole lot of
people, including college students. I know of four meanings for average and
there may well be more. But we all know he meant to compute the mean.

May 1 '06 #13

P: n/a
"Fao" writes:
Hey,Thanks alot for all the help. I am glad that I wasn't the only who
the thought directions were vague at best.
To tell the truth I, did not think the program was possible to
complete, I worked on it for 7 days and only got
as far you saw.

The only problem I have now is that I have to enter the data twice to
get the program to run correctly
shouldn't the "input( )" from "SumType" be pulled to other functions
due to the inheritance? I tried a few a
things but nothing worked.


This has already been answered in a good fashion , but maybe another bit of
explaining wouldn't hurt. You have certain properties, some of them
inherited from your mother. You also have properties totally unrelated to
her. Let's say she had red hair and so do you. You share the
characteristic of having red hair. But she has her hair and you have your
hair. The first set of numbers you type will go into the array ss.n. The
second set you type go into array mm.n. An Object of MaxType has variables
max, max2, n, sum, counter and number. Why, you ask can MaxType not even
see it's own variables if they are declared private rather than protected in
the parent class?

Why do they ask for your telephone number when you buy batteries at Radio
Shack? That's the way things are.
May 2 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.