A constructor cannot be virtual because at the time when the constructor is invoked the virtual table would not be available in the memory. Hence we cannot have a virtual constructor.
A virtual destructor is one that is declared as virtual in the base class and is used to ensure that destructors are called in the proper order. It is to be remembered that destructors are called in the reverse order of inheritance. If a base class pointer points to a derived class object and we some time later use the delete operator to delete the object, then the derived class destructor is not calledThursday, February 4, 2010
UpCasting and DownCasting in C++
Upcasting - UpCasting is defined as casting a pointer or reference of a derived class type to a type of a pointer or reference to a base class. In case of virtual function implementation we can cast a derived class pointer or reference to base class
Downcasting is defined as casting a pointer or reference of a base class type to a type of a pointer or reference to a derived class.
Mostly used to restore the object to its original type.
Downcasting can lead to trouble due to contravariance, e.g.:
struct Base {
int i_;
virtual int foo (void) { return i_; }
};
struct Derived : public Base {
int j_;
virtual int foo (void) { return j_; }
};
void foo (void) {
Base b;
Derived d;
Base *bp = &d; // "OK", a Derived is a Base
Derived *dp = &b;// Error, a Base is not necessarily a Derived
}
Problem: what happens if dp->j_ is referenced or set?
Since a Derived object always has a Base part certain operations
are ok:
bp = &d;
bp->i_ = 10;
bp->foo (); // calls Derived::foo ();
Since base objects don’t have subclass data some operations aren’t
ok
– e.g., accesses information beyond end of b:
dp = (Derived *) &b;
dp->j_ = 20; // big trouble!
C++ permits contravariance if the programmer explicitly casts, e.g.,
dp = (Derived *) &b; // unchecked cast
RTTI can be used to check the type of the object
if (Derived *dp = dynamic_cast(&b))
/* use dp */;
else
/* error! */
For a dynamic cast to succeed, the “actual type” of b would have to
either be a Derived object or some subclass of Derived
dynamic_cast throw bad_cast exception in case of failure
if the types do not match the operation fails at run-time
Downcasting is defined as casting a pointer or reference of a base class type to a type of a pointer or reference to a derived class.
Mostly used to restore the object to its original type.
Downcasting can lead to trouble due to contravariance, e.g.:
struct Base {
int i_;
virtual int foo (void) { return i_; }
};
struct Derived : public Base {
int j_;
virtual int foo (void) { return j_; }
};
void foo (void) {
Base b;
Derived d;
Base *bp = &d; // "OK", a Derived is a Base
Derived *dp = &b;// Error, a Base is not necessarily a Derived
}
Problem: what happens if dp->j_ is referenced or set?
Since a Derived object always has a Base part certain operations
are ok:
bp = &d;
bp->i_ = 10;
bp->foo (); // calls Derived::foo ();
Since base objects don’t have subclass data some operations aren’t
ok
– e.g., accesses information beyond end of b:
dp = (Derived *) &b;
dp->j_ = 20; // big trouble!
C++ permits contravariance if the programmer explicitly casts, e.g.,
dp = (Derived *) &b; // unchecked cast
RTTI can be used to check the type of the object
if (Derived *dp = dynamic_cast
/* use dp */;
else
/* error! */
For a dynamic cast to succeed, the “actual type” of b would have to
either be a Derived object or some subclass of Derived
dynamic_cast throw bad_cast exception in case of failure
if the types do not match the operation fails at run-time
What is the difference between overloading and overriding in C++
This is one of the most common question asked in C++ interview.
Overloading : Function overloading ( No change in input parameter or argument)
Overriding : Virtual Function ( When derived class override the base class implementation)
Overloading : Function overloading ( No change in input parameter or argument)
Overriding : Virtual Function ( When derived class override the base class implementation)
C++ polymorphism Questions
What is polymorphism?
Polymorphism is the ability to provide single interface to multiple implementation it may be for an or function in different ways. Polymorphism gives different meanings or functions to the operators or functions. A single function usage or an operator functioning in many ways can be called polymorphism. Polymorphism refers to codes, operations or objects that behave differently in different contexts.
There are two types of polymorphism
1. Compile Time Polymorphism ( Static Polymorphism )
2. Run Time Polymorphism (Dynamic Polymorphism )
1. Compile time polymorphism
Virtual function is the example of Run Time polymorphism . At the run time base class pointer decide whether to call the base class function or the derived class function.
Polymorphism is the ability to provide single interface to multiple implementation it may be for an or function in different ways. Polymorphism gives different meanings or functions to the operators or functions. A single function usage or an operator functioning in many ways can be called polymorphism. Polymorphism refers to codes, operations or objects that behave differently in different contexts.
There are two types of polymorphism
1. Compile Time Polymorphism ( Static Polymorphism )
2. Run Time Polymorphism (Dynamic Polymorphism )
1. Compile time polymorphism
Compile time polymorphism is a term that refers to C++ template programming. For example, at compile time you determine the actual type of a std::vector by what it contains:
std::vector vi;
std::vector std::string> vs;
or function Overloading is the example of compile time polymorphism
2. Run Time Polymorphism (Dynamic Polymorphism )Virtual function is the example of Run Time polymorphism . At the run time base class pointer decide whether to call the base class function or the derived class function.
Subscribe to:
Posts (Atom)