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.什么是构造函数和析构函数,其用途分别是什么。
2.this指针的实质是什么,举例说明其使用方法。
3.类成员使用动态内存分配机制时需要注意哪些问题。
4.C++中的多态性是如何实现的,举例说明。
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)显示对象当前的x和y的植(3分)
Move add(const Move & m) const; //(3)将m的x、y值与当前的x、y值//相加得到一个新对象并返回(3分)
reset(double a=0, double b=0); //(4)重置x、y的值为a和b(3分)
}
四、下面是一个描述字符串的类定义及其方法实现,请仔细阅读程序,补充完整,使之可以正确运行。注意:每空可以添多条语句。共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分。