搜罗到的觉得有用的东西
C++面试题
1. 多态的作用?
主要是两个:1. 隐藏实现细节,使得代码能够模块化;扩展代码模块,实现代码重用;2. 接口重用:为了类在继承和派生的时候,保证使用家族中任一类的实例的某一属性时的正确调用。
New delete 与malloc free 的联系与区别?
答案:都是在堆(heap)上进行动态的内存操作。用malloc函数需要指定内存分配的字节数并且不能初始化对象,new 会自动调用对象的构造函数。delete 会调用对象的destructor,而free 不会调用对象的destructor.
3.
#define DOUBLE(x) x+x ;
i = 5*DOUBLE(5);
i 是多少?
答案:i 为30.
4. 有哪几种情况只能用intialization list 而不能用assignment?
答案:当类中含有const、reference 成员变量;基类的构造函数都需要初始化表。
5. C++是不是类型安全的?
答案:不是。两个不同类型的指针之间可以强制转换(用reinterpret cast)。C#是类型安全的。
main 函数执行以前,还会执行什么代码?
答案:==全局对象的构造函数==会在main 函数之前执行。
描述内存分配方式以及它们的区别?
1)从==静态存储==区域分配。内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在。例如全局变量,static 变量。
2)在==栈==上创建。在执行函数时,函数内局部变量的存储单元都可以在栈上创建,函数执行结束时这些存储单元自动被释放。栈内存分配运算内置于处理器的指令集。
3) 从==堆==上分配,亦称动态内存分配。程序在运行的时候用malloc 或new 申请任意多少的内存,程序员自己负责在何时用free 或delete 释放内存。动态内存的生存期由程序员决定,使用非常灵活,但问题也最多。
struct 和 class 的区别答案:struct 的成员默认是公有的,而类的成员默认是私有的。struct 和 class 在其他方面是功能相当的。
从感情上讲,大多数的开发者感到类和结构有很大的差别。感觉上结构仅仅象一堆缺乏封装和功能的开放的内存位,而类就象活的并且可靠的社会成员,它有智能服务,有牢固的封装屏障和一个良好定义的接口。既然大多数人都这么认为,那么只有在你的类有很少的方法并且有公有数据(这种事情在良好设计的系统中是存在的!)时,你也许应该使用 struct 关键字,否则,你应该使用 class 关键字。
9.当一个类A 中没有生命任何成员变量与成员函数,这时sizeof(A)的值是多少,如果不是零,请解释一下编译器为什么没有让它为零。(Autodesk)
答案:肯定不是零。举个反例,如果是零的话,声明一个class A[10]对象数组,而每一个对象占用的空间是零,这时就没办法区分A[0],A[1]…了。
- 在8086 汇编下,逻辑地址和物理地址是怎样转换的?(Intel)
答案:通用寄存器给出的地址,是段内偏移地址,相应段寄存器地址*10H+通用寄存器内地址,就得到了真正要访问的地址。
比较C++中的4种类型转换方式?
重点是static_cast, dynamic_cast和reinterpret_cast的区别和应用
11.分别写出BOOL,int,float,指针类型的变量a 与“零”的比较语句。
答案:
BOOL : if ( !a ) or if(a)int : if ( a == 0)
float : const EXPRESSION EXP = 0.000001
if ( a < EXP && a >-EXP)pointer : if ( a != NULL) or if(a == NULL)
12.请说出const与#define 相比,有何优点?
答案:1) const 常量有数据类型,而宏常量没有数据类型。编译器可以对前者进行类型安全检查。而对后者只进行字符替换,没有类型安全检查,并且在字符替换可能会产生意料不到的错误。
2) 有些集成化的调试工具可以对const 常量进行调试,但是不能对宏常量进行调试。
- 简述数组与指针的区别?
数组要么在静态存储区被创建(如全局数组),要么在栈上被创建。指针可以随时指向任意类型的内存块。
(1)修改内容上的差别
char a[] = “hello”;
a[0] = ‘X’;
char *p = “world”; // 注意p 指向常量字符串
p[0] = ‘X’; // 编译器不能发现该错误,运行时错误
(2) 用运算符sizeof 可以计算出数组的容量(字节数)。sizeof(p),p 为指针得到的是一个指针变量的字节数,而不是p 所指的内存容量。C++/C 语言没有办法知道指针所指的内存容量,除非在申请内存时记住它。注意当数组作为函数的参数进行传递时,该数组自动==退化==为==同类型==的指针。
char a[] = "hello world";
char *p = a;
cout<< sizeof(a) << endl; // 12 字节
cout<< sizeof(p) << endl; // 4 字节
计算数组和指针的内存容量
void Func(char a[100]) {
cout<< sizeof(a) << endl; // 4 字节而不是100 字节
}
14.类成员函数的重载、覆盖和隐藏区别?
答案:
a. 成员函数被重载的特征:
(1)相同的范围(在同一个类中);
(2)函数名字相同;
(3)参数不同;
(4)virtual 关键字可有可无。
b. 覆盖是指派生类函数覆盖基类函数,特征是:
(1)不同的范围(分别位于派生类与基类);
(2)函数名字相同;
(3)参数相同;
(4)基类函数必须有virtual 关键字。
c. “隐藏”是指派生类的函数屏蔽了与其同名的基类函数,规则如下:
(1)如果派生类的函数与基类的函数同名,但是参数不同。此时,不论有无virtual关键字,基类的函数将被隐藏(注意别与重载混淆)。
(2)如果派生类的函数与基类的函数同名,并且参数也相同,但是基类函数没有virtual 关键字。此时,基类的函数被隐藏(注意别与覆盖混淆)
There are two int variables: a and b, don‘t use “if”, “? :”, “switch”or other judgement statements, find out the biggest one of the two numbers.
答案:(( a + b ) + abs( a - b )) / 2
如何打印出当前源文件的文件名以及源文件的当前行号?
答案:
cout << __FILE__ ;
cout<<__LINE__ ;
FILE和LINE是系统预定义宏,这种宏并不是在某个文件中定义的,而是由编译器定义的。
main 主函数执行完毕后,是否可能会再执行一段代码,给出说明?
答案:可以,atexit 注册一个函数,它会在main 之后执行int fn1(void), fn2(void), fn3(void), fn4 (void);
_onexit is MS extension. Use ==atexit== for portability.
#include <iostream>
#include <cstring>
#include <stdlib.h>
using namespace std;
void fn1();
void fn2();
void fn3();
void fn4();
int main( int argc,char * argv[])
{
string str("Hello world!");
cout<<str<<endl;
atexit(fn1);
atexit(fn2);
atexit(fn3);
atexit(fn4);
cout<<"This is executed first"<<endl;
return 0;
}
void fn1(){
cout<< " next"<<endl;
}
void fn2(){
cout<< " executed";
}
void fn3(){
cout<< "is";
}
void fn4(){
cout<< "This ";
}
- 如何判断一段程序是由C 编译程序还是由C++编译程序编译的?
#ifdef __cplusplus
cout<<"c++";
#else
cout<<"c";
#endif // __cplusplus
- 链表题:一个链表的结点结构
(1). 已知链表的头结点head,写一个函数把这个链表逆序 ( Intel)。
(2). 已知两个链表head1 和head2 各自有序,请把它们合并成一个链表依然有序。(保留所有结点,即便大小相同)
(3). 已知两个链表head1 和head2 各自有序,请把它们合并成一个链表依然有序,这次要求用递归方法进行。 (Autodesk)
#include <iostream>
#include <cstring>
#include <stdlib.h>
using namespace std;
struct Node
{
int data ;
Node *next ;
};
typedef struct Node Node ;
Node * ReverseList(Node *head) //
{
//p1---->p2------>p3----> p3---->p2----->p1
if ( head == NULL || head->next == NULL )
return head;
Node *p1 = head ;//p1 is head
Node *p2 = p1->next ;// help next data need to reverse
Node *p3 = p2->next ;
p1->next = NULL ;
while ( p3 != NULL )
{
p2->next = p1 ;
p1 = p2 ;
p2 = p3 ;
p3 = p3->next ;
}
p2->next = p1 ;
head = p2 ;
return head ;
}
Node * Merge(Node *ptrHead1 , Node *ptrHead2)
{
//DO NOT change the original sequence
Node* head1 = ptrHead1;
Node* head2 = ptrHead2;
if ( head1 == NULL){
return head2 ;
}
if ( head2 == NULL){
return head1 ;
}
Node *head = NULL ;
Node *p1 = NULL;
Node *p2 = NULL;
//initial
if ( head1->data < head2->data )
{
head = head1 ;
p1 = head1->next;
p2 = head2 ;
}
else
{
head = head2 ;
p2 = head2->next ;
p1 = head1 ;
}
Node *pcurrent = head ;
while ( p1 != NULL && p2 != NULL)
{
if ( p1->data <= p2->data )
{
pcurrent->next = p1 ;
pcurrent = p1 ;
p1 = p1->next ;
}
else
{
pcurrent->next = p2 ;
pcurrent = p2 ;
p2 = p2->next ;
}
}
if ( p1 != NULL )
pcurrent->next = p1 ;
if ( p2 != NULL )
pcurrent->next = p2 ;
return head ;
}
Node * MergeRecursive(Node *ptrHead1 , Node *ptrHead2)
{
//DO NOT change the original sequence
Node* head1 = ptrHead1;
Node* head2 = ptrHead2;
if ( head1 == NULL )
return head2 ;
if ( head2 == NULL)
return head1 ;
Node *head = NULL ;
if ( head1->data < head2->data )
{
head = head1 ;
head->next = MergeRecursive(head1->next,head2);
}
else
{
head = head2 ;
head->next = MergeRecursive(head1,head2->next);
}
return head ;
}
void createLinkedList(Node* node,int number){
Node* temp=NULL;
Node* ptr=node;
for(int i=0;i<number;++i){
temp=new Node;
(*temp).data=i*2;
(*temp).next=(*ptr).next;
(*ptr).next=temp;
}
}
void displayLinkedList(Node* node){
Node* temp=node;
if(temp==NULL){
cout<<"NULL"<<endl;
return;
}
while(temp!=NULL){
cout<<(*temp).data<<endl;
temp=(*temp).next;
}
}
void freeLinkedList(Node* node){
if(node!=NULL)
{
Node* tempPrevious=node;
Node* temp = node->next;
while(temp!=NULL)
{
tempPrevious->next=temp->next;
free(temp);
temp=tempPrevious->next;
}
free(node);
}
}
int main( int argc,char * argv[])
{
Node* node1=new Node;
(*node1).next=NULL;
Node* node2=new Node;
(*node2).next=NULL;
createLinkedList(node1,4);//create No.1 linked list
createLinkedList(node2,8);//create No.2 linked list
cout<<"This is node1:"<<endl;
displayLinkedList(node1->next);
cout<<"This is node2:"<<endl;
displayLinkedList(node2->next);
cout<<"This is ReverseList node1:"<<endl;
Node* seqNode1 = ReverseList(node1->next);
Node* seqNode2 = ReverseList(node2->next);;
displayLinkedList(seqNode1);//display the reverse linked list
displayLinkedList(seqNode2);//display the reverse linked list
//because of merge does not use new memory to store the list......two list is damaged
cout<<"This is merge 1 and 2 :"<<endl;
Node *result1 = Merge(seqNode1,seqNode2);
displayLinkedList(result1);
//freeLinkedList(result1);
cout<<"This is merge 1 and 2 ():"<<endl;
Node *result2 =MergeRecursive(seqNode1,seqNode2);
displayLinkedList(result2);
return 0;
}
最后两个链表对原始链表进行修改。
- 如何判断一个单链表是有环的?(注意不能用标志位,最多只能用两个额外指针)
struct node { char val; node* next;}
bool check(const node* head) {} //return false : 无环;true: 有环
一种O(n)的办法就是(搞两个指针,一个每次递增一步,一个每次递增两步,如果有环的话两者必然重合,反之亦然):
bool check(const node* head) {
if(head==NULL) return false;
node *low=head, *fast=head->next;
while(fast!=NULL && fast->next!=NULL){
low=low->next;
fast=fast->next->next;
if(low==fast) return true;
}
return false;
}