абстрактный класс конструкторы с++

программирование с++ конструкторы

#include <iostream>

using namespace std;

class NegativHitPoints{};
class AttackDeadUnit{};

class Unit {

public:

int HitPoints;
int PowerPoints;

Unit( ): HitPoints(10), PowerPoints(5) {}

Unit ( int HitGift, int PowerGift ): HitPoints (HitGift), PowerPoints(PowerGift) {
if ( HitPoints < 0 ) {
throw NegativHitPoints();
}
}

virtual ~Unit( )= 0;

virtual void attack(Unit& other) {
if ( other.HitPoints == 0 ) {
throw AttackDeadUnit();
}

if ( HitPoints > 0 ) {

other.HitPoints -= PowerPoints;

if ( other.HitPoints == 0 ) {
cout << "DEAD bravo" << endl;
} else if ( other.HitPoints < 0 ) {
throw NegativHitPoints();
}
}
cout << "Unit attack" << endl;
other.counterAttack(*this);
}

virtual void Abstr ( ) = 0;

virtual void counterAttack(Unit &other) {
if ( other.HitPoints > 0 && HitPoints > 0 ) {
other.HitPoints -= PowerPoints / 2;
}

if ( other.HitPoints < 0 ) {
throw NegativHitPoints();
} else if ( other.HitPoints == 0 ) {
cout << "DEAD alfa" << endl;
}
cout << "Unit counterAttack" << endl;
}

int getHitPoints () const {
return HitPoints;
}

int getPowerPoints () const {
return PowerPoints;
}

};
class Soldier: public Unit {

public:

Soldier () : Unit () {}
Soldier (int HitGift, int PowerGift) : Unit (HitGift, PowerGift) {}

void attack (Unit& other) {
cout << "Soldier attack" << endl;
}
};
int main() {

Soldier tert;


return 0;
}

Скажите почему при запуске появляется ошибка

$ g++ unitlevel.cpp
unitlevel.cpp: In function 'int main()':
unitlevel.cpp:357:10: error: cannot declare variable 'tert' to be of abstract type 'Soldier'
unitlevel.cpp:71:28: note: because the following virtual functions are pure within 'Soldier':
unitlevel.cpp:45:15: note: virtual void Unit::Abstr()
Ответы:
virtual void Abstr ( ) = 0; - функция Abstr() она абстрактная. Ты ее должен переопределить в Soilder'e.


9 лет назад

RPI.su - самая большая русскоязычная база вопросов и ответов. Наш проект был реализован как продолжение популярного сервиса otvety.google.ru, который был закрыт и удален 30 апреля 2015 года. Мы решили воскресить полезный сервис Ответы Гугл, чтобы любой человек смог публично узнать ответ на свой вопрос у интернет сообщества.

Все вопросы, добавленные на сайт ответов Google, мы скопировали и сохранили здесь. Имена старых пользователей также отображены в том виде, в котором они существовали ранее. Только нужно заново пройти регистрацию, чтобы иметь возможность задавать вопросы, или отвечать другим.

Чтобы связаться с нами по любому вопросу О САЙТЕ (реклама, сотрудничество, отзыв о сервисе), пишите на почту [email protected]. Только все общие вопросы размещайте на сайте, на них ответ по почте не предоставляется.