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