Скачать презентацию
Идет загрузка презентации. Пожалуйста, подождите
Презентация была опубликована 9 лет назад пользователемИгорь Медников
1 Лекция 16. Введение в ООП. Часть 7 Красс Александр СПбГУ ИТМО, 2008
2 2 Темы Конструкторы и деструкторы при наследовании
3 3 Конструкторы при наследовании class A { public: A() { cout << "A()\n"; } }; class B : public A { public: B() { cout << "B()\n"; } }; void main() { A *a = new B; // Что мы увидим на экране? }
4 4 Конструкторы при наследовании class A { public: A() { cout << "A()\n"; } }; class B : public A { public: B() { cout << "B()\n"; } }; void main() { A *a = new B; // A() } // B()
5 5 Конструкторы при наследовании Множественное наследование – это зло! Но это не касается наследования интерфейсов.
6 6 Конструкторы при наследовании Пример интерфейса: class A { public: virtual void foo() = 0; virtual void boo() = 0; }
7 7 Конструкторы при наследовании Пример множественного наследования: class A {...}; class B {...}; class C : public A, public B {... };
delete i\n"; delete i; } }; class B : public A { int *j; public: B() { j = new int(7); } ~B() { cout << "~B() => delete j\n"; delete j; } }; void" title="8 Деструкторы при наследовании class A { int *i; public: A() { i = new int(5); } ~A() { cout << "~A() => delete i\n"; delete i; } }; class B : public A { int *j; public: B() { j = new int(7); } ~B() { cout << "~B() => delete j\n"; delete j; } }; void" class="link_thumb"> 8 8 Деструкторы при наследовании class A { int *i; public: A() { i = new int(5); } ~A() { cout << "~A() => delete i\n"; delete i; } }; class B : public A { int *j; public: B() { j = new int(7); } ~B() { cout << "~B() => delete j\n"; delete j; } }; void main() { A *a = new B; // Что мы delete a; // увидим? } delete i\n"; delete i; } }; class B : public A { int *j; public: B() { j = new int(7); } ~B() { cout << "~B() => delete j\n"; delete j; } }; void"> delete i\n"; delete i; } }; class B : public A { int *j; public: B() { j = new int(7); } ~B() { cout << "~B() => delete j\n"; delete j; } }; void main() { A *a = new B; // Что мы delete a; // увидим? }"> delete i\n"; delete i; } }; class B : public A { int *j; public: B() { j = new int(7); } ~B() { cout << "~B() => delete j\n"; delete j; } }; void" title="8 Деструкторы при наследовании class A { int *i; public: A() { i = new int(5); } ~A() { cout << "~A() => delete i\n"; delete i; } }; class B : public A { int *j; public: B() { j = new int(7); } ~B() { cout << "~B() => delete j\n"; delete j; } }; void">
delete i\n"; delete i; } }; class B : public A { int *j; public: B() { j = new int(7); } ~B() { cout << "~B() => delete j\n"; delete j; } }; void" title="9 Деструкторы при наследовании class A { int *i; public: A() { i = new int(5); } ~A() { cout << "~A() => delete i\n"; delete i; } }; class B : public A { int *j; public: B() { j = new int(7); } ~B() { cout << "~B() => delete j\n"; delete j; } }; void" class="link_thumb"> 9 9 Деструкторы при наследовании class A { int *i; public: A() { i = new int(5); } ~A() { cout << "~A() => delete i\n"; delete i; } }; class B : public A { int *j; public: B() { j = new int(7); } ~B() { cout << "~B() => delete j\n"; delete j; } }; void main() { A *a = new B; // ~A() => delete a; // delete i } // Деструктор B не вызовется! delete i\n"; delete i; } }; class B : public A { int *j; public: B() { j = new int(7); } ~B() { cout << "~B() => delete j\n"; delete j; } }; void"> delete i\n"; delete i; } }; class B : public A { int *j; public: B() { j = new int(7); } ~B() { cout << "~B() => delete j\n"; delete j; } }; void main() { A *a = new B; // ~A() => delete a; // delete i } // Деструктор B не вызовется!"> delete i\n"; delete i; } }; class B : public A { int *j; public: B() { j = new int(7); } ~B() { cout << "~B() => delete j\n"; delete j; } }; void" title="9 Деструкторы при наследовании class A { int *i; public: A() { i = new int(5); } ~A() { cout << "~A() => delete i\n"; delete i; } }; class B : public A { int *j; public: B() { j = new int(7); } ~B() { cout << "~B() => delete j\n"; delete j; } }; void">
delete i\n"; delete i; } }; class B : public A { int *j; public: B() { j = new int(7); } virtual ~B() { cout << "~B() => delete j\n"; de" title="10 Деструкторы при наследовании class A { int *i; public: A() { i = new int(5); } virtual ~A() { cout << "~A() => delete i\n"; delete i; } }; class B : public A { int *j; public: B() { j = new int(7); } virtual ~B() { cout << "~B() => delete j\n"; de" class="link_thumb"> 10 10 Деструкторы при наследовании class A { int *i; public: A() { i = new int(5); } virtual ~A() { cout << "~A() => delete i\n"; delete i; } }; class B : public A { int *j; public: B() { j = new int(7); } virtual ~B() { cout << "~B() => delete j\n"; delete j; } }; void main() { A *a = new B; // ~B() => delete a; // delete j } // ~A() => delete i delete i\n"; delete i; } }; class B : public A { int *j; public: B() { j = new int(7); } virtual ~B() { cout << "~B() => delete j\n"; de"> delete i\n"; delete i; } }; class B : public A { int *j; public: B() { j = new int(7); } virtual ~B() { cout << "~B() => delete j\n"; delete j; } }; void main() { A *a = new B; // ~B() => delete a; // delete j } // ~A() => delete i"> delete i\n"; delete i; } }; class B : public A { int *j; public: B() { j = new int(7); } virtual ~B() { cout << "~B() => delete j\n"; de" title="10 Деструкторы при наследовании class A { int *i; public: A() { i = new int(5); } virtual ~A() { cout << "~A() => delete i\n"; delete i; } }; class B : public A { int *j; public: B() { j = new int(7); } virtual ~B() { cout << "~B() => delete j\n"; de">
11 11 Для закрепления материала class A { int *i; public: A() { i = new int(5); } virtual void foo() { cout << "A::foo()\n"; } virtual ~A() { delete i; foo(); } }; class B : public A { int *j; public: B() { j = new int(7); } virtual void foo() { cout << "B::foo()\n"; } virtual ~B() { delete j; foo(); } }; void main() // Что мы увидим { // на экране? A *a = new B; delete a; }
12 12 Для закрепления материала class A { int *i; public: A() { i = new int(5); } virtual void foo() { cout << "A::foo()\n"; } virtual ~A() { delete i; foo(); } }; class B : public A { int *j; public: B() { j = new int(7); } virtual void foo() { cout << "B::foo()\n"; } virtual ~B() { delete j; foo(); } }; void main() // B::foo() { // A::foo() A *a = new B; delete a; }
13 13 Для закрепления материала class A { int *i; public: A() { i = new int(5); foo(); } virtual void foo() { cout << "A::foo()\n"; } }; class B : public A { int *j; public: B() { j = new int(7); foo(); } virtual void foo() { cout << B::foo()\n"; } }; void main() // Что мы увидим { // на экране? A *a = new B; delete a; }
14 14 Для закрепления материала class A { int *i; public: A() { i = new int(5); foo(); } virtual void foo() { cout << "A::foo()\n"; } }; class B : public A { int *j; public: B() { j = new int(7); foo(); } virtual void foo() { cout << B::foo()\n"; } }; void main() // A::foo() { // B::foo() A *a = new B; delete a; }
15 15 Спасибо за внимание Вопросы?
Еще похожие презентации в нашем архиве:
© 2024 MyShared Inc.
All rights reserved.