Using vectors vs arrays.

So I'm new to C++ and am trying to understand inheritance. Specifically in my cod below i'm looking how to call my functions.

I know how to do this when my functions are static, but what changes when they are not?


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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

class Characters{
	
public:

	Characters();
	void SetValues();
	int GetAttackDamage();
	int GetHealth();
	int GetArmor();
	int GetAttack();
	int GetSpeed();
	int GetRegen();
	

private:
	int health, armor, attack, speed, regen;

};


 void Characters::SetValues()
{
	cout << "\nEnter Health Value: ";
	std::cin >> health;
	cout << "\nEnter Armor Value: ";
	std::cin >> armor;
	cout << "\nEnter Attack Value: ";
	std::cin >> attack;
	cout << "\nEnter Speed Value: ";
	std::cin >> speed;
	cout << "\nEnter Regen Value: ";
	std::cin >> regen;



}

int Characters::GetAttackDamage()
{
	return{ attack * speed };
}

int Characters::GetHealth()
{
	return health;
}
int Characters::GetArmor()
{
	return armor;
}
int Characters::GetAttack()
{
	return attack;
}
int Characters::GetSpeed()
{
	return speed;
}
int Characters::GetRegen()
{
	return regen;
}



class Monsters:public Characters {

public:
	Monsters();
		
	
	

private:
	
};



int main()
{
	

	Characters::SetValues();<<<<<<<<<THIS!
	
	system("pause");
	return 0;
	
}
Last edited on
You have to create an object of the class you have created:

1
2
Characters character;   //created object character of type Characters
character.SetValues(); //use dot operator to access function of that object 


You need to define the constructor as well. There's just a function declaration for it.
Thank you both, that was just the explanation I was needing.
Sorry back again, still stuck at the same issue. With your help i was lead to believe i was missing this :

"In C and C++, functions must be declared before the are used. You can declare a function by providing its return value, name, and the types for its arguments. The names of the arguments are optional.

A function definition counts as a function declaration"


Which altered my code to:

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

class Characters{
	
public:

	Characters();
	void SetValues(int health, int armor, int attack, int speed, int regen);
	int GetAttackDamage();
	int GetHealth();
	int GetArmor();
	int GetAttack();
	int GetSpeed();
	int GetRegen();
	

private:
	int health, armor, attack, speed, regen;

};


void Characters::SetValues(int health, int armor, int attack, int speed, int regen)
{
	cout << "\nEnter Health Value: ";
	std::cin >> health;
	cout << "\nEnter Armor Value: ";
	std::cin >> armor;
	cout << "\nEnter Attack Value: ";
	std::cin >> attack;
	cout << "\nEnter Speed Value: ";
	std::cin >> speed;
	cout << "\nEnter Regen Value: ";
	std::cin >> regen;
}

int Characters::GetAttackDamage()
{
	return{ attack * speed };
}

int Characters::GetHealth()
{
	return health;
}
int Characters::GetArmor()
{
	return armor;
}
int Characters::GetAttack()
{
	return attack;
}
int Characters::GetSpeed()
{
	return speed;
}
int Characters::GetRegen()
{
	return regen;
}

/*
class Monsters:public Characters {

public:
	Monsters();
		
	
	

private:
	
};
*/


int main()
{
	
	
	Characters characterOne;
	characterOne.SetValues(int health, int armor, int attack, int speed, int regen);
	
	system("pause");
	return 0;
	
}


The change here : void SetValues(int health, int armor, int attack, int speed, int regen);

needs to then be reflected when I call the function for my chracterOne object correct??
Last edited on
Hello.

Almost there,

Line 87: This is where you pass through the values you want to set. There is no need for the type-specifiers.

Essentially you are calling the member function that belongs to the object 'charcterOne'.

http://www.cplusplus.com/doc/tutorial/functions/
Last edited on
If the class function is collecting the data input for those variables, it doesn't seem that you need to send variables from main. e.g. characterOne.SetValues()
@WildBlue can you elaborate?

JRHomes i understand that ideally you would do this:

characterOne.SetValues(10,51,20,89,15); Correct?

since my function is to get user input i would not pass the values as above.
This is where I'm lost on how to handle this scenario.
since my function is to get user input i would not pass the values as above.


