首页 > 编程语言 >C++试题带答案

C++试题带答案

时间:2024-10-15 10:46:42浏览次数:3  
标签:Sample cout int void number C++ char 答案 试题

一、选择填空题

1.有如下定义

struct person

{

char  name[9];

int age;

};

person   pr[10]={"Johu", 17,"Paul", 19,"Mary", 18,"Adam", 16};

根据上述定义,能输出字母M的语句是     

A) cout<<pr[3].mane;            B) cout<<pr[3].name[1];

C) cout<<pr[2].name[1];         D) cout<<pr[2].name[0];

2.作用域区分符(::)的功能是     

A、 标识作用域的级别        B、 标识成员属于哪个类

C、 限定成员的作用范围      D、 指出作用域的范围

3. 可以用 p.a 的形式访问派生类对象 p 的基类成员 a ,其中 a 是         

A. 公有继承的公有成员    B. 公有继承的私有成员 

C. 公有继承的保护成员    D. 私有继承的公有成员

4.有关类的说法不正确的是      

A、类是一种用户自定义的数据类型

B、只有类中的成员函数才能存取类中的私有数据

C、在类中,如果不作特别说明,所有的数据均为私有类型

D、在类中,如果不作特别说明,所有的数据均为公有类型

5.通常拷贝构造函数的参数是     

A、某个对象名           B、某个对象的成员名   

C、某个对象引用名       D、某个对象的指针名

6.设有如下定义:

    int arr[]={6,7,8,9,10};

    int * ptr;

    则下列程序段的输出结果为        

    ptr=arr;

    * (ptr+2)+=2;

    cout<<*ptr <<","<<*(ptr+2)<<endl;

A、6,10         B、6,8            C、7,9        D、8,10    

7. 如果类 A 被说明成类 B 的友元,则             

A. 类 A 的成员即类 B 的成员            B. 类 B 的成员即类 A 的成员 

C. 类 A 的成员函数不得访问类 B 的成员 D. 类 B 不一定是类 A 的友元 

8.下列关于继承的描述中,错误的是        

  1. 析构函数不能被继承            B.派生类是基类的组合

C.派生类的成员除了它自己的成员外还包含了它的基类的成员

D.派生类中继承的基类成员的访问权限到派生类保持不变

9.关于delete运算符的下列描述中,           是错误的。

A.它必须用于new返回的指针

B.它也适用于空指针

C.对一个指针可以使用多次该运算符

D.指针名前只用一对方括号符,不管所删除数组的维数

10. C++提供引用的主要用途之一是建立参数的           方式。

A.. 值传递   B. 引用传递   C. 地址传递    D. 指针传递

二、阅读以下程序并给出执行结果

1.

#include<iostream.h>

class Sample

{

public:

       Sample(int a);

       void add();

       void print();

private:

       int n;

       static int sum;

};

Sample::Sample(int a)

{    

       n=a;

}

void Sample::add()

{

       sum+=n;

}

void Sample::print()

{

       cout<<"n="<<n<<",sum="<<sum<<endl;

}

int Sample::sum=0;

void main()

{

       Sample s1(10),s2(20),s3(30);

       s1.add();       s1.print();

       s2.add();       s2.print();

       s3.add();       s3.print();

}

2.                                             

#include <iostream.h>

class Sample

{

public:

       Sample();

       Sample(int val);

       void Display();

       ~Sample();

protected:

       int i;

};

Sample::Sample()

{

       cout<<"Constructor1"<<endl;

       i=0;

}

Sample::Sample(int val)

{

       cout<<"Constructor2"<<endl;

       i=val;

}

void Sample::Display()

{

       cout<<"i="<<i<<endl;

}

Sample::~Sample()

{

       cout<<"Destructor  "<<i<<endl;

}

void main()

{

       Sample a,b(30);

       a.Display();

       b.Display();

}

3.

#include <iostream.h>

class Base

{

private:

       int a,b;

public:

       Base(int i,int j)

       {

              a=i;

              b=j;

       }

