cpp题目

一、简答题。(4,20,每题5)

1. void GetMemory(char *p)

{

p = (char *)malloc(100);

}

void Test(void)

{

char *str = NULL;

GetMemory(str);    

strcpy(str, "hello world");

printf(str);

}

请问运行Test函数会有什么样的结果?

最佳答案是:程序崩溃。因为GetMemory并不能传递动态内存, Test函数中的str一直都是 NULLstrcpy(str, "hello world");将使程序崩溃。

 

 

2. char *GetMemory(void)

{  

char p[] = "hello world";

return p;

}

void Test(void)

{

char *str = NULL;

str = GetMemory();   

printf(str);

}

请问运行Test函数会有什么样的结果?

有可能会导致程序崩溃,因为函数GetMemory

返回的是局部空间的地址。

3. void GetMemory(char **p, int num)

{

*p = (char *)malloc(num);

}

void Test(void)

{

char *str = NULL;

GetMemory(&str, 100);

strcpy(str, "hello");  

printf(str);   

}

请问运行Test函数会有什么样的结果?

 

最佳答案是:能够输出hello,内存泄漏

 

 

4. void Test(void)

{

char *str = (char *) malloc(100);

    strcpy(str, "hello");

    free(str);     

    if(str != NULL)

    {

      strcpy(str, “world”);

printf(str);

}

}

请问运行Test函数会有什么样的结果?

最佳答案是:篡改动态内存区的内容,后果难以预料,非常危险。因为free(str);之后,str成为野指针, if(str != NULL)语句不起作用。

 

 

 

 

 

 

 

 

 

 

二、编程题(共10分)

 

. 已知strcpy函数的原型是

char *strcpy(char *strDest, const char *strSrc);

其中strDest是目的字符串,strSrc是源字符串。

(1)不调用C++/C的字符串库函数,请编写函数 strcpy

(2)strcpy能把strSrc的内容复制到strDest,为什么还要char * 类型的返回值?

 

最佳答案是:

char * strcpy(char * strDest,const char * strSrc)

{

  char * strDestCopy=strDest; //[3]

  if((strDest==NULL)||(strSrc==NULL)) //[1]

    throw "Invalid argument(s)"; //[2]

  while((*strDest++=*strSrc++)!='\0'); //[4]

  return strDestCopy;

}

返回strDest的原始值使函数能够支持链式表达式,增加了函数的附加值。同样功能的函数,如果能合理地提高的可用性,自然就更加理想

三、使用继承实现以下几个类

Person(5分)

Student(10分)

GarduateStudent(15分)

#include <iostream>

#include <cstring>

using namespace std;

 

class Person

{

public:

    Person()

    {

        next_id++;

        name = new char[1];

        strcpy(name, "");

        id = next_id;

    }

    Person(char* _name)

    {

        next_id++;

        name = new char[strlen(_name)+1];

        strcpy(name, _name);

        id = next_id;

    }

   

    Person(const Person& _p)

    {

        next_id++;

        name = new char[strlen(_p.name)+1];

        strcpy(name, _p.name);

        id = next_id;

    }

   

    const Person& operator=(const Person& p)

    {

        if (this != &p)

        {

            delete []name;

            name = new char[strlen(p.name)+1];

            strcpy(name, p.name);

            id = p.id;

        }

       

        return *this;

    }

   

    ~Person()

    {

        delete [] name;

        name = NULL;

    }

   

    void print()

    {

        cout<<"名字为:"<<name<<"id:"<<id<<endl;

    }

   

private:

    char* name;

    int id;

    static int next_id;

};

 

int Person::next_id = 0;

 

class Student:public Person

{

public:

    Student()

    {

        label = new char[1];

        strcpy(label, "");

        grade = 0;

    }

    Student(char* _name,char* _label,int _grade):Person(_name)

    {

        label = new char[strlen(_label)+1];

        strcpy(label, _label);

        grade = _grade;

    }

   

    Student(const Student& s):Person(s)

    {

        label = new char[strlen(s.label)+1];

        strcpy(label, s.label);

        grade = s.grade;

    }

   

   

    const Student& operator=(const Student& s)

    {

        if (this != &s)

        {

            Person::operator=(s);

            delete [] label;

            label = new char[strlen(s.label)+1];

            strcpy(label, s.label);

            grade = s.grade;

        }

        return *this;

    }

   

    ~Student()

    {

        delete [] label;

        label = NULL;

    }

   

    void print()

    {

        Person::print();

        cout<<"label:"<<label<<"grade:"<<grade<<endl;

    }

   

private:

    char* label;

    int grade;

};

 

class GraduateStudent:public Student

{

public:

    GraduateStudent()

    {

        message = new char[1];

        strcpy(message, "");

        salary = 1000;

    }

   

    GraduateStudent(char* _name,char* _label,int _grade,char* _message,int _salary):Student(_name,_label,_grade)

    {

        message = new char[strlen(_message)+1];

        strcpy(message, _message);

        salary = _salary;

    }

   

    GraduateStudent(const GraduateStudent& g):Student(g)

    {

        message = new char[strlen(g.message)+1];

        strcpy(message, g.message);

        salary = g.salary;

    }

   

    const GraduateStudent& operator=(const GraduateStudent& g)

    {

        if (this != &g)

        {

            Student::operator=(g);

            delete [] message;

            message = new char[strlen(g.message)+1];

            strcpy(message, g.message);

            salary = g.salary;

        }

       

        return *this;

    }

   

    ~GraduateStudent()

    {

        delete [] message;

        message = NULL;

    }

   

    void print()

    {

        Student::print();

        cout<<"message:"<<message<<"salary:"<<salary<<endl;

    }

   

private:

    char* message;//盛放毕业信息

    int salary;

};

 

int main()

{

    Person p;

    Person p1("zhangsan");

    Person p2(p1);

    p2.print();

    p2 = p;

    p1.print();

    cout<<"test student"<<endl;

    Student s;

    Student s1("lisi","student1",100);

    Student s2(s1);

    s2.print();

    s2=s;

    s1.print();

    cout<<"test graduatestudent"<<endl;

    GraduateStudent gs;

    GraduateStudent gs1("wangwu","student2",99,"biyesheng",10000);

    GraduateStudent gs2(gs1);

    gs2.print();

    gs2 = gs;

    gs1.print();

    return 0;

}

 

四、实现以下的String类  

class  String

{

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

friend String operator+(const String&, const String&);

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

public:

String();

String(char*);

String(const String&);

const String& operator=(const String&);

const String& operator+=(const String&);

const char& at(int) const; // 如果越界要抛出异常

char& at(int); // 如果越界要抛出异常

const char& operator[](int)const;

char&operator[](int);

int length();

private:

char* rep; // 存放字符串

int len; // 字符串的长度

};

 

最佳答案是:

#include <iostream>

#include <cstring>

using namespace std;

 

int     strcmp1(const char * p, const char * q)

{

    while (*p==*q && *p!='\0' && *q!='\0')

    {

        p++;

        q++;

    }

   

    if (*p==*q)

    {

        return 0;

    }

    return *p-*q;

}

 

char* strcat1(char * p, const char * q)

{

    if (p==NULL || q==NULL)

    {

        throw "invalid arguments";

    }

    char* pt = p;

    while (*p!='\0')

    {

        p++;

    }

    while (*q != '\0')

    {

        *p++ = *q++;

    }

    *p = '\0';

   

    return pt;

}

 

class String

{

public:

    String()

    {

        rep = new char[1];

        strcpy(rep, "");

        size = 0;

    }

    String(char* _rep)

    {

        size = (int)strlen(_rep);

        rep = new char[size+1];

        strcpy(rep, _rep);

    }

   

    String(const String& s)

    {

        size = s.size;

        rep = new char[size+1];

        strcpy(rep, s.rep);

    }

   

    const String& operator=(const String& s)

    {

        if (this!=&s)

        {

            delete [] rep;

            rep = new char[s.size+1];

            strcpy(rep, s.rep);

            size = s.size;

        }

       

        return *this;

    }

   

    const char& operator[](int index)const

    {

        return rep[index];

    }

   

    char& operator[](int index)

    {

        return rep[index];

    }

   

    const char& at(int index) const

    {

        if(index >= size||index<0)

            throw "越界";

        return rep[index];

    }

   

    char& at(int index)

    {

        if(index >= size||index<0)

            throw "越界";

        return rep[index];

    }

   

    const String& operator+=(const String& s)

    {

        *this = *this + s;

        return *this;

    }

   

    friend ostream& operator<<(ostream& out,const String& s)

    {

        out<<s.size<<endl;

        out<<s.rep<<endl;

        return out;

    }

    //cin>>p

    //

    friend bool operator<(const String& s1, const String& s2)

    {

        if(strcmp1(s1.rep,s2.rep) == -1)

            return true;

        return false;

    }

    friend istream& operator>>(istream& in,String& s)

    {//gets(a);

        cout<<"请输入您所输入的字符串的长度"<<endl;

        in>>s.size;

        delete [] s.rep;

        s.rep = new char[s.size+1];

        cout<<"请输入您所输入的字符串"<<endl;

        in>>s.rep;

        return in;

    }

   

    friend bool operator==(const String& s1,const String& s2)

    {

       

        return !strcmp1(s1.rep, s2.rep);

    }

    //"abc"  "edf"

   

    friend const String operator+(const String& s1,const String& s2)

    {//size rep

        String tmp(s1);//rep

        //空间

        tmp.size+=s2.size;

        char* temp = tmp.rep;

        //delete []tmp.rep;

        tmp.rep = new char[tmp.size+1];

        strcat1(tmp.rep, temp);

        strcat1(tmp.rep, s2.rep);

        delete [] temp;

        return tmp;

    }

   

    int length()

    {

        return size;

    }

    

    ~String()

    {

        delete [] rep;

        rep = NULL;

    }

private:

    char* rep;

    int size;//字符串的长度

};

int main()

{

    return 0;

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值