Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class A {
- private:
- int x;
- public:
- A(int x = 0) {
- if (x > 2) {
- throw (int) 1;
- }
- this->x = x;
- }
- static const int year;
- virtual ~A() {}
- };
- const int A::year = 4;
- class B : public A {
- private:
- int y;
- public:
- B(int x = 0, int y = 0) : A(x) { this->y = y; }
- ~B() {}
- };
- class C : public A {
- private:
- int z;
- public:
- C(int x = 0, int z = 0) : A(x) { this->z = z; }
- void print() {
- cout << "I am from C." << endl;
- }
- };
- /*
- * Virtual - makes the base pointer point to the derived function
- * Pure Virtual = Interface, forces you to inherit, makes the base class abstract meaning you can not make objects of it
- */
- class Book {
- private:
- A **letters;
- int len;
- public:
- Book() {
- letters = NULL;
- len = 0;
- }
- void print() {
- int bs = 0;
- int cs = 0;
- for (int i = 0; i < len; ++i) {
- A *b = dynamic_cast<B *>(letters[i]);
- if (b) { bs++; }
- A *c = dynamic_cast<C *>(letters[i]);
- if (c) { cs++; }
- }
- cout << "B: " << bs << endl;
- cout << "C: " << cs << endl;
- }
- Book &operator+=(A *a) {
- A **old = letters;
- letters = new A *[len + 1];
- for (int i = 0; i < len; ++i) {
- letters[i] = old[i];
- }
- letters[len++] = a;
- delete[] old;
- return *this;
- }
- Book &operator=(const Book &b) {
- if (this == &b) return *this;
- /* Delete old objects in this */
- for (int i = 0; i < len; ++i) {
- delete[] letters[i];
- }
- delete[] letters;
- letters = new A *[b.len + 1];
- for (int j = 0; j < len; ++j) {
- letters[j] = b.letters[j];
- }
- len = b.len;
- return *this;
- }
- };
- int main() {
- B *ptrB = new B(5, 6);
- C *ptrC = new C(7, 3);
- C *ptrC2 = new C(1, 9);
- A *ptr[] = {ptrB, ptrC, ptrC2};
- try {
- A a(3);
- cout << a.year << endl;
- } catch (int){
- cerr << "fuck/n";
- }
- Book book;
- book += new B(5, 6);
- book += new C(5, 6);
- book += new C(5, 6);
- book += new B(5, 6);
- book.print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement