For any class that has virtual function(s) [ and/or derived from a class that has virtual function(s)], compiler generates a 'virtual table' ('vtable' in short).
A vtable is an array of pointer to functions. Compiler also adds a hidden data member, say __vptr to the class. This '__vptr' points to the vtable of the actual class of the object.
For example: vptr->run() is compiler to call a function through pointer.
In vehicle class case, vehicle class has only one virtual member function called run. So compiler generates a vtable for vehicle class. This vtable has only one entry which points to vehicle::run. Assuming 'car' is derived from vehicle and overrides vehicle::run, car class vtable contains one entry. This entry points to car::run.
Every vehicle class object's '__vptr' (pointer to vtable) points to vehicle class vtable. This hidden pointer may be put at the end of the object. Every car class object's '__vptr' points to car class vtable. Whenever a virtual function is called like
v->run();
Compiler generates
((v->__vptr)[0])(v /* this pointer */); // call function pointed by first entry in the // vtable.
This is because first entry corresponds // to run member function.
If a class has more than one virtual function, say 'N' virtual functions then corresponding vtable will have more 'N' entries. Each time 'n'th virtual function is called as
(v->__vptr[n - 1])(v /*, other arguments if any */);Virtual Destructor
A virtual destructor is a destructor that is deployed virtually.i.e. when the delete keyword is used with a pointer to a base class,the derived most destructor is called.
Consider:
class B {public: ~B(); };class D : public B {public: ~D();};
class BVD {public: virtual ~BVD();};class DVD : public BVD {public: virtual ~DVD();};
B* b = new D;delete b; // calls ~B(), does not call ~D() !!! Crash.
BVD* bvd = new DVD;delete bvd; // calls ~DVD(); good.
Clearly, you want to have virtual destructors just so that the correctdestructor is called when an object is deleted. 
But what if your objects don't need any destructors?  What if thereare no cleanup activities that you need to write for your objects?  Inthat case, YOU STILL NEED VIRTUAL DESTRUCTORS!.
When the storage for an object is going to be destroyed, two pieces ofinformation are required.
The address and size of the block of storage that isgoing to be returned to the heap.  It is the destructor which suppliesthis information to the runtime system.
Operator Overloading
Operator overloading is the ability to tell the compiler how to perform a certain operation when its corresponding operator is used on one or more variables
Abstraction
Abstraction is of the process of hiding unwanted details from the user.
Inline functions
When the compiler inline-expands a function call, the function's code gets inserted into the caller's code stream (conceptually similar to what happens with a #define macro). This can, depending on a zillion other things, improve performance, because the optimizer can procedurally integrate the called code — optimize the called code into the caller.
If the compiler inline-expands the call to g(), all those memory operations could vanish. The registers wouldn't need to get written or read since there wouldn't be a function call, and the parameters wouldn't need to get written or read since the optimizer would know they're already in registers.
It can be implemented in two ways.
1) By defining in the declaration itself.
2) By including inline keyword while defining the member functions
1) By defining in the declaration itself.
2) By including inline keyword while defining the member functions
Copy constructors vs Assignment operator
A copy constructor is used to initialize a newly declared variable from an existing variable. A assignment operator is used to assign an existing variable from an another existing variable.The copy constructor and assignment operator do similar things. They both copy state from one object to another, leaving them with equivalent semantic state. In other words, both objects will behave the same way and return the same results when their methods are called.
A copy constructor doesn't need to delete previously allocated memory: since the object in question has just been created, it cannot already have its own allocated data. * and another interesting point regarding Copy constructor to note is, When a function takes an object as argument, instead of, e.g., a pointer or a reference, the copy constructor is called to pass a copy of an object as the argument.* The copy constructor is also implicitly called when a function returns an object:
Deep copy vs Shallow
A deep copy copies all fields, and makes copies of dynamically allocated memory pointed to by the fields. To make a deep copy, you must write a copy constructor and overload the assignment operator, otherwise the copy will point to the original, with disasterous consequences.
A shallow copy of an object copies all of the member field values. This works well if the fields are values, but may not be what you want for fields that point to dynamically allocated memory. The pointer will be copied. but the memory it points to will not be copied -- the field in both the original object and the copy will then point to the same dynamically allocated memory, which is not usually what you want. The default copy constructor and assignment operator make shallow copies.
The default copy constructor and assignment operator make shallow copies.To make a deep copy, you must write a copy constructor and overload the assignment operator, otherwise the copy will point to the original, with disasterous consequences
Name Mangling
Name mangling, (the more politically correct term is name-decoration, although it is rarely used) is a method used by a C++ compiler to generate unique names for identifiers in a program. The exact details of the algorithm are compiler-dependent, and they may vary from one version to another.
Name mangling ensures that entities with seemingly identical names still get unique identifications. The resultant mangled name contains all the necessary information that may be needed by the linker, such as linkage type, scope, calling convention, and so on. When a global function is overloaded, the generated mangled name for each overloaded version is unique. Name mangling is also applied to variables. Thus, a local variable and a global variable with the same user-given name still get distinct mangled names
Namespaces
Namespaces allow to group entities like classes, objects and functions under a name. This way the global scope can be divided in "sub-scopes", each one with its own name.The format of namespaces is:
namespace identifier{entities}Access specifiers
Public, protected and private are three access specifier in C++. Public data members and member functions are accessible outside the class. Protected data members and member functions are only available to derived classes. Private data members and member functions can’t be accessed outside the class. However there is an exception can be using friend classes.
friend functions and friend classes
Once a non-member function is declared as a friend, it can access the private data of the class similarly when a class is declared as a friend, the friend class can have access to the private data of the class which made this a friend.
This is a good way out given by C++ to avoid restrictions on private variables. But this should be used with caution though. If all the functions and classes are declared as friends, then the concept of encapsulation and data security will go for a toss.
Templates
Use function templates to write generic functions that can be used with arbitrary types. For example, one can write searching and sorting routines which can be used with any arbitrary type.
We also have the possibility to write class templates, so that a class can have members that use template parameters as types
Inheritance
There are some points to be remembered about C++ inheritance. The protected and public variables or members of the base class are all accessible in the derived class. But a private member variable not accessible by a derived class.
Some of the exceptions to be noted in C++ inheritance are as follows.The constructor and destructor of a base class are not inherited the assignment operator is not inherited the friend functions and friend classes of the base class are also not inherited.
if you derive with public access specifier,the derived class gets max access level..i.e., public will be public and protected will be protected and private will be private.if you derive with protected access specifier, the derived class gets limited access level..,i.e., public will be protected and protected will become protected and private will remain like private.if you derive with private access specifier, the derived class gets min access level..i.e., public will become as private, protected will become as private and private will remain like private.
The way that the access specifiers, inheritance types, and derived classes interact causes a lot of confusion. To try and clarify things as much as possible:
First, the base class sets it.s access specifiers. The base class can always access it.s own members. The access specifiers only affect whether outsiders and derived classes can access those members.
Second, derived classes have access to base class members based on the access specifiers of the immediate parent. The way a derived class accesses inherited members is not affected by the inheritance method used!
Finally, derived classes can change the access type of inherited members based on the inheritance method used. This does not affect the derived classes own members, which have their own access specifiers. It only affects whether outsiders and classes derived from the derived class can access those inherited members.
static variable,static functions
For some reason, static has different meanings in in different contexts. When specified on a function declaration, it makes the function local to the file.
When specified with a variable inside a function, it allows the vairable to retain its value between calls to the function. See static variables. It seems a little strange that the same keyword has such different meanings....
* Static member functions have external linkage. These functions do not have this pointers. As a result, the following restrictions apply to such functions:
They cannot access nonstatic class member data using the member-selection operators (. or –>). They cannot be declared as virtual. They cannot have the same name as a nonstatic function that has the same argument types. * When a data member is declared as static, only one copy of the data is maintained for all objects of the class.
Static data members are not part of objects of a given class type; they are separate objects. As a result, the declaration of a static data member is not considered a definition. The data member is declared in class scope, but definition is performed at file scope. These static members have external linkage
Virtual inheritance
With virtual inheritance there is only one copy of each object even if (because of multiple inheritance) the object appears more than once in the hierarchy. virtual inheritance is a kind of inheritance that solves some of the problems caused by multiple inheritance (particularly the "diamond problem") by clarifying ambiguity
Object slicing
When we assign Derived Object to Base object or when we instantiate Base object with Derived object, object slicing happens.It also happens when we pass Derived object to a function which takes Base object, which eventually calls Copy constructor.Object slicing happens because, Base copy function (assignment) and constructor does not know anything about Derived
*The C++ run-time system makes sure that when memory allocation fails, an error function is activated.
*The mutable keyword is used to modify datamembers of a object, which is defined as constant object.
Compilation Process
1. Driver - what we invoked as "cc". This is actually the "engine", that drives the whole set of tools the compiler is made of. We invoke it, and it begins to invoke the other tools one by one, passing the output of each tool as an input to the next tool.
2. C Pre-Processor - normally called "cpp". It takes a C source file, and handles all the pre-processor definitions (#include files, #define macros, conditional source code inclusion with #ifdef, etc.) You can invoke it separately on your program, usually with a command like:
cc -E single_source.c3. The C Compiler - normally called "cc1". This is the actual compiler, that translates the input file into assembly language. As you saw, we used the "-c" flag to invoke it, along with the C Pre-Processor, (and possibly the optimizer too, read on), and the assembler.
4. Optimizer - sometimes comes as a separate module and sometimes as the found inside the compiler module. This one handles the optimization on a representation of the code that is language-neutral. This way, you can use the same optimizer for compilers of different programming languages.
5. Assembler - sometimes called "as". This takes the assembly code generated by the compiler, and translates it into machine language code kept in object files. With gcc, you could tell the driver to generated only the assembly code, by a command like:
cc -S single_source.c4. Optimizer - sometimes comes as a separate module and sometimes as the found inside the compiler module. This one handles the optimization on a representation of the code that is language-neutral. This way, you can use the same optimizer for compilers of different programming languages.
5. Assembler - sometimes called "as". This takes the assembly code generated by the compiler, and translates it into machine language code kept in object files. With gcc, you could tell the driver to generated only the assembly code, by a command like:
6. Linker-Loader - This is the tool that takes all the object files (and C libraries), and links them together, to form one executable file, in a format the operating system supports. A Common format these days is known as "ELF". On SunOs systems, and other older systems, a format named "a.out" was used. This format defines the internal structure of the executable file - location of data segment, location of source code segment, location of debug information and so on.
Static vs Dynamic libraries
When you compile a program with static libraries statically linked libraries are linked into the final executable by the linker. This increases the size of the executable. Likewise when a library needs to be updated you'll need to compile the new library and then recompile the application to take advantage of the new library. Ok, so why do we have static libraries then? Well if you're booting your system into maintenance mode static libraries can be beneficial.
Dynamic libraries are libraries that have the library name embedded into the executable but the library itself is not compiled into the binary file. This makes upgrading libraries easier. You can upgrade the library and the application will still work barring any unforeseen changes to the library itself. If the name of the library changes you can just create a symbolic link to the old library name and your back in business.
Also, since the library is not compiled into the executable the executables tend to be smaller. If you have a lot of programs that utilize the same library you might be able to save considerable disk space. If you have a lot of programs that utilize the same library you only need to upgrade the library and then all the programs that use it go on as if nothing has changed. Since the libraries are shared the memory footprint goes down as well.
There is a third type of libraries that are used by programs. These are called dynamically loaded libraries. These libraries are built as normal shared or statically linked libraries. The difference is that they are not loaded at program startup, instead you would use the dlsym() and dlopen() application programming interface to activate the library. This is how you get web browser plugin's, modules (Apache), or just in time compilers to work. When you are done using the library, you would call dlclose() to remove the library from memory. Errors are handled via the dlerror() application programming interface.
executable format(a.out, ELF formats etc):-a.out is the venerable executable format that was common in Unix's early history and originally Linux's only executable format. To this day, the default name of the executable output file of the GNU compiler is a.out (regardless of what it's format is).
Some of the capabilities of ELF are dynamic linking, dynamic loading, imposing runtime control on a program, and an improved method for creating shared libraries[3]. The ELF representation of control data in an object file is platform independent, an additional improvement over previous binary formats.
The three main types of ELF files are executable, relocatable, and shared object files. These file types hold the code, data, and information about the program that the operating system and/or link editor need to perform the appropriate actions on these files. The three types of files are summarized as follows:
An executable file supplies information necessary for the operating system to create a process image suitable for executing the code and accessing the data contained within the file. A relocatable file describes how it should be linked with other object files to create an executable file or shared library. A shared object file contains information needed in both static and dynamic linking.
ELF file format contains the five section types. These five types are (1) the ELF header, (2) the program header table, (3) the section header table, (4) the ELF sections, and (5) the ELF segments.
Basic operations on Linked List
1.basic creation:Node tmp = new Node(newdat); // Step 1tmp.setNext( n.getNext() ); // Step
2(a)n.setNext( tmp ); // Step 2(b)
2.reverse:
struct node *p,*q,*r;r=null;p=head;while(p){ q=p->next; p->next=r; r=p; p=q;}head=p;
3.finding if it is a circular:while (pointer1) {pointer1 = pointer1->next;pointer2 = pointer2->next; if (pointer2) pointer2=pointer2->next;if (pointer1 == pointer2) {print (\"circular\n\");}}
How to find processor endiness?
The attribute of a system that indicates whether integers are represented with the most significant byte stored at the lowest address (big endian) or at the highest address (little endian).
Serious run-time performance penalties occur when using TCP/IP on a little endian processor. For that reason, it may be unwise to select a little endian processor for use in a device, such as a router or gateway, with an abundance of network functionality.
 
