C and C++ concepts

  • What is the difference between a pointer to a constant and a constant pointer? Show code fragments for each case.
  • What is operator overloading? List two operators that canot be overloaded in C++.
  • What are two cases where a class member necessarily has to be initialized in the constructor initializer and not within the body of the constructor?
  • Discuss the behavior of the bar() method in the following class:
    class foo {
    int bar() const;
    }
  • What are storage classes?
    A storage class defines the lifetime of a variable. It could be either local (automatic) or global (static). Items declared with the auto and register specifiers have a local scope. Static and extern specifiers define a global scope. The typedef storage class creates synonyms for a new type.

  • Design a singleton class in C++
  • What does the static keyword do?
    A static variable is allocated when the program begins and deallocated when the program ends and is initialized to 0 by default. Scope is defined by where it is defined - either within a block, function or a file. A static member in a C++ class is shared among all instances of that class. These static members must necessarily be initialized at file level (even if that member was declared private). A static method in a C++ class is one that accesses only static members of that class.
  • Write code between these two statements such that the printf() call works.
    int ***p;
    printf("%d", **p);
  • What is the difference between Dispose() and Finalize() in C#? How do you ensure that Dispose() doesn't interfere with Finalize()?
  • What is the difference between malloc and new?
  • Write a macro that computes the offset of a field in a structure, without instantiating an object:

    OFFSET_OF(StructName,FieldName)

    For example, given the following struct:

    struct _structFoo
    {
    int iMember;
    float fAnotherMember;
    }

    OFFSET_OF(_structFoo,fAnotherMember)

    should result in 4, without instantiating an object of type _structFoo