Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ApkPolimorfizm.cpp : Defines the entry point for the console application.
- //
- #include <iostream>
- #include <fstream>
- using namespace std;
- class Character
- {
- private:
- char name[128];
- char type[128];
- public:
- const char *getType()
- {
- return type;
- }
- const char *getName()
- {
- return name;
- }
- virtual void draw() = 0;
- protected:
- Character(const char *nameVal, const char *typeVal)
- {
- for (int i = 0; i < strlen(nameVal)+1; i++)
- {
- name[i] = nameVal[i];
- }
- for (int i = 0; i < strlen(typeVal)+1; i++)
- {
- type[i] = typeVal[i];
- }
- }
- };
- class Warrior : public Character
- {
- private:
- float armor;
- public:
- Warrior(const char *nameVal, float armorVal) : Character(nameVal ,"Warrior"), armor(armorVal) {}
- void setArmorLevel(float armorVal)
- {
- armor = armorVal;
- }
- float getArmorLevel()
- {
- return armor;
- }
- void draw()
- {
- cout << "Imie: " << getName() << endl;
- cout << "Typ postaci: " << getType() << endl;
- cout << "Ilosc pancerza: " << getArmorLevel() << endl;
- }
- };
- class Enemy : public Character
- {
- private:
- float strenght;
- int concurrentWarriors;
- public:
- Enemy(const char *nameVal, const float strenghtVal, const int concurrentWarriorsVal) : Character(nameVal, "Enemy"), strenght(strenghtVal), concurrentWarriors(concurrentWarriorsVal){}
- float getStrenght()
- {
- return strenght;
- }
- void setStrenght(float strenghtVal)
- {
- strenght = strenghtVal;
- }
- int getConcurrentWarriors()
- {
- return concurrentWarriors;
- }
- void draw()
- {
- cout << "Imie: " << getName() << endl;
- cout << "Typ postaci: " << getType() << endl;
- cout << "Sila: " << getStrenght() << endl;
- cout << "Walczacy wojownicy: " << getConcurrentWarriors() << endl;
- }
- };
- int main()
- {
- const int characterCount = 6;
- Character *characters[characterCount] = {};
- characters[0] = new Warrior("Batman", 10.2);
- characters[1] = new Enemy("Joker", 5.1, 3);
- characters[2] = new Warrior("Superman", 55.3);
- characters[3] = new Enemy("Ultra-Humanite", 17.2, 10);
- characters[4] = new Warrior("Daredevil", 33.7);
- characters[5] = new Enemy("Wilson Fisk", 3.1, 10);
- for (int i = 0; i < characterCount; i++)
- {
- characters[i]->draw();
- }
- for (int i = 0; i < characterCount; i++)
- {
- if (characters[i])
- delete characters[i];
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement