最新c++题库及答案

内容发布更新时间 : 2025/6/22 10:31:50星期一 下面是文章的全部内容请认真阅读。

bool fun(long n); void main()

{long a=10,b=30,l=0; if(a%2==0) a++;

for(long m=a;m<=b;m+=2) if(fun(m))

{if(l++==0) cout <

cout <

bool fun(long n)

{int sqrtm=(int)sqrt(n); for(int i=2;i<=sqrtm;i++) if(n%i==0) return false; return true; }

答案:11 13 17 19 23 29

[解析]循环体用来判断n是否是质数的函数,在main函数判断10~30之间质数。 3. 给出下面程序输出结果。 #include class Test {int x,y; public:

Test(int i,int j=0) {x=i;y=j;}

int get(int i,int j) {return i+j;} };

void main()

{Test t1(2),t2(4,6);

int (Test::*p)(int,int=10); p=Test::get;

cout<<(t1.*p)(5)<

cout<<(p1->*p)(7,20)<

答案:15 27

[解析]指向类成员函数的指针的使用,*p指向Test类中有两个参数的函数的一个指针。 P=Test::get.这样p就和get发生了联系。(t1.*p)(5)等价于调用一个参数的get函数。 4. #include #include #include

class student

{char name[8]; int deg;

char level[7];

friend class process; // 说明友元类 public:

student(char na[],int d) { strcpy(name,na); deg=d; } };

class process { public:

void trans(student &s) {int i=s.deg/10; switch(i) {case 9:

strcpy(s.level, \优\case 8:

strcpy(s.level,\良\case 7:

strcpy(s.level,\中\case 6:

strcpy(s.level,\及格\default:

strcpy(s.level,\不及格\} }

void show(student &s)

{cout<

void main()

{ student st[]={s

>>閻忕偞娲栫槐鎴﹀礂閵婏附鐎�<<
12@gma联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4 ceshi