C++ | Inheritance | Question 6

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<iostream>
using namespace std;
  
class Base {};
  
class Derived: public Base {};
  
int main()
{
    Base *bp = new Derived;
    Derived *dp = new Base;
}

chevron_right


(A) No Compiler Error
(B) Compiler Error in line “Base *bp = new Derived;”
(C) Compiler Error in line ” Derived *dp = new Base;”
(D) Runtime Error


Answer: (C)

Explanation: A Base class pointer/reference can point/refer to a derived class object, but the other way is not possible.


Quiz of this Question

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.