一、笔试题
笔试题1:自定义函数实现strcpy()函数的功能;
char * strcpy(char * strDest, const char * strSrc)
{
if ((strDest==NULL)||(strSrc==NULL))
return NULL;
char * strDestCopy=strDest;
while ((*strDest++ = *strSrc++) != '\0');
return strDestCopy;
}
笔试题2:原地翻转带头结点的单链表;
struct Node {
int data;
Node *next;
};
void reverse(Node* head)
{
if (head == NULL||head->next == NULL) return;
Node* pre = NULL;
Node* cur = head->next;
Node* next;
while (cur)
{
next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
head->next = pre;
}
二、问答题:
谈一谈C++的多态是如何实现的?
1、C++的多态性用一句话概括就是:在基类的函数前加上virtual关键字,在派生类中重写该函数,运行时将会根据对象的实际类型来调用相应的函数。如果对象类型是派生类,就调用派生类的函数;如果对象类型是基类,就调用基类的函数;
2、虚函数,子类函数为什么能覆盖父类函数?
答:存在虚函数的类都有一个一维的虚函数表叫做虚表,类的对象有一个指向虚表开始的虚指针。虚表是和类对应的,虚表指针是和对象对应的。