       void move(int x,int y)

       {

              a+=x;

              b+=y;

       }

       void show(){cout<<"a="<<a<<",b="<<b<<endl;}

};

class Derive:private Base

{

private:

       int x,y;

public:

       Derive(int i,int j,int k,int l):Base(i,j)

       {

              x=k;

              y=l;

       }

       void show(){cout<<"x="<<x<<",y="<<y<<endl;}

       void fun(){move(3,5);}

       void showbase(){Base::show();}

};

void main()

{

       Base b(1,2);

       b.show();

       Derive d(3,4,5,6);

       d.fun();

       d.show();

       d.showbase();

}

三、阅读以下程序并简要叙述其功能(15分,每小题5分)

1.试写出程序的运行结果并叙述func( )函数的功能。

#include <iostream.h>

int* func(int* array, int size, int* index);

void main()

{

  int a[10]={33,54,67,82,37,91,85,63,19,68};

  int* madr;

  int idx;

  madr=func(a, sizeof(a)/sizeof(*a), &idx);

  cout <<"idx="<<idx <<",a["<<idx<<"]="<<a[idx] <<endl;

}

int* func(int* array, int size, int* index)

{

  *index=0;

  for(int i=0; i<size; i++)

    if(array[i]>array[*index])

      *index=i;

  return &array[*index];

}

2. 试写出程序的运行结果并叙述f1( )函数的功能。

#include <iostream.h>

#include <string.h>

char *f1(char *s1,char *s2, int n, int length)

{

        char *temp;

        temp=s1+n-1;

        strncpy(s2,temp,length);

        s2[length]='\0';

        return s2;

}

void main()

{

        char str1[20],str2[20];

        int m,l;

        cin>>str1;

        cin>>m>>l;

        cout<<f1(str1,str2,m,l)<<endl;

}

输入:ABCDEFGHIJK

      3  5

3. 试述rect类中各成员函数的功能。

#include<iostream.h>

class rect

{

private:

       double l,s;

public:

       rect(){};

       rect(double a,double b)

       {

              l=a;s=b;

       }

       double showlength()

       {

              return (l+s)*2;

       }

       double area(){return l*s;}

       rect tlength(rect r2)

       {

              rect temp;

              temp.l=l+r2.l;

              temp.s=s+r2.s;

              return temp;

       }

};

void main()

{

       rect recta(7.2,8.5),rectb(4.5,3.1);

       cout<<"rect a:"<<recta.showlength()<<endl;

       cout<<"rect b:"<<rectb.showlength()<<endl;

       rect rectc=recta.tlength(rectb);

       cout<<"rect c:"<<rectc.showlength()<<endl;

}

四、阅读以下程序并填空(填上正确的语法成分),使其成为完整的程序

从已建立的学生链表中删除学号为number的学生结点。

struct Student

{

    long number;

    float score;

    Student * next;

};

Student * Delete (Student *head,long number) //删除链表结点

{

    Student *p;   //p指向要删除的结点

    if(           (1)            )    //原链表为空链表

    {

       cout<<"\nList is null!\n";

       return(head);

    }

if (             (2)            )  //要删除的结点为链表的第一个结点

    {

       p=head;

       head=head->next;

       delete p;

       cout<<number<<"the head of list have been deleted\n";

       return(head);

    }

for(Student * pGuard=head;        (3)        ;pGuard=pGuard->next)

    {

       if (pGuard->next->number==number)  //找到要删除的结点

       {

                      (4)              

                      (5)             

           delete p;

           cout<<number<<"have been deleted \n";

           return(head);

       }

    }

    cout<<number<<"not found!\n";   //未找到要删除的结点

    return (head);

}

五、编程题

  1. 编写一程序实现将两个指定文本文件的内容首尾相连合并成一个新的文本文件。

2、设计并测试一个描述职工基本信息的类(Person)。

该类含有私有数据成员:职工号、姓名、性别、家庭地址、电话。

公有成员函数:构造函数、显示人员信息、修改职工家庭地址、析构函数。

