C/C++面试题(1)

1) 实现下列代码中横线处的函数,该函数将一个字串转换中的空格变为逗号,并在该字串的最后加上点号。
例如,要求在函数myStringFun的pInputString为: abc die cad 时打印出: abc,die,cad. 该函数返回值必须为void。
void myStringFun(char * pInputString)
{
char * pStr;
___________//your function
printf("%s", pStr);
}

实现如下:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <memory.h>
 4 #include <assert.h>
 5 void myStringFun(char * pInputString);
 6 void stringProcess(char **pOutStr, const char * pInputStr);
 7 int main()
 8 {
 9     char str[] = "abc defg hijk";
10     myStringFun(str);
11     return 0;
12 }
13 
14 void myStringFun(char * pInputString)
15 {
16     char * pStr;
17     printf("Input is: %s\n", pInputString);
18     stringProcess(&pStr, pInputString);
19     printf("Output is: %s", pStr);
20 }
21 
22 void stringProcess(char **pOutStr, const char * pInputStr)
23 {
24     assert(pOutStr != NULL && pInputStr != NULL);
25     int i = 0;
26     int length = strlen(pInputStr);
27     *pOutStr = (char *)malloc((length + 2) * sizeof(char));
28     for(i=0; i < length; i++)
29     {
30         if(pInputStr[i] == ' ')
31             (*pOutStr)[i] = ',';
32         else
33             (*pOutStr)[i] = pInputStr[i];
34     }
35     (*pOutStr)[length] = '.';
36     (*pOutStr)[length+1] = '\0';
37 }

运行结果为:

C/C++面试题(1)

 

2) 实现string类的构造函数,析构函数和实现追加功能operator +=函数,并说明这个类好必须实现哪些成员函数。

 1 #include <iostream>
 2 #include <string.h>
 3 
 4 using namespace std;
 5 
 6 class MyString
 7 {
 8     private:
 9         char * pStr;
10     public:
11         MyString();
12         MyString(const char * m_pStr);
13         MyString(const MyString & m_Str);
14         ~MyString();
15         MyString & operator+=(MyString & m_Str);
16         const char * getString() const {return pStr;}
17         void show() const {cout<<pStr<<endl;}
18 
19 };
20 
21 MyString::MyString()
22 {
23     pStr = new char[1];
24     pStr[0] = '\0';
25 }
26 
27 MyString::MyString(const char * m_pStr)
28 {
29     int length = strlen(m_pStr);
30     int i = 0;
31     pStr = new char[length + 1];
32     while((pStr[i] = m_pStr[i]) != '\0')
33     {
34         i++;
35     }
36 }
37 
38 MyString::MyString(const MyString & m_Str)
39 {
40     int i = 0;
41     const char * pTmp = m_Str.pStr;
42     int length = strlen(pTmp);
43     pStr = new char[length + 1];
44     while((pStr[i] = pTmp[i]) != '\0')
45     {
46         i++;
47     }
48 }
49 
50 MyString::~MyString()
51 {
52     if(pStr != NULL)
53         delete(pStr);
54 }
55 
56 
57 MyString & MyString::operator+=(MyString & m_Str)
58 {
59     int i = 0;
60     int j = 0;
61     char * pSouStr = pStr;
62     const char * pAddStr = m_Str.pStr;
63     int length = strlen(pSouStr) + strlen(pAddStr);
64 
65     pStr = new char[length + 1];
66     while((pStr[i] = pSouStr[i]) != '\0')
67     {
68         i++;
69     }
70     while((pStr[i] = pAddStr[j]) != '\0')
71     {
72         i++;
73         j++;
74     }
75     return *this;
76 }
77 
78 
79 int main()
80 {
81     cout << "Test MyString Class: " << endl;
82     MyString str1;
83     str1.show();
84 
85     MyString str2("String2");
86     str2.show();
87 
88     MyString str3(str2);
89     str3.show();
90 
91     MyString str4("String4");
92     str4.show();
93 
94     str4 += str2;
95     str4.show();
96     return 0;
97 }

运行结果为:

C/C++面试题(1)

另外,还要实现复制构造函数。因为数据成员为指针变量,如果使用默认复制构造函数会导致复制的对象和原对象的数据成员指向同一块内存区域。


3) 实现strstr函数,返回一个字串在另一个字串中第一出现的位置。

4) 构造函数的执行顺序问题:
#include <iostream>

using namespace std;

int i;
int array[6];

class Base
{
public:
Base(){array[i++] = 3;};
virtual ~Base(){array[i++] = 4;};
void fun1(){array[i++] = 5;};
virtual void fun2(){array[i++] = 6;};
};

class Child : public Base
{
public:
Child(){array[i++] = 9;};
virtual ~Child(){array[i++] = 10;}
void fun1(){array[i++] = 11;};
virtual void fun2(){array[i++] = 12;};
};

int main()
{
int n=0;
Base * pBase = new Child();
pBase->fun1();
pBase->fun2();
delete(pBase);
for(n=0; n<6; n++)
{
cout<<array[n]<<endl;
}
return 0;
}

转载于:https://www.cnblogs.com/Jerryli/p/3929496.html

上一篇:C++面试重难点 :自实现string类


下一篇:c++ 类的构造和析构