C++ Programming - OOPS Concepts - Discussion

Discussion Forum : OOPS Concepts - General Questions (Q.No. 3)
3.
Which of the following statements is correct?
Base class pointer cannot point to derived class.
Derived class pointer cannot point to base class.
Pointer to derived class cannot be created.
Pointer to base class cannot be created.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
41 comments Page 3 of 5.

Aditya said:   4 years ago
The dynamic_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.

For example:
// dynamic_cast
#include <iostream>
#include <exception>
using namespace std;

class Base { virtual void dummy() {} };
class Derived: public Base { int a; };

int main () {
try {
Base * pba = new Derived;
Base * pbb = new Base;
Derived * pd;

pd = dynamic_cast<Derived*>(pba);
if (pd==0) cout << "Null pointer on first type-cast.\n";

pd = dynamic_cast<Derived*>(pbb);
if (pd==0) cout << "Null pointer on second type-cast.\n";

} catch (exception& e) {cout << "Exception: " << e.what();}
return 0;
}

Output: Null pointer on second type-cast.

Souradip said:   1 decade ago
The actual reason is - a derived class has all information about a base class and also some extra bit of information. Now a pointer to a derived class will require more space and that is not sufficient in base class. So the a pointer to a derived class cannot point to it. While on the other hand the reverse is true.

Srikanth said:   1 decade ago
The statement is correct because, derived class has no knowledge of its parent.

VINOD said:   1 decade ago
Already derived from that class only, so this can use that class directly, again why to point. So to avoid this efficiency problem language developers already created in that way.

Kavita said:   1 decade ago
I agree with srikant! there is no way a pointer should be able to point to an object of its parent class type because derived class not know anything about the new members that might have declared in parent class.

Yogesh said:   1 decade ago
I totally agree with Vinod. Derived class object can access members of base class[Depending upon type of inheritance], so need to do that, because C++ Developers have developed compiler in this way only.

Shivam said:   1 decade ago
When base class is made as virtual then only derived class pointer points to the base class otherwise derived class pointer can't point to the base class.

Neeraj Raghav said:   1 decade ago
Because base class allready has its pointer n with this pointer we can access the derived class so no need to declare a pointer in derived class.

Nilam patel said:   1 decade ago
Derived classes are derived from their base class so base class has total knowledge of it's derived class n can able to point them but derived class has no ability to point to its base class. Because it's a top down approach.

Hemanth said:   1 decade ago
There is no need of derived class to point to its base class or to it's parent class as it is inherited from it and it would be quite violating the property of inheritance.


Post your comments here:

Your comments will be displayed after verification.