答案

  • 选择填空题

(1)    D     (2)   B     (3)   A    (4)   D     (5)    C    

(6)    A     (7)   D     (8)   D    (9)   C     (10)   B  

二、阅读以下程序,写出其运行结果(15分,每小题5分)

1、

n=10,sum=10

n=20,sum=30

n=30,sum=60

2、

Constructor1

Constructor2

i=0

i=30

Destructor  30

Destructor  0

3、

a=1,b=2

x=5,y=6

a=6,b=9

三、阅读以下程序并简要叙述其功能

1、

idx=5,a[5]=91

func()函数用于寻找数组中的最大值元素,将该元素的下标通过参数返回,并返回其地址值。

2、

输出:CDEFG

功能:以n为起始位置返回str1中长度为length的子串。

3、

       rect():不带参数的构造函数;

       rect(double a,double b):带参数的构造函数,用参数初始化私有成员;

       double showlength():求矩形的周长;

       double area():求矩形的面积;

       rect tlength(rect r2):把当前矩形对象与参数矩形对象合并成一个新的矩形;

四、阅读以下程序并填空(填上正确的语法成分),使其成为完整的程序

(1)       !head               

(2)    head->number==number  

(3)    pGuard->next;          

(4)    p=pGuard->next;       

(5)    pGuard->next=p->next; 

五、编程题

1.

#include <fstream.h>

#include <iostream.h>

void main()

{

    char filename1[32],filename2[32],newfilename[32];

    cout << "输入两个将连接的文件名:";

    cin >>filename1>>filename2;

    ifstream  file1(filename1,ios::nocreate);

    ifstream  file2(filename2,ios::nocreate);

    if(file2.fail()||file2.fail())//测试打开结果

    {

        cerr <<"失败!!!"<<endl;

    }

    else

    {

        cout << "输入新的文件名:";

        cin >>newfilename;

        ofstream  newfile(newfilename); //打开文件,如果不存在则创建

        char str[128];

        while (!file1.eof())

        {

            file1.getline(str,sizeof(str));

            newfile<<str<<endl;

        }

        while (!file2.eof())

        {

            file2.getline(str,sizeof(str));

            newfile<<str<<endl;

        }

        file1.close();      //关闭文件

        file2.close();

        newfile.close();

    }

}

2.

//Person.h

class Person

{

public:

        Person(char *,char*,char,char*,char*);

        void display();

        void changeAdd(char*);

        ~Person();

private:

        char number[10];

        char name[10];

        char sex;

        char address[40];

        char telno[10];

};

//person.cpp

#include "Person.h"

#include <string.h>

#include <iostream.h>

Person::Person(char *no,char*na,char x,char*add,char*tel)

{

        cout<<"Constructing ..."<<endl;

        strncpy(number,no,sizeof(number));

        number[sizeof(number)-1]='\0';

        strncpy(name,na,sizeof(name));

        name[sizeof(name)-1]='\0';

        sex=x;

        strncpy(address,add,sizeof(address));

        number[sizeof(address)-1]='\0';

        strncpy(telno,tel,sizeof(telno));

        number[sizeof(telno)-1]='\0';

cout<<number<<","<<name<<","<<sex<<","<<address<<","<<telno<<endl;

}

void Person::display()

{

cout<<number<<","<<name<<","<<sex<<","<<address<<","<<telno<<endl;

}

void Person::changeAdd(char* newadd)

{

        strncpy(address,newadd,sizeof(address));

        number[sizeof(address)-1]='\0';

}

Person::~Person()

{

        cout<<"Destructing ..."<<endl;

}

//personApp.cpp

#include "Person.h"

#include <iostream.h>

void main()

{

        Person p("2002008","Frank",'M',"zhangshan road","2300088");

        p.changeAdd("ci tong road");

        p.display();

}

标签:Sample,cout,int,void,number,C++,char,答案,试题
From: https://blog.csdn.net/workflower/article/details/142937258

相关文章