TechieClues

C++ Programming - Interview Questions & Answers
1. What is C++?
A high-level programming language developed by Bjarne Stroustrup at Bell Labs. C++ adds object-oriented features to its predecessor, C. C++ is one of the most popular programming language for graphical applications, such as those that run in Windows and Macintosh environments.
2. Is it possible to have Virtual Constructor? If yes, how? If not, Why not possible?
There is nothing like Virtual Constructor. The Constructor can’t be virtual as the constructor is a code which is responsible for creating an instance of a class and it can’t be delegated to any other object by virtual keyword means
3. What about Virtual Destructor?
Yes there is a Virtual Destructor. A destructor can be virtual as it is possible as at runtime depending on the type of object caller is calling to, proper destructor will be called.
4. What is default constructor?
Constructor with no arguments or all the arguments has default values.
5. What is constructor or ctor?
Constructor creates an object and initializes it. It also creates vtable for virtual functions. It is different from other methods in a class.
6. What is copy constructor?
Constructor which initializes the it's object member variables ( by shallow copying) with another object of the same class. If you don't implement one in your class then compiler implements one for you.
For example:
(a) Boo Obj1(10); // calling Boo constructor
(b) Boo Obj2(Obj1); // calling boo copy constructor
(c) Boo Obj2 = Obj1;// calling boo copy constructor
7. 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.
8. When are copy constructors called?
Copy constructors are called in following cases:
(a) when a function returns an object of that class by value
(b) when the object of that class is passed by value as an argument to a function
(c) when you construct an object based on another object of the same class
(d) When compiler generates a temporary object
9. Can a constructor throws an exception? How to handle the error when the constructor fails?
The constructor never throws an error.
10. 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.
Related Links