Skip to content

Commit

Permalink
Виртуален деструктор
Browse files Browse the repository at this point in the history
  • Loading branch information
triffon committed May 11, 2022
1 parent ebcda45 commit 26274be
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
22 changes: 19 additions & 3 deletions lectures/1/player/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <iostream>
#include <typeinfo>
#include "player.hpp"
#include "hero.hpp"
#include "superhero.hpp"
Expand Down Expand Up @@ -69,6 +70,8 @@ void testHero() {
}

void testSuperHero() {
std::cout << sizeof(Player) << std::endl;

SuperHero superman("Супермен", 100, 5, "летене", false, 10);
superman.println();
SuperHero superman2 = superman;
Expand Down Expand Up @@ -109,8 +112,17 @@ void testSuperHero() {
std::cout << "Изглежда героите са равносилни!";
}

SuperHero drstrange("Dr. Strange", 20, 8, "отваряне на портали", true, 9);
hero = &drstrange;
std::cout << typeid(hero).name() << ' ' << typeid(*hero).name() << std::endl;

if (typeid(*hero) == typeid(SuperHero))
std::cout << "Я, това било супергерой!\n";

hero = new SuperHero("Dr. Strange", 20, 8, "отваряне на портали", true, 9);

std::cout << typeid(hero).name() << ' ' << typeid(*hero).name() << std::endl;

if (typeid(*hero) == typeid(SuperHero))
std::cout << "Я, това било супергерой!\n";

winner = Hero::battle(superman, *hero, 40);
if (winner != nullptr) {
Expand All @@ -120,14 +132,18 @@ void testSuperHero() {
std::cout << "Изглежда героите са равносилни!";
}

drstrange.deactivatePower();
((SuperHero*)hero)->deactivatePower();
winner = Hero::battle(superman, *hero, 50);
if (winner != nullptr) {
std::cout << "Имаме победител!" << std::endl;
winner->println();
} else {
std::cout << "Изглежда героите са равносилни!";
}

std::cout << "Изтриваме hero:\n";
delete hero;
std::cout << "Изтрихме hero!\n";
}

int main() {
Expand Down
2 changes: 1 addition & 1 deletion lectures/1/player/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Player {
Player(const char* _name = "<анонимен>", unsigned _score = 0);
Player(Player const&);
Player& operator=(Player const&);
~Player();
virtual ~Player();

char const* getName() const { return name; }
unsigned getScore() const { return score; }
Expand Down
1 change: 1 addition & 0 deletions lectures/1/player/superhero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ SuperHero& SuperHero::operator=(SuperHero const& other) {

SuperHero::~SuperHero() {
// !!! Hero::~Hero();
std::cout << "~SuperHero()\n";
delete[] power;
}

Expand Down

0 comments on commit 26274be

Please sign in to comment.