Rohit Hajare
BAN USERdynamic_cast can only be used with pointers and references to classes (or with void*). Its purpose is to ensure that the result of the type conversion points to a valid complete object of the destination pointer type.
This naturally includes pointer upcast (converting from pointer-to-derived to pointer-to-base), in the same way as allowed as an implicit conversion.
But dynamic_cast can also downcast (convert from pointer-to-base to pointer-to-derived) polymorphic classes (those with virtual members) if -and only if- the pointed object is a valid complete object of the target type.
The dynamic_cast operator is intended to be the most heavily used RTTI component. It doesn't give us what type of object a pointer points to. Instead, it answers the question of whether we can safely assign the address of an object to a pointer of a particular type.
Unlike other casts, a dynamic_cast involves a run-time type check. If the object bound to the pointer is not an object of the target type, it fails and the value is 0. If it's a reference type when it fails, then an exception of type bad_cast is thrown. So, if we want dynamic_cast to throw an exception (bad_cast) instead of returning 0, cast to a reference instead of to a pointer. Note also that the dynamic_cast is the only cast that relies on run-time checking.
void Sorting(int Number[],int size)
{
int temp;
int count = size /2;
int k = 0;
int incr = 0;
while(k < count)
{
for(int i=incr;i<size;i++)
{
for(int j = i+1;j<size;j++)
{
if(Number[j] > Number[i])
{
temp = Number[j];
Number[j] = Number[i];
Number[i] = temp;
}
}
}
incr++;
for(int i = incr;i<size;i++)
{
for(int j = i+1;j<size;j++)
{
if(Number[j] < Number[i])
{
temp = Number[i];
Number[i] = Number[j];
Number[j] = temp;
}
}
}
incr++;
k++;
}
for(int k=0;k<size;k++)
{
std::cout<<Number[k];
}
}
The reason for this restriction is that the is-a relationship is not, in most of the cases, symmetric. A derived class could add new data members, and the class member functions that used these data members wouldn't apply to the base class. so if base class doesn't know anything about child class then how he behave like child class.
- Rohit Hajare March 28, 2016