1.

Write a program in C++ to found out whether a word is palindrome or not?

Answer»

We cant make a class constructor virtual in C++ to create polymorphic objects. C++ being static typed (the purpose of RTTI is different) language, it is meaningless to the C++ compiler to create an object polymorphically. The compiler must be aware of the class type to create the object. In other words, what type of object to be created is a compile time decision from C++ compiler perspective. If we make constructor virtual, compiler flags an error. In fact except INLINE, no other keyword is allowed in the declaration of constructor.

Deleting a derived class object using a pointer to a base class that has a non-virtual destructor results in undefined behavior. To correct this situation, the base class should be defined with a virtual destructor. For example, following program results in undefined behavior.

// CPP program WITHOUT virtual destructor  // causing undefined behavior #include<iostream> using namespace std; class base {   PUBLIC:     base()          { cout<<"Constructing base \n"; }     ~base()     { cout<<"Destructing base \n"; }      }; class derived: public base {   public:     derived()          { cout<<"Constructing derived \n"; }     ~derived()     { cout<<"Destructing derived \n"; } }; int MAIN(VOID) {   derived *d = new derived();     base *b = d;   delete b;   getchar();   return 0; }


Discussion

No Comment Found