Polymorphism

I am needing help with polymorphism. I have no clue how to work with this. I have to write a program that creates an Orc and a Human and demonstrates the behavior of the classes. I am just needing some help with setting this program up. I have set up the classes with the information, but how do I get the createCharacter function to work? I know my program is not correct right now and I have some errors and things, but I am still just trying to get a better understanding of this. These are the requirements:

We will have a “Character” class first. Character class takes care of creation of a character.

Protected Member Variable:
characterTotal - This will hold the total of character stats.
Public Member Functions:
createCharacter - This function should be a pure virtual function.

Next, define a class named Human. It should be derived from the Character class.
It should have the following members:

Private Member Variables:
characterStrength - STR of the Character (0 to 18)
characterDexterity - DEX of the Character (0 to 18)
characterIntelligence - INT of the Character (0 to 18)
characterType - (Can be “Paladin”, “Ranger” or “Wizard”)

Public Member Functions:
constructor - accepts values for STR, DEX, INT and character type. Should call
the overridden createCharacter function.
getStrength - returns the value of characterStrength.
getDexterity - returns the value of characterDexterity.
getIntelligence - returns the value of characterIntelligence.
getType - returns the value of characterType.


Next, define a class named Orc. It should be derived from the Character class. It should have the following members:
Private Member Variables:
characterStrength - STR of the Character (0 to 18)
characterDexterity - DEX of the Character (0 to 18)
characterIntelligence - INT of the Character (0 to 18)
characterClan - (Can be “Barbarian”, “Berserker” or “Vanguard”)

Public Member Functions:
constructor - accepts values for STR, DEX, INT and character clan. Should call
the overridden createCharacter function.
getStrength - returns the value of characterStrength.
getDexterity - returns the value of characterDexterity.
getIntelligence - returns the value of characterIntelligence.
getClan - returns the value of characterClan.

At this step we can define createCharacter and showCharacter functions of each of the classes.

For Human class createCharacter will:
Get the values of STR, DEX and INT. Will calculate the total of the values.
(Let’s assume STR = 17, DEX = 12 and INT = 10. It will store 37 into characterTotal.Itwill print out a message: “The strong human Paladin, has a total scoreof 37.” (Strong adjective comes due to STR being 17. If something is above 17 you should say something related. STR = strong, DEX = dexterous, INT =
intelligent).

For Orc class createCharacter will:
Get the values of STR, DEX and INT. Will calculate the total of the values.
However Orcs receive -2 to INT and DEX. They receive +2 to STR. (Let’s
assume STR = 16, DEX = 10 and INT = 8. It will store 16+2,10-2,8-2 = 28 into
characterTotal.
It will print out a message “The berserker Orc has a total score of 28.” (Here the
Orcs get their adjectives from their clan names so you do not need to do
something specific to STR, DEX or INT.)





Last edited on
This is my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>  
#include "Character.h"
#include "Human.h"
#include "Orc.h"

using namespace std;

//Main.cpp
int main()
{
	char choice;
	char userC;
	cout << "Welcome!\n";
	cout << "" << endl;
	
	cout << "Pick your choice:\n";
	cout << "A -- Human\n";
	cout << "B -- Orc\n";
	cin >> choice;
	
	switch (choice)
	{
		case 'a':
		case 'A': 
					cout << "Please choose one of the following\n";
					cout << "A -- Paladin \n";
					cout << "B -- Ranger \n";
					cout << "C -- Wizard \n";\
					cin >> userC;
					cout << "" << endl;
					createCharacter();
					characterTotal = characterStrength + characterDexterity + characterIntelligence 
					
		
		
		
		
		
		
		
		
		case 'b':
		case 'B':
					cout << "Please choose one of the following\n";
					cout << "A -- Barbarian \n";
					cout << "B -- Berserker \n";
					cout << "C -- Vanguard \n";\
					cin >> userC;
					cout << "" << endl;
					createCharacter();
					characterTotal = characterStrength + characterDexterity + characterIntelligence 
					
		
		
		
		
		
	}
	
	return 0;
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//character.h
#ifndef CHARACTER_H
#define CHARACTER_H

using namespace std;

class Character
{
	protected:
				float characterTotal;
				
	public:
				virtual void createCharacter() = 0; //Pure virtual function
				
				
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef HUMAN_H
#define HUMAN_H

#include "Character.h"
using namespace std;

class Human
{
	private:
				int characterStrength();
				int characterDexterity();
				int characterIntelligence();
				string characterType();
				
	public:
				Human();//Constructor 
				
				int getStength()
				{
					cout << "Enter a number from 0 -18\n";
					return characterStrength;
				}
				
				int getDexterity()
				{
					return CharacterDexterity;
				}
				
				int getIntelligence()
				{
					return characterIntelligence;
				}
				
				string getType()
				{
					return characterType;
				}
				
				
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//orc.h
#ifndef ORC_H
#define ORC_H

#include "Character.h"
#include "Human.h"

using namespace std;

class orc
{
	private:
				int characterStrength();
				int characterDexterity();
				int characterIntelligence();
				string characterClan();
				
	public:
				orc(); //Constructor
				
				int getStrength()
				{
					return characterStrength;
				}
				
				int getDexterity()
				{
					return characterDexterity;
				}
				
				int getIntelligence()
				{
					return characterIntelligence;
				}
				string getClan()
				{
					return characterClan;
				}
				
};
#endif 
Topic archived. No new replies allowed.