Advertisement
Filip_Markoski

Letters

May 19th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class A {
  6. private:
  7.     int x;
  8.  
  9. public:
  10.     A(int x = 0) {
  11.         if (x > 2) {
  12.             throw (int) 1;
  13.         }
  14.         this->x = x;
  15.     }
  16.  
  17.     static const int year;
  18.  
  19.     virtual ~A() {}
  20. };
  21.  
  22. const int A::year = 4;
  23.  
  24. class B : public A {
  25. private:
  26.     int y;
  27.  
  28. public:
  29.     B(int x = 0, int y = 0) : A(x) { this->y = y; }
  30.  
  31.     ~B() {}
  32. };
  33.  
  34. class C : public A {
  35. private:
  36.     int z;
  37. public:
  38.     C(int x = 0, int z = 0) : A(x) { this->z = z; }
  39.  
  40.     void print() {
  41.         cout << "I am from C." << endl;
  42.     }
  43.  
  44.  
  45. };
  46.  
  47. /*
  48.  * Virtual - makes the base pointer point to the derived function
  49.  * Pure Virtual = Interface, forces you to inherit, makes the base class abstract meaning you can not make objects of it
  50. */
  51.  
  52. class Book {
  53. private:
  54.     A **letters;
  55.     int len;
  56. public:
  57.     Book() {
  58.         letters = NULL;
  59.         len = 0;
  60.     }
  61.  
  62.     void print() {
  63.         int bs = 0;
  64.         int cs = 0;
  65.         for (int i = 0; i < len; ++i) {
  66.             A *b = dynamic_cast<B *>(letters[i]);
  67.             if (b) { bs++; }
  68.             A *c = dynamic_cast<C *>(letters[i]);
  69.             if (c) { cs++; }
  70.         }
  71.         cout << "B: " << bs << endl;
  72.         cout << "C: " << cs << endl;
  73.     }
  74.  
  75.     Book &operator+=(A *a) {
  76.         A **old = letters;
  77.         letters = new A *[len + 1];
  78.         for (int i = 0; i < len; ++i) {
  79.             letters[i] = old[i];
  80.         }
  81.         letters[len++] = a;
  82.         delete[] old;
  83.         return *this;
  84.     }
  85.  
  86.     Book &operator=(const Book &b) {
  87.         if (this == &b) return *this;
  88.         /* Delete old objects in this */
  89.         for (int i = 0; i < len; ++i) {
  90.             delete[] letters[i];
  91.         }
  92.         delete[] letters;
  93.         letters = new A *[b.len + 1];
  94.         for (int j = 0; j < len; ++j) {
  95.             letters[j] = b.letters[j];
  96.         }
  97.         len = b.len;
  98.         return *this;
  99.     }
  100. };
  101.  
  102. int main() {
  103.     B *ptrB = new B(5, 6);
  104.     C *ptrC = new C(7, 3);
  105.     C *ptrC2 = new C(1, 9);
  106.     A *ptr[] = {ptrB, ptrC, ptrC2};
  107.  
  108.     try {
  109.         A a(3);
  110.         cout << a.year << endl;
  111.     } catch (int){
  112.         cerr << "fuck/n";
  113.     }
  114.  
  115.     Book book;
  116.     book += new B(5, 6);
  117.     book += new C(5, 6);
  118.     book += new C(5, 6);
  119.     book += new B(5, 6);
  120.     book.print();
  121.     return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement