I want to make a class superhero which contain a protected data member health and member function:
1- non default constructor that initialize health if entered >100 to 100
2-Apply power function which takes a pointer of super hero class P and heals it by incrementing health by 10.
and another class super fighter which contains private data member:
firepower represent the fire power the float can represent
Public member functions:
1-non default constructor which take health and firepower and initializes data member.
2-Applypower: it takes a pointer of SuperHero class P. It applies the power as follows:
If the current SuperFighter (the calling object) is close to die (health < 20),
then he becomes friendly and heals P (using ApplyPowers of SuperHero class).
If the current SuperFighter has health >= 20, then he becomes evil and fights
P by decrementing the health of P by the firepower of the current super fighter.
Remember: health can be negative but not more than 100
the part of when the health is less than 20 is working good,but when i make the health bigger than 20 it decrements the health of te superfighter not the superhero.
What I have tried:
//Header file of superHero:
#pragma once
#include <iostream>
#include <string>
using namespace std;
class SuperHero {
protected:
float Health;
public:
SuperHero(float);
virtual void ApplyPower(SuperHero*);
void print();
};
//.cpp file of super hero
#include <string>
#include "2.h"
using namespace std;
SuperHero::SuperHero(float h) {
Health = (h > 100) ? 100 : h;
}
void SuperHero::ApplyPower(SuperHero* p) {
if (p->Health >= 95 && p->Health <= 100) {
p->Health = 100;
}
else
{
{
p->Health +=10;
}
}
}
void SuperHero::print() {
cout << Health << ' ';
}
//superfighter header file:
#pragma once
#include <string>
#include "2.h"
#include <iostream>
using namespace std;
class superfighter :protected SuperHero {
private:
float firepower;
friend class SuperHero;
public:
superfighter(float, float);
void ApplyPower(SuperHero*);
void print();
};
//superfighter .cpp
#include <string>
#include "2.h"
#include <iostream>
#include "3.h"
using namespace std;
superfighter::superfighter(float h, float f) :SuperHero(h){
firepower = f;
}
void superfighter::ApplyPower(SuperHero* p) {
if (Health < 20) {
p->ApplyPower(p);
}
else {
SuperHero::Health -=firepower;
}
}
void superfighter::print() {
cout << Health << ' ' << firepower << endl;
}
//main Function
#include <string>
#include "2.h"
#include <iostream>
#include "3.h"
using namespace std;
int main() {
float a, b, c;
cin >> a;
cin >> b;
cin >> c;
SuperHero h1(a);
superfighter f1(b, c);
f1.ApplyPower(&h1);
h1.print();
f1.print();
}