fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class base{
  5. public:
  6. int b;
  7. void Print_b(){
  8. cout<<b<<endl;
  9. }
  10. };
  11.  
  12. class derived: public base{
  13. public:
  14. int d;
  15. void Print_d(){
  16. cout<<d<<endl;
  17. }
  18. };
  19. int main() {
  20. // your code goes here
  21. base b1;
  22. b1.b = 1;
  23. b1.Print_b();
  24.  
  25. derived d1;
  26. d1.d = 2;
  27. d1.Print_b();
  28.  
  29. d1.Print_d();
  30. cout<<"via pointers\n";
  31.  
  32. derived * bptr = &d1;
  33. bptr->Print_b();
  34.  
  35. bptr = & b1;
  36. bptr->Print_b();
  37. bptr->Print_d();
  38.  
  39. base * dptr = &d1;
  40. dptr->Print_b();
  41.  
  42.  
  43. return 0;
  44. }
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
Compilation error #stdin compilation error #stdout 0s 3340KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:35:7: error: invalid conversion from ‘base*’ to ‘derived*’ [-fpermissive]
  bptr = & b1;
       ^
stdout
Standard output is empty