1.   compared to prefix  ++, postfix increment needs one more step to create a temporary variable? what's more , the return type is not reference, there will be another

  temp var being created to store the return value.

int & operator++(int &a) { ++a; return a;}
int operator++(int &a){int b = a; ++a; return b;}

2.   what's the difference between using quotes ("") and angle brackets(<>) when include file?

  the first file location to start searching is current folder, then go to default folder, which can be set through env variables

3.   difference among cerr, clog and cout?

  cout  is standard output which connected to console;

  cerr is same to cout but for error message, clog is also for err msg, but it does have a buffer.

4.    static language and dynamic language

  A language is statically typed if the type of a variable is checked at compile time, this enable all checking be done by compiler.

  A language is dynamically typed if the type is associated with run-time values, and not named variables/fields/etc. thus we don't have to specify  the type every

  time we use a variable

5.    how to compare two floats/doubles

bool areEqualRel(float a, float b, float epsilon) {
    return (fabs(a - b) <= epsilon * std::max(fabs(a), fabs(b)));
}

6.   what is undefined behavior?

  the behavior depends on the compiler implementation for the code, systems it runs on... , it's importable

6.1  initialization and assignment

  for built-in types, they all assign variable with certain value (an uninitialized variable contains garbage). but for user-define types, sometimes, initialization

  should allocate mem for pointer, while assignment should delete the exiting mem and allocate a new one.

7.   define and declare

  define will allocate memory for the variable, declare not.

  with prefix keyword extern, to declare a variable, which defined in other place., complier don't have to know where it's located. useful when its share

  among several modules.

8.    function of keyword const

  const indicate complier that this variable will not be modified, it can do some optimization for it. but it doesn't mean we can't modify the variable indirectly, it depends on

  which segment compiler put it. for example, if it's stored in RO segment, OS will prevent any attempt to change it.

  it's useful to prevent magic number, which may repeat many times.

  all global default is extern,  but a const global variable has a file scope, internal linakge

9.    situation where union is used

  union are used to construct a discriminator

structmy_variant_t{
    int type;
    union{
        char char_value;
        short short_value;
        int int_value;
        long long_value;
        float float_value;
        double double_value;
        void* ptr_value;
    };
};
* construct a new float variant instance */
void init_float(structmy_variant_t* v,float initial_value){
    v->type = VAR_FLOAT;
    v->float_value = initial_value;
}
 
/* Increments the value of the variant by the given int */
void inc_variant_by_int(structmy_variant_t* v,int n){
    switch(v->type){
    case VAR_FLOAT:
        v->float_value += n;
        break;
 
    case VAR_INT:
        v->int_value += n;
        break;
    ...
    }
}

10.   how to use enumerator

  enumerator is a type!! enum class is introduced in c++11; usually we will add a scope to enum, rather than make it global.

structDays
{
   enum type
   {
      Saturday,Sunday,Tuesday,Wednesday,Thursday,Friday
   };
};
 
Days::type day =Days::Saturday;
if(day ==Days::Saturday)

11.   difference between #define and typedef

  #define can do a lot of things besides typedef, typedef only do typedef, what's more , typedef have its scope!!

12.   why there is a colon in the end of definition of class?

  see code< int c; class A { int b; } c = 7; > compiler will mistake c as an object of class A;

13.   getline will drop the std::endl it received and store other chars into buffer.

  strlen will not count on the char '\0', similar  to string.size()

  if char a[] = "he", then a[2] = '\0'; to access array with index outside bound causes undefined behavior.

14.   difference between #include "string.h" and #include <cstring>

  the first one is in a C-type way, contents of both are same, but functions in the first one are not in namespace std!!

15.   when do comparison in for() statement, why != is preferred than < ?

  NOT EQUAL can be used to compared index array and iterators as well.

16.   array is deprecated to use when vector is enough

17.   why size_t matters

  max value of size_t is defined as the largest number of element a array can have in certain OS. portable and readable

18.   what's NULL

  NULL is a preprocess sign, don't defined in the namespace std

19.   reinterpret_cast: keep the original bits and treat it as another type

20.   when parameter is an array reference , its size should be fixed!!!               int  func ( int ( &int )[10]);

21.   return result of function will be used to initialize a temporary variable, if the type is not a reference

