Mock tests, Interview questions, Tutorials and Tech news
 
 
Home > Programming / tutorials > C++ cheat sheet – Part 1

C++ cheat sheet – Part 1

November 22nd, 2009 Vinay Leave a comment Go to comments

Here are some notes I prepared in C++ while preparing for the C++ interviews. Hope you find them helpful

1. There are 5 implicit methods defined for a class by the compiler:

  • Default copy constructor
  • Default constructor
  • Assignment Operator
  • Default destructor
  • Address Operator

2. A constructor that takes just 1 argument is called CONVERSION constructor.

3. New and Delete call constructor and destructor respectively besides allocation and deallocating the memory for the object. However, malloc and free just deal with memory allocation and deallocation respectively. New and Delete can be overloaded whereas malloc and free can’t.

4. “operator new” works just like malloc, which is different from “new”.

5. There are 4 storage classes in C++ (auto, register, extern and static).

6. There are 3 storage qualifiers in C++ (const, volatile and mutable).

7. If a variable/object (structure or a class) is initialized with const keyword, the compiler does not allow changes to values of member variables of that variable/object anytime later in the program other than during initialization. However, those member variables of the object/variable can be modified if they are declared as MUTABLE. Moreover, as a rule, member functions that are declared as “const”, may not change value of ANY of the member variables. However, it can change the value of those members that are declared as “mutable”. Note that these member variables may NOT be declared as consts. Declaring them as mutable just allows the const member functions to be able to modify these variables. Another thing to remember is – Const versions of the functions are called for objects that are declared as const. Meaning, if there are two objects of the same type, one const and the other one not, we’d need two functions for the same operation (whatever that may be), one with const declaration and other one without.

8. With the help of “using <namespace>” declarator, it is possible to access members of the namespace without using the scope resolution operator.

9. An iterator class helps in traversing through objects in a container. There are 5 categories of iterators:

  • input iterators
  • output iterators
  • forward iterators
  • bidirectional iterators
  • random access

10. Following operators cannot be overloaded:

  • sizeof
  • .
  • *
  • ::
  • ?:

11. Virtual functions are implemented using a table of function pointers called vtable. Each virtual function in a class has an entry in the table. The constructor of the class creates/generates this table. Never call a virtual function from a class constructor.

12. A reference must always refer to an object and therefoer must always be initialized, whereas pointers do not have any such restriction. A pointer can be reassigned to point to different objects, whereas a reference always points to the object with which it was initialized.

13. To handle a constructor that fails, use exceptions.

14. C++ rule is that you must never throw an exception from a destructor that is being called during stack unwinding process of another exception. Instead write a message to a log file to handle a destructor that fails.

15. Virtual Destructors – using these you can destroy objects without knowing their type – the correct destructor for the object is called using virtual functions. Destructors can also be declared as pure virtual functions for abstract class.

16. We must use Initialization List instead of assignment operator in constructors while initializing non-static const members and reference data members. Copy constructors can be made private.

17. Functions cannot be overloaded on the basis of whether the parameter is a value or a reference. The caller does not know the difference.

18. Access privileges in C++ are public, protected and private.

19. A class defined inside a function body is called Local Class.

20. A copy constructor always accepts only the reference of the object of the same and not the object itself.

I will cover some more points in next post. Please give your feedback.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Mixx
  • Google Bookmarks
  • IndianPad
  • Reddit
Categories: Programming / tutorials Tags:
Get Adobe Flash playerPlugin by wpburn.com wordpress themes