您好,欢迎来到叨叨游戏网。
搜索
您的当前位置:首页2010年研究生C++试题A

2010年研究生C++试题A

来源:叨叨游戏网
课程号:SG04006

C++程序设计考试试卷A

考试形式:闭卷考试 考试时间:120分钟

所有答案答在答题纸上,试卷和答题纸一并上交存档

院系 学号 姓名 得分

题号总分
一、选择题。本10题,每3分。

1. 以下代码的输出结果是 】。

#include

int main()

{

using namespace std;

int a=true * 2;

int b=20;

cout<<"a+b="<

}

A.a+b=22

B.a+b=20

C.a+b =18

D.编译错误

2. 以下代码的输出结果是 】。

#include

const char *st = "This is a simple.\\n";

int main()

{

int len = 0;

while ( *st++ )

++len;

std::cout << len << ": " << st-18 << std::endl;

return 0;

}

A.18: This is a simple.

B.18: his is a simple.

C.18: is is a simple.

D.编译错误

3. 以下代码的输出结果是 】。

#include

int main()

{

int val=20;

const int * pval=&val;

val=40;

std::cout<<*pval;

return 0;

}

A.20

B.40

C.不确定的值

D.编译错误

4. 在创建派生类对象时,构造函数的执行顺序是【 】。

A.对象成员构造函数、基类构造函数、派生类本身的构造函数

B.派生类本身的构造函数、基类构造函数、对象成员构造函数

C.基类构造函数、派生类本身的构造函数、对象成员构造函数

D.基类构造函数、对象成员构造函数、派生类本身的构造函数

5. 下面的程序输出结果为 】。

#include

int main()

{

using namespace std;

int rats = 101;

int & rodents = rats;

rodents++;

cout << "rats = " << rats;

cout << ", rodents = " << rodents << endl;

return 0;

}

A.rats = 101, rodents = 101

B.rats = 102, rodents = 102

C.rats = 101, rodents = 102

D.rats = 101, rodents的值不确定

6. 下面的程序输出结果为 】。

#include

void ptrswap( int *&v1, int *&v2 );

int main()

{

int i = 10;

int j = 20;

int *pi = &i;

int *pj = &j;

std::cout << "Before ptrswap():\pi: "

<< *pi << "\pj: " << *pj << std::endl;

ptrswap( pi, pj );

std::cout << "After ptrswap():\pi: "

<< *pi << "\pj: " << *pj << std::endl;

std::cout<<"i: "<

return 0;

}

A.Before ptrswap(): pi: 10 pj: 20

After ptrswap(): pi: 10 pj: 20

i: 10 j: 20

B.Before ptrswap(): pi: 10 pj: 20

After ptrswap(): pi: 20 pj: 10

i: 20 j: 10

C.Before ptrswap(): pi: 10 pj: 20

After ptrswap(): pi: 20 pj: 10

i: 10 j: 20

D.Before ptrswap(): pi: 10 pj: 20

After ptrswap(): pi: 20 pj: 10

i: 20 j: 10

7. 如果classA是一个类,则下面的语句将调用 】。

classA * pc = new classA(“anonymous”);

A.构造函数classA::classA(const char * s);

B.构造函数classA::classA();

C.拷贝构造函数

D.使用赋值操作符

8. 下面是一个类声明。该类的一个正确的构造函数是 】。

#include

#include

#include

class Student

{

private:

std::string name;

std::valarray scores;

public:

...

};

A.Student() : string("Null Student"), std::valarray () {}

B.Student(const std::string & s): string(s), scores() {}

C.Student(const std::string & s): string(s), std::valarray () {}

D.Student(const std::string & s) : name(s), scores() {}

9. 下面是某个类中重载的赋值操作符。则应该加入的代码为 】。

class MyString

{

private:

char * str;

int len;

...

};

MyString & MyString::operator = (const MyString & st)

{

//加入赋值操作的代码

}

A. delete [] str;

if (this == &st)

return *this;

len = st.len;

str = new char[len + 1];

std::strcpy(str, st.str);

return *this;

B. delete [] str;

len = st.len;

str = new char[len + 1];

std::strcpy(str, st.str);

return *this;

C. if (this == &st)

return *this;

delete [] str;

len = st.len;

str = new char[len + 1];

std::strcpy(str, st.str);

return *this;

D. if (this == &st)

return *this;

