C++ Programming - OOPS Concepts - Discussion
Discussion Forum : OOPS Concepts - General Questions (Q.No. 3)
3.
Which of the following statements is correct?
Discussion:
41 comments Page 1 of 5.
Thrineshneeraj said:
8 months ago
The base class pointer can point to base class objects and derived class objects.
To access the base part, which is in a derived class, the base class uses its pointer.
So vice versa is not there. i.e. there is no derived class part in the base class, so the derived class pointer cannot access the base class.
To access the base part, which is in a derived class, the base class uses its pointer.
So vice versa is not there. i.e. there is no derived class part in the base class, so the derived class pointer cannot access the base class.
(5)
Atom said:
11 months ago
The base class pointer cannot point to the derived class: This is incorrect. A base class pointer can point to an object of the derived class, which is known as polymorphism (using a base class pointer to access derived class objects).
The derived class pointer cannot point to the base class: This is correct. A derived class pointer cannot point to an object of the base class without explicit typecasting, as the derived class may have additional members not present in the base class.
Pointer to derived class cannot be created: This is incorrect. Pointers to derived classes can be created just like pointers to any other type.
Pointer to base class cannot be created: This is incorrect. Pointers to base classes can be created and are commonly used, especially in polymorphism.
The derived class pointer cannot point to the base class: This is correct. A derived class pointer cannot point to an object of the base class without explicit typecasting, as the derived class may have additional members not present in the base class.
Pointer to derived class cannot be created: This is incorrect. Pointers to derived classes can be created just like pointers to any other type.
Pointer to base class cannot be created: This is incorrect. Pointers to base classes can be created and are commonly used, especially in polymorphism.
(2)
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.
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.
Rucha said:
4 years ago
C is a top-down approach and cpp is a bottom-up approach.
(3)
Priya modak said:
8 years ago
I agree because it is a top-down approach.
Ari said:
8 years ago
Yes, you are Right @Nayan.
Down Casting is way to point base class by derived class pointer.
Down Casting is way to point base class by derived class pointer.
Nayan said:
9 years ago
I do not agree with this. Just because it can be done in down casting by dynamic_cast.
(1)
Yash said:
9 years ago
Hierarchy is the base class to derived class.
Controls call base class first and then derived class.
So, there is no point a derived class would point a base class unless base class is "virtual".
Controls call base class first and then derived class.
So, there is no point a derived class would point a base class unless base class is "virtual".
(1)
PREEYANKA said:
10 years ago
Base class pointer can point to base class object and derived class objects. To access base part, which is in derived class, base class uses its pointer. So vice versa is not their. i.e their is no derived class part in base class, so derived class pointer cannot access base class.
(4)
Mahejbin said:
10 years ago
Extending the base class by name so the derived class know which data we are using maybe derived class point to the base class.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers