首页编程c++面试题目100及最佳答案,c++编程题库及答案

c++面试题目100及最佳答案,c++编程题库及答案

编程之家2026-05-19873次浏览

大家好,今天来为大家解答c++面试题目100及最佳答案这个问题的一些问题点,包括c++编程题库及答案也一样很多人还不知道,因此呢,今天就来为大家分析分析,现在让我们一起来看看吧!如果解决了您的问题,还望您关注下本站哦,谢谢~

c++面试题目100及最佳答案,c++编程题库及答案

求C++大神。14个选择题,100分,谢谢啊!急!

1.以下叙述中正确的是( D)

D)所有被调用的函数一定要在调用之前进行定义

2. C++语言的跳转语句中,对于break和continue说法正确的是(B)

B)continue语句只应用与循环体中

3. for(int x=0,y=0;!x&&y<=5; y++)语句执行循环的次数是(C)

C)6

c++面试题目100及最佳答案,c++编程题库及答案

4.假定AA为一个类,a()为该类公有的函数成员,x为该类的一个对象,则访问x对象中函数成员a()的格式为(B)。

B) x.a()

5.下面有关重载函数的说法中正确的是(C)

C)重载函数必须有不同的形参列表

6.下列关于构造函数的描述中,错误的是(D)

D)构造函数不可以重载

c++面试题目100及最佳答案,c++编程题库及答案

7.设有数组定义:char array[]=″China″;,则数组array所占的空间为( C)

C)6个字节

8.下面选项中不属于面向对象程序设计特征的是(D)。

D)相似性

9.在C++中用来实现运行时多态性的是( D)。

D)虚函数

10.以下程序的输出结果是(A)

#include<iostream>

using namespace std;

void reverse(int a[],int n)

{

int i,t;

for(i=0;i<n/2; i++)

{ t=a[i]; a[i]=a[n-1-i];a[n-1-i]=t;

}

}

void main()

{

int b[10]={1,2,3,4,5,6,7,8,9,10};int i,s=0;

reverse(b,8);

for(i=6;i<10;i++)s+=b[i];

cout<<s;

}

A) 22

11.下面叙述不正确的是(D)

D)基类的公有成员在派生类中仍然是公有

13.下面描述中,表达错误的是(D)

D)私有继承时基类中的public成员在派生类中是private的

14.有以下程序

#include<iostream>

using namespace std;

void main()

{

int a=5,b=0,c=0;

if(a=b+c) cout<<"***"<<endl;

else cout<<"$$$"<<endl;

}

下列选项叙述正确的是(D)

D)输出$$$

15.假定AB为一个类,则执行“AB x;”语句时将自动调用该类的(B)。

B)无参构造函数

急!请C++高手帮忙编程。100分送上。

/*(1)定义一个函数 int count(int a[],int n)在n个元素的数组a中,统计出大于零的元素个数,此个数作为函数返回值。在main()函数中,对数组b做如下初始化

int b[]={15,16,-23,7,-5,19,-2,0,28,11};

然后调用你定义的函数,在主函数中输出数组b中小于零元素的个数。

*/

#include"iostream.h"

int z=0;//零的个数

int count(int a[],int n)

{

int dl=0;//大于0的个数

int i;

for(i=0;i<n;i++)

{

if(a[i]>0)

dl++;

else if(a[i]==0)

z++;

}

return dl;

}

void main()

{

int n,x;

int b[]={15,16,-23,7,-5,19,-2,0,28,11};

n=10;

x=n-count(b,n);

x=x-z;

cout<<"less than zero number="<<x<<endl;

}

/*(2)写两个函数,分别求两个整数的最大公约数和最小公倍数,

用主函数调用这两个函数并输出结果。两个整数由键盘输入。

利用最大公约数求最小公倍数

由于两个数的乘积等于这两个数的最大公约数与最小公倍数的积。这就是说,求两个数的最小公倍数,可以先求出两个数的最大公约数,再用这两个数的最大公约数去除这两个数的积,所得的商就是两个数的最小公倍数。

例求105和42的最小公倍数。

因为105和42的最大公约数是21,

105和42的积是4410,4410÷21=210,

所以,105和42的最小公倍数是210。

*/

#include"iostream.h"

int gys,gbs;

int gy(int a,int b)

{

int r;

r=a%b;

if(r!=0)

{

a=b;

b=r;

r=a%b;

}

return b;

}

int gb(int a,int b)

{

int r;

r=a*b/gys;

return r;

}

int main()

{

int a,b,t;

cout<<"Input a"<<endl;

cin>>a;

cout<<"Input b"<<endl;

cin>>b;

if(a==0|| b==0)

{

cout<<"Input Error"<<endl;

return 0;

}

if(a<b)

{

t=a;a=b;b=t;

}

gys=gy(a,b);

gbs=gb(a,b);

cout<<"gys="<<gys;

cout<<"gbs="<<gbs;

}

/*(3)求方程ax2+bx+c=0的根,用三个函数分别求当b2-4ac大于0、

等于0和小于0时的根,并输出结果。从主函数出入a、b、c的值,

编程求解该方程的根。

*/

#include"iostream.h"

#include<math.h>

void dy(double a,double b,double c,double derta)

{

double d;

double x1,x2;

if(a==0)

{

x1=-c/b;

x2=-c/b;

}

else

{

d=pow(derta,0.5);

x1=(-b+d)/2/a;

x2=(-b-d)/2/a;

}

cout<<"x1="<<x1<<endl;

cout<<"x2="<<x2<<endl;

}

void deng(double a,double b,double c, double derta)

{

dy(a,b,c,derta);

}

void xiao(void)

{

cout<<"no answer!"<<endl;

}

int main()

{

double a,b,c;

double derta;

cout<<"Input a"<<endl;

cin>>a;

cout<<"Input b"<<endl;

cin>>b;

cout<<"Input c"<<endl;

cin>>c;

derta=b*b-4*a*c;

if( derta>0)

{

dy(a,b,c,derta);

}

else if(derta==0)

{

deng(a,b,c,derta);

}

else// derta<0

{

xiao();

}

}

/*(4)写一个判素数的函数,在主函数输入一个整数,输出是否素数的信息

*/

#include"iostream.h"

#include<math.h>

int compare(int n)

{

int b=1;

int i;

if(n==2|| n==1)

{

}

else

{

for(i=2;i<=n;i++)

{

if(n%i==0)

{

b=0;

break;

}

}

}

return b;

}

int main()

{

int n;

cout<<"Input n"<<endl;

cin>>n;

if(compare(n)==1)

{

cout<<"this number is prime number!"<<endl;

}

else

{

cout<<"this number is not prime number!"<<endl;

}

}

/*(1)编写一个函数,实现两个整数的交换。如:主调函数中

int a=10;

int b=20;

使用引用作为函数的参数,交换后为:

a=20;

b=10;

*/

#include"iostream.h"

void change(int&a,int&b)

{

int t;

t=a;

a=b;

b=t;

}

void main()

{

int a=10;

int b=20;

change(a,b);

cout<<"a="<<a<<"\nb="<<b<<endl;

}

/*(2)编程实现两个字符串的交换。如:

char*p1=”hello”

char*p2=”good”

使用引用作为函数的参数,交换后为:

p1:” good”

p2:” hello”

*/

#include"iostream.h"

#include"stdio.h"

#include"string.h"

void change(char*&p1,char*&p2)

{

char*t1;

t1=p1;

p1=p2;

p2=t1;

}

void main()

{

char*p1="hello";

char*p2="good";

change(p1,p2);

cout<<"p1="<<p1<<"\np2="<<p2<<endl;

}

C++ 语言 生成一个0~100的随机数

一、C++获取随机数的方法

rand()方法是C++获取随机数的方法,可以通过srand()方法获取系统的时间,用系统时间作为判断依据,生成随机数,随机数的大小通过rand()方法获取的值,进行取余后获得。也就是说,srand方法获取到了系统的时间,他是毫秒级的,然后通过这个毫秒级的数据,对100取余,即可获得0-100的随机数,如果是1-100的随机数,可以在运算结果加1来实现。

二、实现代码

#include<iostream.h>

intmain(intargc,char*argv[])

{

intnumber=0;//随机数

intinput=0;//记录用户输入的数字

srand(unsigned(time(0)));//获取系统时间

number=rand()%100;//生成随机数

cout<<"Pleaseguessthenumber(0-100):";//输入提示语句

cin>>input;//接收用户输入

while(number!=input){//猜数字环节

if(input<number){//用户输入比随机数小

cout<<"Yournumberissmallerthanmynumber!Guessagain!"<<endl;

}else{//用户输入比随机数大

cout<<"Yournumberisbiggerthanmynumber!Guessagain"<<endl;

}

cin>>input;//接收用户输入

}

cout<<"Thatisit!"<<endl;//答对提示

return0;

}运行结果:

注:猜数字使用2分法速度最快。

好了,本文到此结束,如果可以帮助到大家,还望关注本站哦!

ai快捷键?ai的快捷键大全(AI快捷键大全)网站源码 com大全 51码网站大全