Advertisement
Dr4noel

Dem de virtual

May 22nd, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <iostream>
  2. #include< conio.h>
  3.  
  4. using namespace std;
  5.  
  6. class TestVirtual {
  7. public:
  8.     virtual void functie() {
  9.         cout << "print base class" << endl;
  10.     }
  11.     void functie3() {
  12.         cout << "show base class" << endl;
  13.     }
  14. };
  15.  
  16. class TestMostenire : public TestVirtual {
  17. public:
  18.     void functie() {
  19.         cout << "print derivated class" << endl;
  20.     }
  21.  
  22.     void functie3() {
  23.         cout << "show derivaated class" << endl;
  24.     }
  25. };
  26.  
  27. void main() {
  28.     TestVirtual *bptr, c;
  29.     TestMostenire d;
  30.     bptr = new TestVirtual();
  31.     //Daca folosesti obiect de tipul clasei o sa folosesti automat functiile de le ai in clasa
  32.     bptr->functie();
  33.  
  34.     bptr->functie3();
  35.  
  36.     //DACA FOLOSESTI OBIECT DE TIP POINTER DIN CLASA DE BAZA SI VREI SA ACCESZI CE AI IN DERIVATA ITI TREBUIE VIRTUAL
  37.     bptr = &d;
  38.  
  39.     cout << endl;
  40.  
  41.     bptr->functie();
  42.  
  43.     bptr->functie3();
  44.  
  45.     cout << endl;
  46.  
  47.     //Daca folosesti obiect de tipul clasei o sa folosesti automat functiile de le ai in clasa
  48.  
  49.     d.functie();
  50.  
  51.     d.functie3();
  52.  
  53.     _getch();
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement