期中模拟考试 题解



期中模拟考试 题解



provided by 王浩翔,17/05/08
题目来源:2017软工期中模拟




目录


理论部分


单选题

Question 2

Which function will be called?

void f(int i) {}
void f(const int i) {}
int main() {
    const int a = 0;
    f(a);
}

A. void f(int i)
B. void f(const int i)
C. compile error
D. runtime error

Standard Answer: C
因为这两个函数的参数类型都是一样的。不同的是前者是一个变量。后者是一个常量。const只是一个修饰符,在传参的时候,比如 f(5),那么这个时候编译器就不知道调用哪一个了。因为这两个都符合调用;
函数重载的条件是:作用域+返回类型+函数名+参数列表 这几个条件不能完全相同;否则就不能算重载只能算是重复定义。

Question 3

Which function will be called?

void f(int* i) {}
void f(const int* i) {}

int main() {
    const int a = 0;
    const int* p = &a;
    f(p);
}

A. void f(int* i)
B. void f(const int* i)
C. compile error
D. runtime error

Standard Answer: B
int*是指向变量的指针,而const int*指向一个常量。

Question 4

Which of the following statement about static members is NOT true?

A. A static member is declared in class.
B. A static member function cannot access any non-static member variable.
C. A static member does not have this pointer.
D. A static member must be initialized in a constructor.

Standard Answer: D
static成员在类中声明,而在类外初始化。

Question 5

Which of the following is NOT TRUE of a constructor and destructor of the same class?

A. they both have same name aside from the tilde (~) character.
B. they are both called once per object (in general).
C. they both are able to accept default arguments.
D. both are called automatically, even if not defined in the class.

Standard Answer: C
析构函数不接受参数。

Question 8

Which is NOT true about pointer and reference?

A. A reference is the entire object, while a pointer is only the address of it.
B. A pointer can point to NULL, while a reference can never point to NULL.
C. Pointers can be assigned NULL directly, while a reference cannot be assigned NULL and must be assigned at initialization.
D. A pointer is a variable that holds a memory address, while a reference has the same memory address as the item it references.

Standard Answer: A
编译器不会专门开辟空间存储一个引用,而是将发送引用的地方替换为真正的地址

Question 9

Which is true about class and struct in C++?

A. Members of a class defined with the keyword class are public by default.
B. Members of a class defined with the keywords struct are public by default.
C. Struct can not have member functions.
D. They are totally the same

Standard Answer: B
使用struct定义时成员默认是public的,而使用class定义时成员默认是private的。

Question 10

Which is correct?
(1) const object can only call const member function
(2) non-const object can only call non-const member function
(3) static member function can also be const

A. (1)
B. (2)
C. (1) and (2)
D. (1) and (3)
E. (2) and (3)
F. none

Standard Answer: A
const修饰符用于表示函数不能修改成员变量的值,该函数必须是含有this指针的类成员函数,函数调用方式为thiscall,而类中的static函数本质上是全局函数,调用规约是__cdecl或__stdcall,不能用const来修饰它。

Question 11

What’s the value of a and b after constructor?

class A {
public:
    A() : b(1), a(b+1) {}
private:
    int a;
    int b;
};

A. 2 1
B. 1 2
C. undefined-value 1
D. undefined-value undefined-value

Standard Answer: C
初始化顺序并不是根据初始化列表中出现的顺序,而是根据声明的顺序来初始化。,因此初始化a时b是未知的。

Question 14

What’s the output of the code ?

class A {
public:
    A() { cout << "A"; }
};
class B {
public:
    B() { cout << "B"; }
};
class C : public A {
public:
    C() { cout << "C"; }
private:
    static B b;
};
B C::b;
int main() {
    C c;
    return 0;
}

A. ABC
B. CBA
C. BAC
D. ACB

Standard Answer: C
构造函数调用顺序:虚拟继承>继承>参数列表>自身

Question 20

Is-a relationship between the classes is shown through :

A. Inheritance
B. Container classes
C. Polymorphism
D. encapsulation

Standard Answer: A
is-a( 是 “a” )表示的是属于关系。比如兔子属于一种动物(继承关系)。
has-a( 有 “a” ) 表示组合、包含关系。

实验部分


Problem A - Copy Constructor

  • 题目大意
    实现一个简易的字符串类
class FUN
{
  private:
    char *str;
  public:
    FUN(char *s);        //拷贝s到str
    FUN(const FUN& C);   //拷贝C.str到str
    ~FUN()
    {
        delete[]str;
    }
    void show();         //输出str中储存的字符串
};
  • 思路
    简单的签到题,注意内存的申请与释放即可

  • 代码

FUN::FUN(char *s)
{
  str = new char[1000];
  memcpy(str,s,strlen(s));
}
FUN::FUN(const FUN& C)
{
  str = new char[1000];
  memcpy(str,C.str,strlen(C.str));
}
void FUN::show()
{
  cout<<str<<endl;
}


Problem B - A Static Function

  • 题目大意
    在类中创建一个静态变量(data)来记录当前创建的该类数量

  • 思路
    注意在类外对data进行初始化

  • 代码

class Exp
{
static int data;
  public:
    Exp()
    {
      data++;
    }
    ~Exp()
    {
      data--;
    }
    static int get_Exp()
    {
      return data;
    }
};
int Exp::data = 0;


Problem C - A Class with Dual Role

  • 题目大意
    B是D的基类,完成D类使程序正常运行并输出以下结果
class B{
  private:
    int x;
    int y;
  public:
    B(int a , int b){
        x = a;
        y = b;
    }
    void print() const {
        cout << x << ", " << y << endl;
    }
};
class D: public B{
  private:
    B member;
    int c;
  public:
    // Your code will be included here.
};
int main(){
    int i, j, m, n, k;
    cin>>i>>j>>m>>n>>k;
    D(i, j, m, n, k).print();
}

Input

6 3 4 5 8

Output

Printing from Base:
6, 3
Printing from member:
4, 5
Printing from D field:
8

  • 思路
    You need to define a constructor for D and redefine the print() function for D.

  • 代码

D::D(int i,int j,int m,int n,int c):B(i,j), member(B(m,n)), k(c){}
void D::print() const{
    cout << "Printing from Base:" << endl;
    B::print();
    cout << "Printing from member:" << endl;
    member.print();
    cout << "Printing from D field:" << endl;
    cout << k;
}


Problem D - String (eden)

  • 和做过的Simulate std::string(eden)相似,实现一个自定义string类

  • 代码

class String {  
  public:  
    String(){
        _size = 0;
        _buff = new char[1001];
        _buff[0] = '\0';
    }
    explicit String(const char *src){
        _size = strlen(src);
        _buff = new char[1001];
        memcpy(_buff, src, _size+1);
    }
    String(const String &src){
        _size = src._size;
        _buff = new char[1001];
        memcpy(_buff, src._buff, _size+1);
    } 
    ~String(){
        delete[] _buff;
    }
    String& operator=(const String& src){
        _size = src._size;
        memcpy(_buff, src._buff, _size+1);
        return *this;
    }  
    const char* c_str() const{
        return _buff;
    }
    inline char& operator[](int i){
        return _buff[i];
    }
    friend ostream& operator<<(ostream& os, const String& src);
  private:  
    char *_buff;  
    int _size;  
};
ostream& operator<<(ostream& os, const String& src){
    cout << src._buff;
    return os;
}


Problem E - Student

  • 原题
    You are given a defintion of class Person.
    Person has :
    two member variables, name and age
    two overload function set, change the member variables accroding to the parameter
    a function sayHi, print some messages
    (see Person.h for detail)
    You should implement two class Date and Student

class Date :

class Date {
  private:
    int _year;
    int _month;
    int _day;
  public:
    Date(int y, int m, int d);
    Date(string dateString); // the format of dateString is like "2017-5-7"
    int getYear() const;
    void setYear(int y);
    int getMonth() const;
    void setMonth(int m);
    int getDay() const;
    void setDay(int d);
    bool operator==(Date& other) const;
    bool operator<(Date& other) const;
    bool operator>(Date& other) const;
    std::string toString() const; // return a string like "year-month-day"
};

Student derive from Person, it has:

a new member variables, graduateDate, means when the Student graduate
a overloaded function set(Date d), which changes the graduateDate to parameter d
a overrided function sayHi, it should do the same thing as sayHi in Person first, and then:
if its graduateDate > today(2017-5-7), output “I will graduate on year-month-day.”
if its graduateDate == today(2017-5-7), output “I graduated today!”
if its graduateDate < today(2017-5-7), output “I have graduated on year-month-day.”

  • 题目大意
    实现题目中给出的Date类和Student类

  • 思路
    此题主要考察类的继承。
    在需要重载基类的同名函数时,回忆一下 namespace 的三种用法,其中一种称为“using declaration/使用声明”,这里可以用上类似的代码using BaseName::functionName(很多情况下,class/struct域,和一个namespace有相同的功能)

  • 代码

class Date {
  private:
    int _year;
    int _month;
    int _day;
  public:
    Date(int y, int m, int d){
        _year = y;
        _month = m;
        _day = d;
    }
    Date(string dateString){
        int i;
        _year = _month = _day = 0;
        for(i = 0; dateString[i] != '-'; i++)
            _year = _year*10 + dateString[i] - '0';
        for(i = i+1; dateString[i] != '-'; i++)
            _month = _month*10 + dateString[i] - '0';
        for(i = i+1; i < dateString.size(); i++)
            _day = _day*10 + dateString[i] - '0';
    }
    int getYear() const{
        return _year;
    }
    void setYear(int y){
        _year = y;
    }
    int getMonth() const{
        return _month;
    }
    void setMonth(int m){
        _month = m;
    }
    int getDay() const{
        return _day;
    }
    void setDay(int d){
        _day = d;
    }
    bool operator==(Date& other) const{
        if(_year == other._year && _month == other._month && _day == other._day)
            return true;
        return false;
    }
    bool operator<(Date& other) const{
        if(_year < other._year)
            return true;
        if(_year > other._year)
            return false;
        if(_month < other._month)
            return true;
        if(_month > other._month)
            return false;
        return _day < other._day;
    }
    bool operator>(Date& other) const{
        if(_year > other._year)
            return true;
        if(_year < other._year)
            return false;
        if(_month > other._month)
            return true;
        if(_month < other._month)
            return false;
        return _day > other._day;
    }
    std::string toString() const{
        stringstream s;
        s << _year << "-" << _month << "-" << _day;
        return s.str();
    }
};
class Person {
  public:
    Person(string name, int age) : name(name), age(age) {}
    virtual void sayHi() const {
        cout << "Hi, My name is " << name << ". I'm " << age << " years old." << endl;
    }
    void set(int age) {
        this->age = age;
    }
    void set(string name) {
        this->name = name;
    }
  private:
    string name;
    int age;
};
class Student:public Person{
  private:
    Date graduateDate;
  public:
    using Person::set;
    Student(string name, int age, Date d):Person(name, age), graduateDate(d){
    }
    void set(Date d){
        this->graduateDate = d;
    }
    void sayHi() const{
        Person::sayHi();
        Date today("2017-5-7");
        if(graduateDate > today)
            cout << "I will graduate on " << graduateDate.toString() << "." << endl;
        else if(graduateDate == today)
            cout << "I graduated today!" << endl;
        else
            cout << "I have graduated on " << graduateDate.toString() << "." << endl;
    }
};


试题下载


猜你喜欢

转载自blog.csdn.net/weixin_36326096/article/details/71403748