In c++ You You will be making 3 classes - One class is Character, it is the base
ID: 3816216 • Letter: I
Question
In c++
You You will be making 3 classes -
One class is Character, it is the base class. It has a variable for health (int) and size (int)
The next is nonPlayer and the last is monster. They are derived off of Character
class nonPlayer has a talkative or nontalkative (true of false) data member
Monster has maxHit and MinHit data member (ints)
For every data member in the class you should have get and set functions.
In main, create all three types of classes and call a display function on it which shows the current value of the variables -- So, nonPlayer woudl say "Hi, I am talkative and I am size 3", and monster would say - The most "I hit is 30 and the min is 10. I am size 7". Character woudl just print "I am size 10".
You will have 7 files - 3 header files, and 4 cpp files.
Explanation / Answer
Create files(Mentioned in Bold font) and copy paste the code in corresponding file as mentioned below :
Character.h
#include<iostream>
class Character {
private :
int health;
int size;
public:
Character(int,int);
int getHealth();
void setHealth(int);
int getSize();
void setSize(int);
void dispaly();
};
NonPlayer.h
#include "Character.h"
class NonPlayer : public Character {
private;
bool talkative;
public :
NonPlayer();
bool isTalkative();
void setTalkative(bool);
void display();
};
Monster.h
#include "Character.h"
class NonPlayer : public Character {
private;
bool talkative;
public :
NonPlayer();
bool isTalkative();
void setTalkative(bool);
void display();
};
Character.cpp
#include "Character.h"
using namespace std;
Character :: Character() {
this->health = 0;
this-> size = 10;
}
int Character ::getSize() {
return size;
}
void Character :: setSize(int size) {
this->size = size;
}
int Character :: getHealth() {
return health;
}
void Character :: setHealth(int health) {
this->health = health;
}
void Character :: dispaly() {
cout << "I am size " << getSize();
}
NonPlayer.cpp
#include "NonPlayer.h"
using namespace std;
NonPlayer :: NonPlayer() {
talkative = true;
setSize(3);
}
bool NonPlayer :: isTalkative() {
return talkative;
}
void NonPlayer :: setTalkative(bool talkative) {
this-> talkative = talkative;
}
void NonPlayer :: display() {
cout << "Hi, I am" << isTalkative() ? " talkative" : "not talkative " << " and I am size " << getSize();
}
Monster.cpp
#include "Monster.h"
using namespace std;
Monster :: Monster() {
maxHit = 30;
minHit = 10;
setSize(7);
}
int Monster :: getMaxHit() {
return maxHit;
}
void Monster :: setMaxHit(int maxHit) {
this->maxHit = maxHit;
}
int Monster :: getMinHit() {
return minHit;
}
void Monster :: getMinHit()(int minHit) {
this->minHit = minHit;
}
void Monster :: display() {
cout << "I hit the max is " << getMaxHit() << " and the min is " << getMinHit() << " I am size " << getSize();
}
Main.cpp
#include "Monster.h"
#include "NonPlayer.h"
using namespace std;
int main()
{
Character character;
Monster monster;
NonPlayer nonPlayer;
nonPlayer.dispaly();
monster.dispaly();
character.dispaly();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.