程序如下:
class number
{
protected:
int m_number;
public:
int getnumber(){ return m_number;}
void setnumber(int n){ m_number=n;}
void prime();
};
#include<iostream.h>
void class number::prime()
{
int i;
for(i=2;i<m_number;i++)
{if(m_number%i==0)break;}
if(m_number==i)
cout<<m_number<<" is prime."<<endl;
}
class dernumber:public number
{
public:
void prime();
};
#include<math.h>
void dernumber::prime()
{
double sqrtm=sqrt(m_number);
int i;
for(i=2;i<=sqrtm;i++)
{if(m_number%i==0)break;}
if(sqrtm<i)
cout<<m_number<<" is prime.\n";
}
#include<time.h>
void main()
{
number anum;
dernumber adernum;
clock_t start,finish;
double elapsed_time1,elapsed_time2;
int i;
unsigned int max(100000);
time(&start);
for(i=10000;i<max;i++)
{ anum.setnumber(i);
anum.prime();}
time(&finish);
elapsed_time1=difftime(finish,start);
time(&start);
for(i=10000;i<=max;i++)
{ adernum.setnumber(i);
adernum.prime();}
time(&finish);
elapsed_time2=difftime(finish,start);
cout<<"delay for using number class:"<<elapsed_time1<<" seconds"<<endl;
cout<<"delay for using adernu class:"<<elapsed_time2<<" seconds"<<endl;
}
编译器信息:
--------------------Configuration: 02 - Win32 Debug--------------------
Compiling...
prime.cpp
E:\VC\02\prime.cpp(26) : error C2653: 'dernumber' : is not a class or namespace name
E:\VC\02\prime.cpp(28) : error C2065: 'm_number' : undeclared identifier
E:\VC\02\prime.cpp(39) : error C2065: 'dernumber' : undeclared identifier
E:\VC\02\prime.cpp(39) : error C2146: syntax error : missing ';' before identifier 'adernum'
E:\VC\02\prime.cpp(39) : error C2065: 'adernum' : undeclared identifier
E:\VC\02\prime.cpp(45) : warning C4018: '<' : signed/unsigned mismatch
E:\VC\02\prime.cpp(51) : warning C4018: '<=' : signed/unsigned mismatch
E:\VC\02\prime.cpp(52) : error C2228: left of '.setnumber' must have class/struct/union type
E:\VC\02\prime.cpp(53) : error C2228: left of '.prime' must have class/struct/union type
Error executing cl.exe.
prime.obj - 7 error(s), 2 warning(s)
我始终找不出错误在哪,高手能不能帮下忙,谢了先。(我学C++刚三天,可能是低级错误)。