C++ FAQs


C++ FAQS Collected From Various Sites 


Question 1: What is C++?

Answer:

                  C++ is a programming language. It  means "C with Classes", reflecting its nature as an evolution of the C language.

Question 2: Is it possible to have Virtual Constructor? If yes, how? If not, Why not possible ?

Answer: Virtual Constructor doesn't exist.

Constructor can’t be virtual as the constructor is a code which is responsible for creating a instance of a class and it can’t be delegated to any other object by virtual keyword means.

 

Question 3: Can a constructor throw a exception? How to handle the error when the constructor fails?

Answer: The constructor never throws a error.

Question 4: What are the differences between a C++ struct and C++ class?

Answer: The default member and base-class access specifies are different.

This is one of the commonly misunderstood aspects of C++. Believe it or not, many programmers think that a C++ struct is just like a C struct, while a C++ class has inheritance, access specifies, member functions, overloaded operators, and so on. Actually, the C++ struct has all the features of the class. The only differences are that a struct defaults to public member access and public base-class inheritance, and a class defaults to the private access specified and private base-class inheritance.

Question 5: How do you know that your class needs a virtual destructor?

Answer: If your class has at least one virtual function, you should make a destructor for this class virtual. This will allow you to delete a dynamic object through a pointer to a base class object. If the destructor is non-virtual, then wrong destructor will be invoked during deletion of the dynamic object.



Question 6: Can inline functions have a recursion?

Answer: No.

Syntax wise It is allowed. But then the function is no longer Inline. As the compiler will never know how deep the recursion is at compilation time.

Question 7: What is your reaction to this line of code?

delete this;

Answer: It is not a good programming Practice.

A good programmer will insist that you should absolutely never use the statement if the class is to be used by other programmers and instantiated as static, extern, or automatic objects. That much should be obvious.

The code has two built-in pitfalls. First, if it executes in a member function for an extern, static, or automatic object, the program will probably crash as soon as the delete statement executes. There is no portable way for an object to tell that it was instantiated on the heap, so the class cannot assert that its object is properly instantiated. Second, when an object commits suicide this way, the using program might not know about its demise. As far as the instantiating program is concerned, the object remains in scope and continues to exist even though the object did itself in. Subsequent de-referencing of the pointer can and usually does lead to disaster. I think that the language rules should disallow the idiom, but that's another matter.

Question 8: What is the difference between new/delete and malloc/free?
Answer
: Malloc/free do not know about constructors and destructors. New and delete create and destroy objects, while malloc and free allocate and deallocate memory.

Queation 9: What is virtual constructors/destructors?  

Ask any programmer, he'll immediately reply saying "A destructor is a member function of a class, which gets called when the object goes out of scope". This means all clean ups and final steps of class destruction are to be done in destructor. A virtual function is something which helps a derived class in overriding the implementation of a functionality of a base class.

The order of execution of destructor in an inherited class during a clean up is like this.

1. Derived class destructor

2. Base class destructor

A difference between a destructor (of course also the constructor) and other member functions is that, if a regular member function has a body at the derived class, only the version at Derived class gets executed. Whereas in case of destructors, both derived as well as base class versions get executed.

Now turning our attention to why a destructor has to be virtual, the reason is that we, programmers are very smart. We'll do days and nights of work to inherit and extend the functionality of an existing class which is being used, and say that we don't want to change the implementation/interface just for the sake of a new entrant. Let me explain this with an example.

--------------------------------------------------------------------------------

#include <iostream.h>

class Base

{

public:

Base(){ cout<<"Constructor: Base"<<endl;}

~Base(){ cout<<"Destructor : Base"<<endl;}

};

class Derived: public Base

{

//Doing a lot of jobs by extending the functionality

public:

Derived(){ cout<<"Constructor: Derived"<<endl;}

~Derived(){ cout<<"Destructor : Derived"<<endl;}

> };

void main()

{

Base *Var = new Derived();

delete Var;

}

 

Try executing this code, you'll see the difference. To our observation, the constructors are getting called in the proper order. But to the dread of a programmer of a large project, the destructor of the derived class was not called at all.

 

This is where the virtual mechanism comes into our rescue. By making the Base class Destructor virtual, both the destructors will be called in order. The following is the corrected sample.

 

#include <iostream.h>

class Base

{

public:

Base(){ cout<<"Constructor: Base"<<endl;}

virtual ~Base(){ cout<<"Destructor : Base"<<endl;}

};

class Derived: public Base

{

//Doing a lot of jobs by extending the functionality

public:

Derived(){ cout<<"Constructor: Derived"<<endl;}

~Derived(){ cout<<"Destructor : Derived"<<endl;}

};

void main()

{

Base *Var = new Derived();

delete Var;

}

Note:

There is one more point to be noted regarding virtual destructor. We can't declare pure virtual destructor. Even if a virtual destructor is declared as pure, it will have to implement an empty body (at least) for the destructor.

Queation 10 : Why can't we overload the sizeof, :?, :: ., .* operators in c++?

Most operators can be overloaded by a programmer. The exceptions are

 

. (dot) :: ?: sizeof

 

There is no fundamental reason to disallow overloading of ?:. I just didn't see the need to introduce the special case of overloading a ternary operator. Note that a function overloading expr1?expr2:expr3 would not be able to guarantee that only one of expr2 and expr3 was executed.

 

Sizeof cannot be overloaded because built-in operations, such as incrementing a pointer into an array implicitly depends on it. Consider:

 

X a[10];

X* p = &a[3];

X* q = &a[3];

p++; // p points to a[4]

// thus the integer value of p must be

// sizeof(X) larger than the integer value of q

 

Thus, sizeof(X) could not be given a new and different meaning by the programmer without violating basic language rules.

 

In N::m neither N nor m are expressions with values; N and m are names known to the compiler and :: performs a (compile time) scope resolution rather than an expression evaluation. One could imagine allowing overloading of x::y where x is an object rather than a namespace or a class, but that would - contrary to first appearances - involve introducing new syntax (to allow expr::expr). It is not obvious what benefits such a complication would bring.

 

Operator . (dot) could in principle be overloaded using the same technique as used for ->. However, doing so can lead to questions about whether an operation is meant for the object overloading . or an object referred to by . For example:

 

class Y {

public:

void f();

// ...

};

 

class X { // assume that you can overload .

Y* p;

Y& operator.() { return *p; }

void f();

// ...

};

 

void g(X& x)

{

x.f(); // X::f or Y::f or error?

}

 

This problem can be solved in several ways. At the time of standardization, it was not obvious which way would be best. For more details, see D&E.

Queation 11: Difference between stack memory and heap memory?   

 Answers:

 

Stack = region of memory for temporaries

Stack pointer pushed on function entry

Popped on function exit

Stack data: new every time

stack is used for local variables

Heap = distinct region of memory for persistent objects

Allocate persistent data on heap with new keyword

Dynamically allocated memory (heap)exists until it is released either explicitly by the programmer

C/C++ ? explicitly managed

 


Make a Free Website with Yola.