22.   why func1 in subclass B will override same one in base class A;

  same to local variable hide global, every class has a scope. compiler will start to find its suitable implement from its local scope

23.   why ostream can't be copied!

  ostream works like pipe, which connect its data source (usually buffers) and it’s destination, to copy an ostream will just get an empty pipe, no data will obtained.

  That why all functions with parameters of type ostream should take a reference.

24.   List all Container types in C++

  A container is a holder object that stores a collection of other objects (its elements). They are implemented as class templates, which allows a great flexibility

  in the types supported as elements.

i)     Sequence containers:

Vector : random access, push/pop_back(), it’s inefficient to insert data at other position;

Deque : random access, push/pop_back & front, inefficient to insert data at other position

List      : efficient to insert data, provide only linear access

Array   : fixed-size array class

ii)    Associative containers

std::sets contain only the key, while in std::map there is an associated value,If you want to build a dictionary of all the words that appear in a text, you could use a std::set<std::string>, but if you also want to count how many times each word appeared

iii)   container adapeter

Container adaptors are not full container classes, but classes that provide a specific interface relying on an object of one of the container classes

Stack  : first in last out

Queue :   first in first out

Priority_queue: the greatest element among the remaining ones always keep in the top;

25.   the implemention of reference is indeed a pointer, but we can’t depend it  in the code

26.   why should we initialize member variables in the constructor rather assign values to them?

  No mater whether we initialize varialbles or not, it will be initialized( allocate mem and assign value if it have)

  But for reference, constant variable, class object without default constructor, they must be initialized, and initialization order is same to that them declared

27.   synthesized default constructor will be created only when there is no other constructor

28.   what is indirect type conversion ( class)

  Class A have constructor with parameter of type B, func1 of class A takes a parameter of class A, if we pass an object of class B, it will be converted to create

  a temp object of class A; we can use explicit to prevent this

  another method: define and conversion operator: operator TYPE(), no paremeters and return type

  but when there are non-member operator function and member operator function, the last one was first used when compile

29.  static is a global but has special access control, if a class member is static ,it should be initialized outside class,mem is allocated when it’s initialized.

30.  when should we define copy constructor/ assignment operator fun

  when we should allocate resource for class (member data), such as deep copy, if there is member variable which is a pointer.

31.  friendship can’t be inherited, classic friend function, ostream & opearator<<(ostream &, const class &b)

  if same function exits in both base class and subclass, it works, but no polymorphism will happen,

  constructor will create an virtual-table( or several v-table when there are several base-class)

32.  difference between static member function and non-static member function?

  Compared to other normal function, class member function has a narrower scope and has pointer this as its

  Parameter (static member function doesn’t)

  All functions are a segment of machine-code. Its scope is used to help compiler to find its address.

  anywhere in code a function is called, it will replaced by the function code or function address

33.  If D inherited function1 from base classes C and B, both inherited from Class A?

  Then D do have inherited two versions of function1, both are of different scope. If we just call function1 in class D, then compiler will fail to help to decide

  which one should be used.

34.  What is member function ?

  same to global function, with a scope helping compiler to get its address. stored in text segemt

35.  why constructor and assignment function should not be virtual?

  When compile, to construct an object, a constructor needs the exact type of the object it is to create and it’s size. Before call constructor,

  there is no virtual table and no pointer to object (haven’t be created).

  assignment operator is not inherited. It may be re-defined (with different parameter type)  by the implementer of the derived class or else it is automatically

  synthesized by the compiler, so there's not much point in declaring it virtual.

36.  Pure virtual function and virtual function

  the only difference is that if a class has a pure virtual function, then it's abstract class, can't be instantiated.

  if subclasses don't define the implementation, then this subclass can't be instantiated as well.

37.  virtual inheritance

  used to solve diamond problem. virtual inheritance will create a new special virtual table.

38.  procedure to create and destroy an subclass object

  assume class B inherited from class A, when construct an object of class B, it will call constructor of class A fist to create an A object and then call B constructor;

   when destroy B object, call destructor of B, and then its type become type A, call destructor of A.

39.   overload happens in the same scope, function with name (even with different signature) in local scope will hide global ones

40.   when use template function, compiler will instantiate function according to arguments passed in.

  but for template class, we should pass the type name to it

template<class T> parm fcn(T * array) { typename T::type * P;} 

