Saturday, March 13, 2010

Some concepts in C and C++

Those questions are found when I prepared for my job interview question.They might be simple questions but I've never think of them.Hope that I can understand better, and also for anyone are learning C and C++.

Why the constructor can't be virtual?
A: Constructors cannot be virtual. Declaring a constructor as a virtual function is a syntax error. The reason is a constructor can not be virtual because at the time when the constructor is invoked the virtual table (vtable) would not be available in the memory.
Virtual allows us to call a function knowing only the interfaces and not the exact type of the object. To create an object you need complete information. In particular, you need to know the what you want to create exactly. Hence call to a constructor can't be virtual.

Difference between delete and destructor in c++?
Delete can deallocate the memory of those objects which are created by using the new operator whereas the destructor can delete any object once the life of the object ends.(this is normally when a program ends)

Differences between Structure and Class.
1.Structure cannot be inherited.
2.Structure is a value data type whereas Class is a reference type.
3.All declarations inside a structure are by default public whereas by default all the members inside the class are private.
4.Structure instances cannot be used as operand to arithmetic operators like +-*/ . But object (instances of classes) can be (using operator overloading).
5.A structure can't declare protected members,a class can.
6.We can't inherit a structue from another structure, which we can do in class.

Difference between "overloading" and "overriding"?
Overloading a method (or function) in C++ is the ability for functions of the same name to be defined as long as these methods have different signatures (different set of parameters). Method overriding is the ability of the inherited class rewriting the virtual method of the base class.

There are many valuable comments on it.

No comments:

Post a Comment