That's basically what I was getting at. You don't need to send values or variables to the SetValues function from main because you've made SetValues prompt for input and assign the input to the class data members (health, armor etc) that the function has access to.

line 12 void SetValues(int health, int armor, int attack, int speed, int regen);
line 27
1
2
void Characters::SetValues(int health, int armor, int attack, int speed, int regen)
{//code here} 

line 87 characterOne.SetValues(int health, int armor, int attack, int speed, int regen);
Precisely what wildblue said. If you are not sending through the values, there is no need for the parameters if the function is handling that.
Great thank you, got everything working. Not sure why I was making it more difficult than it was, I had confused myself somewhere along the way.
Hi i'm back.
I'm now trying to create a menu for the user to create a monster or hunter (which are inherited classes of character) or view the hunters/monsters that have been created.

In my code below I'm stuck on the portion of viewing the created 'monsters object'.

So once a monster is created it should be stored(how do i do this for vectors in this case)? How do I access that list of created objects that are stored in a vector? I've created a function 'selectMonster' so far.

I used this as a resource but this is called in the main, what happens when I use it in a function or class? http://www.dreamincode.net/forums/topic/63358-store-class-objects-in-vector/

I've created a vector of "<characters> * chars"

So with that, this code needs to reference that vector to store my newly created object

1
2
3
4
5
6
7
 
else if (option == 2) // Checking if user selected option 2
	{                
			//Create New Monster
			Monsters mOne;
			mOne.SetValues();      
			system("pause");


^----I understand this is all wrong at the moment. Am I to put this new object into my vector "chars".


I think my real issue is not understanding classes fully. any tips?
Thanks.






Edit: Cleaned up code a bit (silly mistakes made it hard to read).





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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>

using namespace std;

class Characters{

private:
	int health, armor, attack, speed, regen;
	
public:

	
	virtual void SetValues();
	void getValues();
	
	 int GetAttackDamage();
	 int GetHealth();
	 int GetArmor();
	 int GetAttack();
	 int GetSpeed();
	 int GetRegen();
	
};
class Monsters:public Characters{
	
	vector<Characters *> chars;
	vector<Characters *>::iterator it;
	void NewMonster();
	void selectMonster();
	void EditMonster();

public:
		int menu();
};

void Monsters::NewMonster(){

	int select;
	Characters * chars;
}

void Characters::SetValues()
{

	cout << "\nEnter Health Value: ";
	std::cin >> health;
	cout << "\nEnter Armor Value: ";
	std::cin >> armor;
	cout << "\nEnter Attack Value: ";
	std::cin >> attack;
	cout << "\nEnter Speed Value: ";
	std::cin >> speed;
	cout << "\nEnter Regen Value: ";
	std::cin >> regen;
	
}

void Monsters::selectMonster()
{ // Function of a class.
	int select, i = 1;
	it = chars.begin(); // Set the iterator to the beginning of the vector.
	while (it != chars.end()){ // Until the end of the vector,
		cout << i << ". "; // Print the number for a line,
		(*it)->SetValues(); //Set Values
		++it; // Go to the next item in the list,
		++i; // And finally, make the next line the next number
	}
	cout << " "; // User shell input prompt
	cin >> select; // Get input from the user
	it = chars.begin(); // Set the iterator to the beginning of the vector.
	//advance(it, chars - 1); // Move the iterator to the item selected by the user. It's (sel - 1) because vectors start at 0, and the list as printed starts at 1.
}


int Characters::GetAttackDamage()
{ 
	return{ attack * speed };
}

int Characters::GetHealth()
{
	return health;
}
int Characters::GetArmor()
{
	return armor;
}
int Characters::GetAttack()
{
	return attack;
}
int Characters::GetSpeed()
{
	return speed;
}
int Characters::GetRegen()
{
	return regen;
}


int main()
{
	int option; // user's entered option will be saved in this variable
	do //do-while loop starts here.that display menu again and again until user select to exit program
	{
		//Displaying Options for the menu
		cout << "1) Create New Hunter " << endl;
		cout << "2) Create New Monster " << endl;
		cout << "3) Display Hunters" << endl;
		cout << "4) Dispaly Monsters " << endl;
		cout << "5) Terminate Program " << endl;
		//Prompting user to enter an option according to menu
		cout << "Please select an option : ";
		
		cin >> option;  // taking option value as input and saving in variable "option"

		if (option == 1) // Checking if user selected option 1
		{
			//Create New Charcters
			cout << " New Hunter created, enter stats:";
		
		}
		else if (option == 2) // Checking if user selected option 2
		{
			//Create New Monster
			Monsters mOne;
			mOne.SetValues();
			system("pause");
		}
		else if (option == 3) // Checking if user selected option 3
		{
			//Display Characters

		}
		else if (option == 4) //Checking if user selected option 4
		{
			//Display Monsters
			
		}
		else if (option == 5) //Dispaly Monsters 
		{
			cout << "Terminating Program" << endl;
		}
		else //if user has entered invalid choice (other than 1,2,3,4 or 5)
		{
			//Displaying error message
			cout << "Invalid Option entered" << endl;
		}
	} while (option != 5);  //condition of do-while loop
	
	
	return 0;
	
}
Last edited on
I had to start over since I believe I made far too many mistakes.

In my Code below, what would be the most ideal way of creating new objects and storing them (arrays or vectors)?

I'd like to understand Vectors but can't quite understand where to begin in my code below. thanks.



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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
#include <memory>

using namespace std;

class Characters{

protected:
	int health, armor, attack, speed, regen;
	string name;

//Base Class
public: 

	void SetName()
	{
		cout << "\n Enter Name: ";
		std::cin >> name;
	}

	void SetValues()
	{
		cout << "\nEnter Health Value: ";
		std::cin >> health;
		cout << "\nEnter Armor Value: ";
		std::cin >> armor;
		cout << "\nEnter Attack Value: ";
		std::cin >> attack;
		cout << "\nEnter Speed Value: ";
		std::cin >> speed;
		cout << "\nEnter Regen Value: ";
		std::cin >> regen;
	}

	
	
	int GetHealth()
	{
		return health;
	}
	int GetArmor()
	{
		return armor;
	}
	int GetAttack()
	{
		return attack;
	}
	int GetSpeed()
	{
		return speed;
	}
	int GetRegen()
	{
		return regen;
	}

	void GetValues()
	{
		cout << "\nHealth :" << health << "\nArmor :" << armor << "\nAttack :" << attack << "\nSpeed :" << speed << "\nRegen :" << regen;
	}
	
};

class Monsters:public Characters{
	
	int menu();

public:
	int GetClawAttack(){
		return (armor + attack + health / speed);
		cout << armor + attack + health / speed;
	}
	int GetChargeAttack(){
		return (armor + speed/regen);
		cout << armor + speed / regen;
	}
	
	
};

	
	class Hunters :public Characters{



	public:

		int GetCleaveAttack(){
			return ( attack + speed + armor );
			cout << attack + speed + armor;
		}
		int GetStrikeAttack(){
			return ( speed + attack *2 );
			cout << speed + attack * 2;
		}


	};


int main()
{
	
	
	int option; // user's entered option will be saved in this variable
	do //do-while loop starts here.that display menu again and again until user select to exit program
	{
		//Displaying Options for the menu
		cout << "1) Create New Hunter " << endl;
		cout << "2) Create New Monster " << endl;
		cout << "3) Display Hunters" << endl;
		cout << "4) Dispaly Monsters " << endl;
		cout << "5) Terminate Program " << endl;
		//Prompting user to enter an option according to menu
		cout << "Please select an option : ";
		
		cin >> option;  // taking option value as input and saving in variable "option"

		if (option == 1) // Checking if user selected option 1
		{
			//Create New Charcters
			cout << " New Hunter created, enter stats:";
		
		}
		else if (option == 2) // Checking if user selected option 2
		{
			//Create New Monster
			Monsters mOne;
			mOne.SetValues();
			mOne.GetValues();
			system("pause");
		}
		else if (option == 3) // Checking if user selected option 3
		{
			//Display Characters
			
		}
		else if (option == 4) //Checking if user selected option 4
		{
			//Display Monsters
			
		}
		else if (option == 5) //Dispaly Monsters 
		{
			cout << "Terminating Program" << endl;
		}
		else //if user has entered invalid choice (other than 1,2,3,4 or 5)
		{
			//Displaying error message
			cout << "Invalid Option entered" << endl;
		}
	} while (option != 5);  //condition of do-while loop
	
	
	return 0;
	
}
Topic archived. No new replies allowed.