in c++ use functions and calls main should not have a lot of code in it ernpioye
ID: 3903652 • Letter: I
Question
in c++
use functions and calls
main should not have a lot of code in it
Explanation / Answer
#include<iostream>
#include<string>
#include<stdlib.h>
#include<time.h>
using namespace std;
class Adventurer{
private:
int health;
public:
Adventurer(){
srand(time(NULL));
health = rand() % 100;
}
void takeHit(int a){
health = health - a;
}
int getHealth(){
return health;
}
};
class Party{
private:
Adventurer *list;
int max;
public:
Party(int a){
list = new Adventurer[a];
max = a;
}
~Party(){
delete [] list;
}
void damage(int i, int a){
if (i < max){
list[i].takeHit(a);
}
else {
cout << "Invalid index ";
}
}
int count(){
int cnt = 0;
for (int i = 0; i<max; i++){
if (list[i].getHealth() > 0)
cnt++;
}
return cnt;
}
};
int main(){
Party p(10);
cout << p.count() << endl;
p.damage(7,56);
p.damage(5,99);
cout << p.count() << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.