Saturday, 13 May 2017

CPP

What is assignment operator?  
Default assignment operator handles assigning one object to another of the same class. Member to member copy (shallow copy).

What is "this" pointer? 
The this pointer is a pointer accessible only within the member functions of a class,  struct, or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer. When a nonstatic member function is called for an object, the address of the object is passed as a hidden argument to the function


 How virtual functions are implemented C++? 
Virtual functions are implemented using a table of function pointers, called the vtable. There is one entry in the table per virtual function in the class. This table is created by the constructor of the class.
 If the derived class overrides any of the base classes virtual functions, those entries in the vtable are overwritten by the derived class constructor. This is why you should never call virtual functions from a constructor:

Should my constructors use "initialization lists" or "assignment"? 
Initialization lists. In fact, constructors should initialize as a rule all member objects in the initialization list.
Consider the following constructor that initializes member object x_ using an initialization list: Fred::Fred() : x_(whatever) { }. The most common benefit of doing this is improved performance.

How can I handle a constructor that fails?
throw an exception. Constructors don't have a return type, so it's not possible to use return codes. The best way to signal constructor failure is therefore to throw an exception.
How can I handle a destructor that fails?
Write a message to a log-_le. But do not throw an exception. The C++ rule is that you must never throw an exception from a destructor that is being called during the "stack unwinding" process of another exception


What is the difference between a copy constructor and an overloaded assignment operator? 
 A copy constructor constructs a new object by using the content of the argument object. An       overloaded assignment operator assigns the contents of an existing object to another existing       object of the same class.

What is pure virtual function? or what is abstract class?
function prototype in a base class without implementation and do the complete implementation in derived class. This base class is called abstract class and client won't able to instantiate an object using this base class. You can make a pure virtual function or abstract class.


How does free know the size of memory to be deleted.? int *i = (int *)malloc(12); followed by free(i); how did free function call know how much of memory to delete? 

It depends on the implementation, but there is usually a malloc header added to all the memory allocated through malloc. on Linux its 4 bytes of memory preceding the memory returned to you, whihc contains the number of bytes allocated + 4(itself). 
so when you say, 
int *i = (int *)malloc(12); 
it allocates 16 bytes.

as you can see above total of 16 bytes are allocated, first 4 bytes stores the number of bytes allocated.

What is the difference between a pointer and a reference? 
 A reference must always refer to some object and, therefore, must always be initialized; pointers do not have such restrictions. A pointer can be reassigned to point to different objects while a reference always refers to an object with which it was initialized


What are the debugging methods you use when came across a problem? 
Debugging with tools like : (a) GDB, DBG, Forte, Visual Studio. (b) Analyzing the Core dump. (c) Using tusc to trace the last system call before crash. (d) Putting Debug statements in the program source code


 How the compiler arranges the various sections in the executable image?  
The executable had following sections: (a) Data Section (uninitialized data variable section, initialized data variable section ) (b) Code Section (c) Remember that all static variables are allocated in the initialized variable section.

Why do C++ compilers need name mangling?
Name mangling is the rule according to which C++ changes function's name into function       signature before passing that function to a linker. This is how the linker differentiates between       different functions with the same name.


What are C++ storage classes?
auto: the default. Variables are automatically created and initialized when they are defined and            are destroyed at the end of the block containing their definition. They are not visible   outside that block.
 register: a type of auto variable. a suggestion to the compiler to use a CPU register for                 performance.
 static: a variable that is known only in the function that contains its definition but is never             destroyed and retains its value between calls to that function. It exists from the time the             program begins execution.
 extern: a static variable whose definition and placement is determined when all object and library     modules are combined (linked) to form the executable code file. It can be visible outside   the file where it is defined.

 What are storage qualifiers in C++ ?

Const: Indicates that memory once initialized, should not be altered by a program.
Volatile: Indicates that the value in the memory location can be altered even though nothing in the program code modifies the contents. for example if you have a pointer to hardware location that contains the time, where hardware changes the value of this pointer variable and not the program. The intent of this keyword to improve the optimization ability of the compiler.


No comments:

Post a Comment