//use type name here to declare that it's a type , not an object multiply p;

41.  how to compare two object (a,b) with <?

if (a<b) else if (b<a) else

what is instantiation of template?

waht is instantiate?

template specialization
what is stack unwind, when do this, local varialbe will be freed, but for those created by operator new

signed vs unsigned

most asked interview questions for C/C++的更多相关文章

  1. WCF学习系列二---【WCF Interview Questions – Part 2 翻译系列】

    http://www.topwcftutorials.net/2012/09/wcf-faqs-part2.html WCF Interview Questions – Part 2 This WCF ...

  2. [译]Node.js Interview Questions and Answers (2017 Edition)

    原文 Node.js Interview Questions for 2017 什么是error-first callback? 如何避免无止境的callback? 什么是Promises? 用什么工 ...

  3. WCF学习系列三--【WCF Interview Questions – Part 3 翻译系列】

    http://www.topwcftutorials.net/2012/10/wcf-faqs-part3.html WCF Interview Questions – Part 3 This WCF ...

  4. WCF学习系列四--【WCF Interview Questions – Part 4 翻译系列】

    WCF Interview Questions – Part 4   This WCF service tutorial is part-4 in series of WCF Interview Qu ...

  5. [转]Design Pattern Interview Questions - Part 4

    Bridge Pattern, Composite Pattern, Decorator Pattern, Facade Pattern, COR Pattern, Proxy Pattern, te ...

  6. [转]Design Pattern Interview Questions - Part 2

    Interpeter , Iterator , Mediator , Memento and Observer design patterns. (I) what is Interpreter pat ...

  7. [转]Design Pattern Interview Questions - Part 3

    State, Stratergy, Visitor Adapter and fly weight design pattern from interview perspective. (I) Can ...

  8. [转]Design Pattern Interview Questions - Part 1

    Factory, Abstract factory, prototype pattern (B) What are design patterns? (A) Can you explain facto ...

  9. 101+ Manual and Automation Software Testing Interview Questions and Answers

    101+ Manual and Automation Software Testing Interview Questions and Answers http://www.softwaretesti ...

  10. 115 Java Interview Questions and Answers – The ULTIMATE List--reference

    In this tutorial we will discuss about different types of questions that can be used in a Java inter ...

随机推荐

  1. Hive基本命令解析

    1. Hive的分区作用 命令:创建分区 create table t_sz_part(id int, name string) partitioned by (country string) row ...

  2. 打印流PrintStream

    打印流PrintStream PrintStream extends OutputStream 1.打印流的特点 只负责数据的输出,不负责数据的读取 与其他的流不同,打印流永远不会抛出IOExcept ...

  3. adduser Ubuntu添加sudo用户

    第一种方法: 添加sudo用户 当你安装Ubuntu的时候,它会自动添加第一个用户到sudo组,允许这个用户通过键入其自身帐户密码来获得超级用户(root)身份.然而,系统不会再自动添加其他的用户到s ...

  4. 如何利用iconfont图标代替小图片

    1.首先 你要有一个阿里巴巴矢量图这个网站的账号:http://www.iconfont.cn/ 在这里注册哦~ 2.蓝后 可以在首页搜索你想要的图标,比如 我想放一个管理员的图标在页面上: 就要点击 ...

  5. pyaudio

    安装: 下载whl文件:https://github.com/intxcc/pyaudio_portaudio/releases 切换到whl文件目录,直接用pip安装      pip instal ...

  6. 三层结构与MVC

    实验室学弟突然又谈到了三层结构与MVC的话题,想想还是有必要整理一下这些个基本概念,且也纠正自己之前对二者的概念混淆,乃至将其错误思想指导于开发中. 关键词:三层结构(三层架构).MVC框架(思想). ...

  7. centos 查找命令的可用包/命令属于哪个软件包

    centos 查找命令的可用包 yum provides */commond 例如: yum provides */lsb_release

  8. Centos7安装美团SQL优化工具SQLAdvisor

    1 下载源码 git clone https://github.com/Meituan-Dianping/SQLAdvisor.git 2 安装依赖环境 yum install cmake libai ...

  9. JQuery基本过滤器

  10. git修改文件夹名字

    git mv -f oldfolder newfolder git add -u newfolder (-u选项会更新已经追踪的文件和文件夹) git commit -m "changed ...