Advertisement
varungurnaney

HSC: Virtual Keyword

Oct 8th, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. /* A virtual member is a member function that can be redefined in a derived class, while preserving its calling properties through references. The syntax for a function to become virtual is to precede its declaration with the virtual keyword:
  2. */
  3.  
  4. #include<iostream.h>
  5. #include<conio.h>
  6.  
  7.  
  8. class vehicle
  9. {
  10. protected: float mileage;
  11. public:
  12.     vehicle(float m1)
  13.     {
  14.     mileage=m1;
  15.     }
  16.  
  17.     virtual void display()
  18.     {
  19.     cout<<"vehicle details:\n";
  20.     }
  21.  
  22. };
  23.  
  24.  
  25. class two:public vehicle
  26. {
  27. char clutch;
  28. public:
  29.     two(float m1,char c):vehicle(m1)
  30.     {
  31.     clutch=c;
  32.     }
  33.  
  34.     void display()
  35.     {
  36.      cout<<"\nMileage: "<<mileage;
  37.      cout<<"\nClutch: "<<clutch;
  38.      }
  39. };
  40.  
  41. class four:public vehicle
  42. {
  43.     char ac;
  44.     public:
  45.       four(float m1, char c):vehicle(m1)
  46.       {
  47.       ac=c;
  48.       }
  49.  
  50.       void display()
  51.       {
  52.  
  53.      cout<<"\nMileage:"<<mileage;
  54.      cout<<"\nAC:"<<ac;
  55.      }
  56. };
  57.  
  58.  
  59. void main()
  60. {
  61. vehicle *b;
  62. clrscr();
  63.  
  64. two activa(40,'y');
  65. b=&activa;
  66. b->display();
  67.  
  68. four maruti(17,'n');
  69. b=&maruti;
  70. b->display();
  71.  
  72. four bmw(5,'y');
  73. b=&bmw;
  74. b->display();
  75. getch();
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement