codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Problem with Inheritance and Polymorphism  Fao at 03:47 on Saturday, April 29, 2006
 

I am a newbie and I hate for my first post to be me asking for help by I am desperate.

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;


while (n != -999)
{
if (n > max)
{
max2 = max;
max = n;
}


if (n < max &&n > max2)
{
max2 = n;
}


if (n < max2)
{ }


i++;
cin >> n;
}



}


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


*/











CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  onChange issue
•  Remote program execution using cgi-perl
•  Chat application
•  How to send multiple perameters in SOAP request.
•  Java code for Insert picture on the table in spreadsheet
•  Re: Problem with concatenation
•  how to genrates the crystal report by sending a id at runtime
•  help me
•  pls help me with this..


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2007