Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
}
Posted
Updated 26-Apr-20 23:54pm

superfighter is a subclass of SuperHero.
ApplyPower receives a SuperHero instance through parameter p.
You want it to change p->Health but instead you are changing Health value of the superfigther instance.
"SuperHero::Health -= firepower" is not using "p" parameter, thus is obviously not changing anything of p.

protected modifier allow a class member to be protected from direct/explicit outside motifications; changing the value from another instance (like you want to do in superfighter::ApplyPower) is considered outside modification;

If you want to change Health of a SuperHero instance from another SuperHero instance (even a superfighter one), you have to make Health public or to provide a public method that implements the operation.

One solution (not necessarily the best one) is to declare one or more methods in SuperHero to change Health value.
You can think something like:
C++
void Heal(SuperHero* healer, float amount)
{
  if (amount > 0)
    Health += amount;
}
void Damage(SuperHero* attacker, float amount)
{
  if (amount > 0)
    Health -= amount;
}


The two example methods will change the Health of the current instance by the specified amount.
The healer and attacker parameters let you implement additional logic, like decide whether to apply the given amount or to apply additional effects.

You can then replace
C++
SuperHero::Health -=firepower;

with
C++
p->Damage(this, firepower);
 
Share this answer
 
v2
A word about inheritance:

When one class inherits from a base class (also called super class), it means that it inherits all the properties and functions of that base class. An instance of that derived class is still only one instance though! There is no separate instance of the base class! E. g. when you specify
C++
#include <iostream>
class animal {
public:
   virtual void speak() {} // default: doesn't speak a lot...
};
class cat : public animal {
public:
   void speak() { std::cout << "Meow" << std::endl ; }
};

There is no separate instance of class animal! A cat *is* already an animal. You can make your animal of choice 'speak' without the need to create or pass another object:
C++
int main() {
   animal* my_animal = new cat; // this is a cat. It is also an animal!
   my_animal->speak();
   delete my_animal;
   return 0;
}

Of course, the main use for using inheritance is that you have two or more derived classes, and functions or array that interact with instances of these classes through the interface of the base class:
C++
#include <iostream>
class animal {
public:
   virtual void speak() {} // default: doesn't speak a lot...
};
class cat : public animal {
public:
   void speak() { std::cout << "Meow" << std::endl ; }
};
class cow : public animal {
   void speak() { std::cout << "Moo" << std::endl; }
};
void talk(animal* first, animal* second) {
   first->speak();
   second->speak();
}
int main() {
   animal* my_cat = new cat;
   animal* my_cow = new cow;
   talk(my_cat, my_cow);
   delete my_cat;
   delete my_cow;
   return 0;
}

The function talk() doesn't know, nor does it need to know, what animals are talking here. It only knows they are animals which have a function to speak, because that is what the base class animal specifies.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900