len = st.len;

str = new char[len + 1];

std::strcpy(str, st.str);

return *this;

10. 关于const成员函数的说法不正确的是 】。

A. 通过把成员函数声明为const 以表明它不修改类对象

B. const 类对象能调用被声明为const 的成员函数

C. 修改类数据成员的函数声明为const 是非法的

D. 如果一个类的成员函数不修改类数据成员,就应该把声明为const 成员函数

二、简答题。简要回答以下问题。5题,每4分。

1什么是构造函数和析构函数,其用途分别是什么。

2this指针的实质是什么,举例说明其使用方法。

3.类成员使用动态内存分配机制时需要注意哪些问题。

4C++中的多态性是如何实现的,举例说明。

5.实现has-a 关系两种方式是什么,有何区别。

三、下面的代码是一个简单的类声明,请提供4个成员函数的定义。本题共12分。

class Move //描述平面上的一个点

{

private:

double x;

double y;

public:

Move(double a=0, double b=0); //1)构造函数(3分)

showmove() const; //2)显示对象当前的xy的植(3分)

Move add(const Move & m) const; //3)将mxy值与当前的xy//相加得到一个新对象并返回(3分)

reset(double a=0, double b=0); //4)重置xy的值为ab3分)

}

四、下面是一个描述字符串的类定义及其方法实现,请仔细阅读程序,补充完整,使之可以正确运行。注意:每空可以添多条语句。共10空,每空2分。

// mystring.h -- class definition

#include

using std::ostream;

using std::istream;

#ifndef MYSTRING_H_

#define MYSTRING_H_

class String

{

private:

char * str; // pointer to string

int len; // length of string

static int num_strings; // number of objects

static const int CINLIM = 80; // cin input limit

public:

// constructors and other methods

String(const char * s); // constructor

String(); // default constructor

String(const String &); // copy constructor

~String(); // destructor

int length () const { return len; }

// overloaded operator methods

String & operator=(const String &);

String & operator=(const char *);

char & operator[](int i);

const char & operator[](int i) const;

// overloaded operator friends

friend bool operator<(const String &st, const String &st2);

friend bool operator>(const String &st1, const String &st2);

friend bool operator==(const String &st, const String &st2);

friend ostream & operator<<(ostream & os, const String & st);

friend istream & operator>>(istream & is, String & st);

// static function

static int HowMany();

};

#endif

// mystring.cpp -- String class methods

#include // string.h for some

#include "mystring.h" // includes

using std::cin;

using std::cout;

// 初始化静态类成员num_strings

A

// static method

int String::HowMany()

{

return num_strings;

}

// class methods,要求动态分配字符串内存空间

String::String(const char * s)

{

B

}

String::String()

{

len = 4;

str = new char[1];

str[0] = '\\0';

num_strings++;

}

String::String(const String & st)

{

C

}

String::~String() // necessary destructor

{

D

}

// overloaded operator methods

// assign a String to a String

String & String::operator=(const String & st)

{

if (this == &st)

E

delete [] str;

len = st.len;

F

}

// assign a C string to a String

String & String::operator=(const char * s)

{

delete [] str;

len = std::strlen(s);

str = new char[len + 1];

std::strcpy(str, s);

return *this;

}

// read-write char access for non-const String

char & String::operator[](int i)

{

G

}

// read-only char access for const String

const char & String::operator[](int i) const

{

G //此处与上一空内容一样

}

// overloaded operator friends

bool operator<(const String &st1, const String &st2)

{

return (std::strcmp(st1.str, st2.str) < 0);

}

bool operator>(const String &st1, const String &st2)

{

return st2.str < st1.str;

}

bool operator==(const String &st1, const String &st2)

{

H

}

// simple String output

ostream & operator<<(ostream & os, const String & st)

{

I

}

// quick and dirty String input

istream & operator>>(istream & is, String & st)

{

J

}

五、创建一个Music类,其数据成员为作曲家(以定长数组表示),歌曲名字(以动态内存分配空间),歌曲编号,播放时间(分钟);写出适合的构造函数,拷贝构造函数,重载赋值操作符;写出显示曲目信息的接口。

在此基础上派生Classic类用于表示古典音乐,加入成员创建年代。

注意恰当的使用虚函数方法。

本题共18分。

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- gamedaodao.net 版权所有 湘ICP备2024080961号-6

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务