Advertisement
vvccs

virtual function

Jun 6th, 2023
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class A
  5. {
  6. public:
  7.     void display() {}
  8. };
  9.  
  10. class B : public A
  11. {
  12. public:
  13.     virtual void display()
  14.     {
  15.         cout << "\nDisplay function of B Class";
  16.     }
  17. };
  18.  
  19. class C : public A
  20. {
  21. public:
  22.     virtual void display()
  23.     {
  24.         cout << "\nDisplay function of C Class";
  25.     }
  26. };
  27.  
  28. class D: public B, public C{
  29.     public:
  30.     void display(){
  31.         cout << "\nDisplay function of D Class, since B & C class have virtual functions";
  32.     }
  33. };
  34.  
  35. int main()
  36. {
  37.     B *aa;
  38.     D obj;
  39.     aa= &obj;
  40.     aa->display();
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement