Friday 5 April 2013

What is reference ??

What is reference ??

reference is a name that acts as an alias, or alternative name, for a previously defined variable or an object.
prepending variable with "&" symbol makes it as reference.
for example:
int a;
int &b = a;




Blog Author: Vijay Kumar

What is passing by reference?

What is passing by reference?

Method of passing arguments to a function which takes parameter of type reference.
for example:
void swap( int & x, int & y )
{
 int temp = x;
 x = y;
 y = temp;
}
int a=2, b=3;
swap( a, b );
Basically, inside the function there won't be any copy of the arguments "x" and "y" instead they refer to original variables a and b. so no extra memory needed to pass arguments and it is more efficient.




Blog Author: Vijay Kumar

What is virtual function?

What is virtual function?

When derived class overrides the base class method by redefining the same function, then if client wants to access redefined the method from derived class through a pointer from base class object, then you must define this function in base class as virtual function.
class parent
{
   void Show()
{
cout << "i'm parent" << endl;
}
};
class child: public parent
{
   void Show()
{
cout << "i'm child" << endl;
}
};
parent * parent_object_ptr = new child;
parent_object_ptr->show() // calls parent->show() i
now we goto virtual world...
class parent
{
   virtual void Show()
{
cout << "i'm parent" << endl;
}
};
class child: public parent
{
   void Show()
{
cout << "i'm child" << endl;
}
};
parent * parent_object_ptr = new child;
parent_object_ptr->show() // calls child->show()



Blog Author: Vijay Kumar

What is pure virtual function? or what is abstract class?

What is pure virtual function? or what is abstract class?

When you define only 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 this way..
class Boo
{
void foo() = 0;
}
Boo MyBoo; // compilation error




Blog Author: Vijay Kumar

What is the use of 'using' declaration?

What is the use of 'using' declaration?

A using declaration makes it possible to use a name from a namespace without the scope operator.




Blog Author: Vijay Kumar

What is a dangling pointer?

What is a dangling pointer?

A dangling pointer arises when you use the address of an object after its lifetime is over. This may occur in situations like returning addresses of the automatic variables from a function or using the address of the memory block after it is freed.




Blog Author: Vijay Kumar

What do you mean by Stack unwinding?

What do you mean by Stack unwinding?

It is a process during exception handling when the destructor is called for all local objects in the stack between the place where the exception was thrown and where it is caught.




Blog Author: Vijay Kumar

Name the operators that cannot be overloaded??

Name the operators that cannot be overloaded??

sizeof, ., .*, .->, ::, ?:




Blog Author: Vijay Kumar

What is a container class? What are the types of container classes?

What is a container class? What are the types of container classes?

A container class is a class that is used to hold objects in memory or external storage. A container class acts as a generic holder. A container class has a predefined behavior and a well-known interface. A container class is a supporting class whose purpose is to hide the topology used for maintaining the list of objects in memory. When a container class contains a group of mixed objects, the container is called a heterogeneous container; when the container is holding a group of objects that are all the same, the container is called a homogeneous container.




Blog Author: Vijay Kumar

What is inline function??


What is inline function??
The __inline keyword tells the compiler to substitute the code within the function definition for every instance of a function call. However, substitution occurs only at the compiler's discretion. For example, the compiler does not inline a function if its address is taken or if it is too large to inline.


What is the difference between const char *p, char * const p and const char * const p?


What is the difference between const char *p, char * const p and const char * const p?

The basic rule of thumb with const (and volatile) keyword is that it applies to whatever is immediately to its left. If there is nothing to its left, it applies to whatever is immediately to its right. By this logic, "const char *" is a (non-const) pointer to a const char, and "char const *" means the same thing.

1. const char *p : means p is pointer pointing to a constant char i.e. you can not change the content of the location where it is pointing but u can change the pointer itself to point to some other char. 

2. char const *p, and char * const p : both are same & in this case p is a constant pointer poiting to some char location. you can change the contents of that location but u can't change the pointer to point to some other location.

const char *p - This is a pointer to a constant character. You cannot change the value pointed by p, but you can change the pointer p itself.

*p = 'S' is illegal.
p = "Test" is legal.

Note - char const *p is the same.


const * char p - This is a constant pointer to non-constant character. You cannot change the pointer p, but can change the value pointed by p.

*p = 'A' is legal.
p = "Hello" is illegal.


const char * const p - This is a constant pointer to constant character. You cannot change the value pointed by p nor the pointer p.

*p = 'A' is illegal.
p = "Hello" is also illegal. 

What is overloading??

What is overloading??

With the C++ language, you can overload functions and operators. Overloading is the practice of supplying more than one definition for a given function name in the same scope.
- Any two functions in a set of overloaded functions must have different argument lists.
- Overloading functions with argument lists of the same types, based on return type alone, is an error.



Blog Author: Vijay Kumar

What is Overriding?

What is Overriding?

To override a method, a subclass of the class that originally declared the method must declare a method with the same name, return type (or a subclass of that return type), and same parameter list.
The definition of the method overriding is:
· Must have same method name. 
· Must have same data type. 
· Must have same argument list. 
Overriding a method means that replacing a method functionality in child class. To imply overriding functionality we need parent and child classes. In the child class you define the same method signature as one defined in the parent class.



Blog Author: Vijay Kumar

How virtual functions are implemented C++?

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.  When a derived class is constructed, its base class is constructed first which creates the vtable.  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: because the vtable entries for the object may not have been set up by the derived class constructor yet, so you might end up calling base class implementations of those virtual functions.



Blog Author: Vijay Kumar

What is name mangling in C++??

What is name mangling in C++??

The process of encoding the parameter types with the function/method name into a unique name is called name mangling. The inverse process is called demangling.
For example Foo::bar(int, long) const is mangled as `bar__C3Fooil'.
For a constructor, the method name is left out. That is Foo::Foo(int, long) const is mangled as `__C3Fooil'.



Blog Author: Vijay Kumar

What is the difference between const char *myPointer and char *const myPointer?

What is the difference between const char *myPointer and char *const myPointer?

Const char *myPointer is a non constant pointer to constant data; while char *const myPointer is a constant pointer to non constant data.



Blog Author: Vijay Kumar

How can I handle a constructor that fails?

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.



Blog Author: Vijay Kumar

How can I handle a destructor that fails?

How can I handle a destructor that fails?

Write a message to a log-file. 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. For example, if someone says throw Foo(), the stack will be unwound so all the stack frames between the throw Foo() and the } catch (Foo e) { will get popped. This is called stack unwinding.
During stack unwinding, all the local objects in all those stack frames are destructed. If one of those destructors throws an exception (say it throws a Bar object), the C++ runtime system is in a no-win situation: should it ignore the Bar and end up in the } catch (Foo e) { where it was originally headed? Should it ignore the Foo and look for a } catch (Bar e) { handler? There is no good answer -- either choice loses information.
So the C++ language guarantees that it will call terminate() at this point, and terminate() kills the process. Bang you're dead.



Blog Author: Vijay Kumar

What are the differences between a C++ struct and C++ class?

What are the differences between a C++ struct and C++ class?

The default member and base class access specifiers are different.
The C++ struct has all the features of the class. The only differences are that a struct defaults to public member access and public base class inheritance, and a class defaults to the private access specifier and private base class inheritance.



Blog Author: Vijay Kumar

How do you access the static member of a class?

How do you access the static member of a class?

<ClassName>::<StaticMemberName>


Blog Author: Vijay Kumar

What is multiple inheritance(virtual inheritance)? What are its advantages and disadvantages?



What is multiple inheritance(virtual inheritance)? What are its advantages and disadvantages?
Multiple Inheritance is the process whereby a child can be derived from more than one parent class. The advantage of multiple inheritance is that it allows a class to inherit the functionality of more than one base class thus allowing for modeling of complex relationships. The disadvantage of multiple inheritance is that it can lead to a lot of confusion(ambiguity) when two base classes implement a method with the same name.


Blog Author: Vijay Kumar

What are the access privileges in C++? What is the default access level?



What are the access privileges in C++? What is the default access level?
The access privileges in C++ are private, public and protected. The default access level assigned to members of a class is private. Private members of a class are accessible only within the class and by friends of the class. Protected members are accessible by the class itself and it's sub-classes. Public members of a class can be accessed by anyone.


Blog Author: Vijay Kumar

What is a nested class? Why can it be useful?



What is a nested class? Why can it be useful?
A nested class is a class enclosed within the scope of another class. For example:
  //  Example 1: Nested class
  //
  class OuterClass
  {
    class NestedClass
    {
      // ...
    };
    // ...
  };
Nested classes are useful for organizing code and controlling access and dependencies. Nested classes obey access rules just like other parts of a class do; so, in Example 1, if NestedClass is public then any code can name it as OuterClass::NestedClass. Often nested classes contain private implementation details, and are therefore made private; in Example 1, if NestedClass is private, then only OuterClass's members and friends can use NestedClass.
When you instantiate as outer class, it won't instantiate inside class.


Blog Author: Vijay Kumar

What is a local class? Why can it be useful?



What is a local class? Why can it be useful?
local class is a class defined within the scope of a function -- any function, whether a member function or a free function. For example:
  //  Example 2: Local class
  //
  int f()
  {
    class LocalClass
    {
      // ...
    };
    // ...
  };
Like nested classes, local classes can be a useful tool for managing code dependencies.


Blog Author: Vijay Kumar

Can a copy constructor accept an object of the same class as parameter, instead of reference of the object?



Can a copy constructor accept an object of the same class as parameter, instead of reference of the object?
No. It is specified in the definition of the copy constructor itself. It should generate an error if a programmer specifies a copy constructor with a first argument that is an object and not a reference.


Blog Author: Vijay Kumar

What is encapsulation??



What is encapsulation??
Containing and hiding information about an object, such as internal data structures and code. Encapsulation isolates the internal complexity of an object's operation from the rest of the application. For example, a client component asking for net revenue from a business object need not know the data's origin.