我需要“继承”的帮助和“多态性”和“多态性”。 [英] I need help with "Inheritance" and "Polymorphism"
问题描述
您好,我在继承方面遇到了一些问题。编译器确实没有返回任何错误消息,但是当我执行程序时,它只允许我输入数字,但是没有其他事情发生。我想
问题可能在我的输入功能或主要功能中。
如果有人在那里可以帮助我,我们将不胜感激。
这是代码:
#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:
无效计算();
双输出();
};
类MaxType:public SumType
{
受保护:
最多两次,最多2次;
公开:
无效计算();
双输出();
};
class Max2Type:public SumType
{
受保护:
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<< 输入您的号码: << endl;
// ptr0 =& n [0];
while(number!= SENTINEL)
{
counter ++;
cin>>柜台;
}
}
无效SumType ::计算()
{
sum = sum + number;
}
double SumType :: output()
{
返还金额;
}
/////////// ////////////////////////////////////////////////// /////////////////////////
void AverageType :: calculation()
{
avg = sum / counter;
}
double AverageType :: output()
{
返回平均值;
}
////////////// ////////////////////////////////////////////////// ////////////////////
无效MaxType ::计算()
{
int i;
cin>> n [0]>> n [1];
if(n [0]> n [1])
{
max = n [0];
max2 = n [1];
}
其他
{
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()
{
返回最大值;
}
///////////////// ////////////////////////////////////////////////// ///////
无效Max2Type ::计算()
{
int i;
cin>> n [0]>> n [1];
if(n [0]> n [1])
{
max = n [0];
max2 = n [1];
}
其他
{
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]< max2)
{}
i ++;
cin>> n [i];
}
}
double Max2Type :: output()
{
返回max2;
}
///////////////// //////////////////////////////////////////////// <无线电通信/>
int main()
{
SumType s1;
AverageType a1;
MaxType m1;
MaxType m2;
s1.input();
s1.calculation(); < br $>
s1.output();
a1.input();
a1.calculation();
a1.output();
m1.input();
m1.calculation();
m1 .output();
m2.input();
m2.calculation();
m2.output() ;
cout<< 总和是<< s1.output()<< endl;
cout<< 平均值是指 << a1.output()<< endl;
cout<< 最大数量是<< m1.output()<< endl;
cout<< 第二大数字是<< m2.output()<< endl;
cout<< endl<<结束;
返回0;
}
/ *
输出:
* /
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:
*/
推荐答案
Fao写道:
你好,我是有一些继承问题。编译器确实不会返回任何错误消息,但是当我执行程序时,它只允许我输入数字,但没有其他事情发生。我认为问题可能出在我的输入函数或主函数中。
[...]
void SumType :: input()
{
cout<< 输入您的号码: << endl;
// ptr0 =& n [0];
while(number!= SENTINEL)
{
计数器++;
cin>>计数器;
那么,你输入一个数字,哪个变量得到它?它存储在哪里?
你不觉得你需要以某种方式填充你的对象所拥有的数组吗?什么
如果我输入的数字超过10个就会发生?
}
}
[...]
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
-
请在通过电子邮件回复时删除资金''A'
我这样做如果你说填充阵列,请不要回复最热门的回复,请不要问
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
,你的意思是我需要一个类似于
这个:
void fillarray(int n [10],int listsize)
{
int index ;
for(index = 0; index< listsize; index ++)
cin>>列表[索引];
}
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];
}
Fao写道:
Fao wrote:
当你说填充数组,你的意思是我需要一个类似于
的函数:
void fillarray(int n [10],int listsize)
when you say "Fill the array", you mean I need a function similar to
this:
void fillarray(int n[10], int listsize)
在风格方面,我认为10但不存在。我不认为
即使是模板技巧,在fillarray()中也可以到达10.所以这个
表示我们不应该通过假装数组。参数转换为函数,并且
所以我们应该只写这个:
void fillarray(int * n,int listsize)
-
Phlip
http://c2.com/cgi/wiki?ZeekLand < - 不是博客!!!
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!!!
这篇关于我需要“继承”的帮助和“多态性”和“多态性”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!