Advertisement
Guest User

Untitled

a guest
Feb 5th, 2012
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include  "iostream";
  3.  
  4. using namespace std;
  5.  
  6.  
  7. //class A
  8. class A
  9. {
  10.     A* next;
  11.     static A* list;
  12. protected:
  13.     int nr;
  14. public:
  15.     A (int n) {nr = n; next = list; list = this;} //WHAT HAPPENS HERE ???
  16.     ~A()  {};
  17.     virtual void f() { cout << "A::f " << nr << endl; }
  18.     static void printList();
  19. };
  20.  
  21. A* A::list = NULL;
  22.  
  23. void A::printList()  
  24. {
  25.     for (A* p = list; p ; p = p->next)  
  26.         p->f();
  27.    
  28.     cout << "\n";
  29. }
  30.  
  31.  
  32. //class B
  33. class B : public A
  34. {
  35.     char* name;
  36.     public:
  37.         B(int n, char* str) : A(n), name(str) {}
  38.         virtual void f() { cout << "B::f " << nr << " " << name << endl;}
  39. };
  40.  
  41.  
  42. int _tmain(int argc, _TCHAR* argv[])
  43. {
  44.     A a1(10), a2(20), a3(30);
  45.     B bl(44,  "b1"), b2(55, "b2");
  46.     A a(60);
  47.  
  48.     A::printList();
  49.  
  50.     char f;
  51.     cin >> f;
  52.     return 0;  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement