© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu

C and C++ Interview Questions

17. Differentiate between declaration and definition in C++

Ans:

A declaration introduces a name into the program; a definition provides a unique description of an entity (e.g. type, instance, and function). Declarations can be repeated in a given scope, it introduces a name in a given scope. There must be exactly one definition of every object, function or class used in a C++ program.
A declaration is a definition unless:
Ø  it declares a function without specifying its body,
Ø  it contains an extern specifier and no initializer or function body,
Ø  it is the declaration of a static class data member without a class definition,
Ø  it is a class name definition,
Ø  it is a typedef declaration.
  A definition is a declaration unless:
Ø  it defines a static class data member,
Ø  it defines a non-inline member function.

Ex 1: How can a '::' operator be used as unary operator?

Ans:

The scope operator can be used to refer to members of the global namespace. Because the global namespace doesn’t have a name, the notation :: member-name refers to a member of the global namespace. This can be useful for referring to members of global namespace whose names have been hidden by names declared in nested local scope. Unless we specify to the compiler in which namespace to search for a declaration, the compiler simple searches the current scope, and any scopes in which the current scope is nested, to find the declaration for the name.

Ex 2: What is placement new?

Ans:

When you want to call a constructor directly, you use the placement new. Sometimes you have some raw memory that's already been allocated, and you need to construct an object in the memory you have. Operator new's special version placement new allows you to do it.
           class Widget
          {
               public :
                     Widget(int widgetsize);
                      ...
                      Widget* Construct_widget_int_buffer(void *buffer,int widgetsize)
                       {
                              return new(buffer) Widget(widgetsize);
                       }
          };
            This function returns a pointer to a Widget object that's constructed within the buffer passed to the function. Such a function might be useful for applications using shared memory or memory-mapped I/O, because objects in such applications must be placed at specific addresses or in memory allocated by special routines.

SLogix Student Projects
bottom