🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

C++ Workshop - Week 5 (Ch. 6 & Ch. 11) - Quizzes and Exercises

Started by
12 comments, last by Dbproguy 16 years, 1 month ago

Quizzes and Extra Credit for Week 5.

Post your answers to the Quizzes from Week 5 here rather than in the chapter thread, so as not to spoil things for the others. Chapter 6 Quiz 1. What was the ‘C’ capability which allowed you to combine related variables? Did this solve the problem of connecting data with behaviors? 2. How do you make a new type in C++? 3. What is a class? 4. What is another name for variables within a class? What is another name for the member functions? 5. What are the requisite parts of a class declaration? Show an example. 6. What are the two things a class declaration tells your compiler? 7. When discussing naming conventions, what’s the most important point to remember? 8. What is an object? 9. How do you declare an object? Show an example. 10. What operator is used to access members and methods of a class for objects created on the stack. 11. What is the difference between public and private members/methods within a class? Which is the default? 12. In general, should methods or members be private? Why? 13. What is the primary way you initialize the member data of a class? 14. How can you identify a constructor for a class? 15. What is a default constructor? How is it called. 16. What does “declaring a method of a class const” mean? What is the syntax for such a declaration? 17. In the context of classes and objects, what is a client? 18. Where do class declarations go? Where do the member function definitions go? 19. When one class has a member variable which is an object of a different class they are said to form a ___ relationship? 20. What is the only difference between C++ classes and C++ structs? Chapter 11 Quiz 1. What’s the purpose of a model? 2. What is the basic philosophy of iterative design/development? 3. What’s the difference between waterfall vs. iterative development? 4. What are the 6 steps listed in your book for iterative development. 5. What happens if you lose sight of the vision of your product? 6. What is a use case? 7. What is a domain expert? 8. What is an actor? 9. What are the 6 questions you should ask yourself about the actors when determining use cases, according to your book? 10. What is a domain model? 11. What is a scenario? 12. What are the 9 items, according to your book, should you include when documenting a scenario? 13. What is systems analysis? 14. What is the simplistic technique, according to your book, for determining the classes in your project? 15. What are the three areas of concern for the Static Model of your classes? 16. What is the most important principle when deciding the responsibilities of a class? 17. What are the two types of diagrams shown in the book for determining how classes interact? Exercise 1 – The random character generator In the game “Dungeons & Dragons” by WoTC, each player controls a character. The character has a number of attributes, specifically, they have Strength, Dexterity, Stamina, Intellect, Wisdom, and Charisma – each which can have a score in the range of 1 to 20. These “ability” scores are used to determine the success and/or failure of skills used, as well as how your character performs in combat. We’ll explore more of that later. For this week, I want you to write a program which contains a “Character” class. The character should have attributes representing the 6 ability scores, with accesses for each score. As well, the class should contain two constructors, 1 which takes as input 6 ability scores, and the second which is the default. The default constructor should randomly assign each ability score a value in the range of 1 to 20. To make it easier, I’ve posted the code below to help generate random numbers. Your main function should create two “characters”….one by using the default constructor, and one by passing in values between 1-20…might want to check in your constructor to make sure the input is valid (1-20). After both characters are created, use your accessors to print the attributes of each character to the screen with appropriate labels.

// Required headers
#include <stdlib.h>
#include <time.h>

// Call this ONCE, in your main function to seed the random number generator
srand( (unsigned)time( NULL ) );

// Call this in your default constructor in order to generate a random number from 1 to 20.
int randomNum = ( rand() % 20 ) + 1;
[Edited by - jwalsh on July 13, 2006 9:01:14 PM]
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Advertisement
Thats a cool exercise Jeromy! It would be fun if we can build on top of this exercise in the next weeks, adding stuff as we learn new things.
I really did enjoy working on this week's project. I would love any feedback anyone might have regarding sloppiness on my part or any other comments in general:) Thank you again!


Chapter 6 Quiz

1. What was the ‘C’ capability which allowed you to combine related variables? Did this solve the problem of connecting data

with behaviors?
The C capability was called structures and no it did not.

2. How do you make a new type in C++?
You declare a class.

3. What is a class?
A composition of of related variables and functions.

4. What is another name for variables within a class? What is another name for the member functions?
Variables within a class are called members variables or data members. Functions within a class are called member functions

or methods.

5. What are the requisite parts of a class declaration? Show an example.
class Cat
{
int itsAge;
void meow();
};
The word 'class', the class name and brackets (with an ending semi-colon) are all required parts of the class declaration.

6. What are the two things a class declaration tells your compiler?
It tells the compiler what the class exactly is, and how much memory it needs to allocate for each object it creates of that

class.

7. When discussing naming conventions, what’s the most important point to remember?
Choose a naming convention and be consistent with it.

8. What is an object?
AN individual instance of a class.

9. How do you declare an object? Show an example.
Cat cupcake;
The above statement creates a cat object called cupcake.

10. What operator is used to access members and methods of a class for objects created on the stack.
The dot (.) operator.

11. What is the difference between public and private members/methods within a class? Which is the default?
Private members can only be accessed when processing is inside the actual object. Public members can be accessed from

anywhere in your program. Class members are private by default.

12. In general, should methods or members be private? Why?
Yes, because if anything has the ability to access your members or methods, it makes your program much more difficult to

debug. It also allows you to distinguis between how your data is stored and how it is used.

13. What is the primary way you initialize the member data of a class?
With a constructor.

14. How can you identify a constructor for a class?
By defining a member function of the same name of the class, IE
Cat::Cat (int itsAge)
{
itsAge=initialage;
}


15. What is a default constructor? How is it called.
If you don't specify your own constructor, the default constructor is automatically called. The default constructor has no

actual code in it, it simply initializes your object.

16. What does “declaring a method of a class const” mean? What is the syntax for such a declaration?
This means that the method being declared const will not manipulate member variables at all. An example would be:
void someFunction () const;

17. In the context of classes and objects, what is a client?
Anyone or anything that actually interfaces with those objects.

18. Where do class declarations go? Where do the member function definitions go?
Class declarations should go into .hpp files and be included into your program using #include. Member function

definitions. Member function definitions can go into any .cpp file that is also included in your program.

19. When one class has a member variable which is an object of a different class they are said to form a ___ relationship?
a has-a relationship.

20. What is the only difference between C++ classes and C++ structs?
Members in classes are private by default. In structs, they are public by default.



Chapter 11 Quiz

1. What’s the purpose of a model?
To create a meaningful abstraction of the real world.

2. What is the basic philosophy of iterative design/development?
You begin with an idea you want to build on, and as you investigate it, the idea evolves. You may end up going back to

previous stages of design/development based on what you find in later stages.

3. What’s the difference between waterfall vs. iterative development?
In waterfall development, you are incapable of going 'backward' to previous stages, in iterative development you can.

4. What are the 6 steps listed in your book for iterative development.
Conceptulaization, analysis, design, implementation, testing and rollout.
`
5. What happens if you lose sight of the vision of your product?
Chances are your product is doomed!

6. What is a use case?
A description of how the software will be used.

7. What is a domain expert?
An expert in the area of business in which you are designing the product.

8. What is an actor?
Any person or system that interacts with the product you are developing.

9. What are the 6 questions you should ask yourself about the actors when determining use cases, according to your book?
1. Why is the actor using this system?
2. What outcome does the actor want or expect from each request?
3. What happened to cause the actor to use this system now?
4. What must the actor do to use the system?
5. What information must the actor provide to the system?
6. What information does the actor hope to get from the system?

10. What is a domain model?
A document that captures everything you know about the domain.

11. What is a scenario?
A specific set of circumsances within an individual use case.

12. What are the 9 items, according to your book, should you include when documenting a scenario?
1. Preconditions
2. Triggers
3. What actions the actors take.
4. What results or changes are caused by the system.
5. What feedback the actors receive.
6. Whether repeating activites occur, and what causes them to conclude.
7. A description of the logical flow of the scenario.
8. What causes the scenario to end.
9. Postconditions

13. What is systems analysis?
The process of collecting information from all the other systems you plan on interacting with in your product.

14. What is the simplistic technique, according to your book, for determining the classes in your project?
Pulling out all the nouns in your use case scenarios.

15. What are the three areas of concern for the Static Model of your classes?
Responsibilities, attributes and relationships.

16. What is the most important principle when deciding the responsibilities of a class?
Each class should be responsible for one thing.

17. What are the two types of diagrams shown in the book for determining how classes interact?
Sequence disgrams and collaboration diagrams.


Exercise 1 – The random character generator

In the game “Dungeons & Dragons” by WoTC, each player controls a character. The character has a number of attributes, specifically, they have Strength, Dexterity, Stamina, Intellect, Wisdom, and Charisma – each which can have a score in the range of 1 to 20. These “ability” scores are used to determine the success and/or failure of skills used, as well as how your character performs in combat. We’ll explore more of that later. For this week, I want you to write a program which contains a “Character” class. The character should have attributes representing the 6 ability scores, with accesses for each score. As well, the class should contain two constructors, 1 which takes as input 6 ability scores, and the second which is the default. The default constructor should randomly assign each ability score a value in the range of 1 to 20. To make it easier, I’ve posted the code below to help generate random numbers. Your main function should create two “characters”….one by using the default constructor, and one by passing in values between 1-20…might want to check in your constructor to make sure the input is valid (1-20). After both characters are created, use your accessors to print the attributes of each character to the screen with appropriate labels.



#include <iostream>#include <stdlib.h>#include <time.h>using namespace std;class character //defining the character class{	public: //public accessor and modifier functions		int GetStrength() const;		void SetStrength (int strength);		int GetDexterity() const;		void SetDexterity (int dexterity);		int GetStamina() const;		void SetStamina (int stamina);		int GetIntellect() const;		void SetIntellect (int intellect);		int GetWisdom() const;		void SetWisdom (int wisdom);		int GetCharisma() const;		void SetCharisma (int charisma);		//construction and destructor declarations		character (int strength, int dexterity, int stamina, int intellect, int wisdom, int charisma);		character();		~character();	private: //private data members		int itsStrength;		int itsDexterity;		int itsStamina;		int itsIntellect;		int itsWisdom;		int itsCharisma;};//constructor definitionscharacter::character(int strength, int dexterity, int stamina, int intellect, int wisdom, int charisma){	//check input values to make sure they are valid. If not, we will print an error message.		if ((0 >= strength) || (strength > 20))		cout << "Error! Input value for strength was invalid!" << endl << endl;	if ((0 >= dexterity) || (dexterity > 20))		cout << "Error! Input value for dexterity was invalid!" << endl << endl;	if ((0 >= stamina) || (stamina > 20))		cout << "Error! Input value for stamina was invalid!" << endl << endl;	if ((0 >= intellect) || (intellect > 20))		cout << "Error! Input value for intellect was invalid!" << endl << endl;	if ((0 >= wisdom) || (wisdom > 20))		cout << "Error! Input value for wisdom was invalid!" << endl << endl;	if ((0 >= charisma) || (charisma > 20))		cout << "Error! Input value for charisma was invalid!" << endl << endl;		itsStrength = strength;	itsDexterity = dexterity;	itsStamina = stamina;	itsIntellect = intellect;	itsWisdom = wisdom;	itsCharisma = charisma;}character::character() //default constructor that takes in no values and assigns random values to all six attributes{	int rdmStrength = ( rand() % 20 ) + 1;	int rdmDexterity = ( rand() % 20 ) + 1;	int rdmStamina = ( rand() % 20 ) + 1;	int rdmIntellect = ( rand() % 20 ) + 1;	int rdmWisdom = ( rand() % 20 ) + 1;	int rdmCharisma = ( rand() % 20 ) + 1;	itsStrength = rdmStrength;	itsDexterity = rdmDexterity;	itsStamina = rdmStamina;	itsIntellect = rdmIntellect;	itsWisdom = rdmWisdom;	itsCharisma = rdmCharisma;}character::~character() //destructor, takes no action.{}//The following six member functions are the accessor functions for characterint character::GetStrength() const {	return itsStrength;}int character::GetDexterity() const{	return itsDexterity;}int character::GetStamina() const{	return itsStamina;}int character::GetIntellect() const{	return itsIntellect;}int character::GetWisdom() const{	return itsWisdom;}int character::GetCharisma() const{	return itsCharisma;}//The following six member definitons define the modifier functions for character classesvoid character::SetStrength(int strength){	itsStrength = strength;}void character::SetDexterity(int dexterity){	itsDexterity = dexterity;}void character::SetStamina(int stamina){	itsStamina = stamina;}void character::SetIntellect(int intellect){	itsIntellect = intellect;}void character::SetWisdom(int wisdom){	itsWisdom = wisdom;}void character::SetCharisma(int charisma){	itsCharisma = charisma;}int main(){	srand( (unsigned)time( NULL ) );	int inputStrength, inputDexterity, inputStamina, inputIntellect, inputWisdom, inputCharisma;	cout << "Creating Palejo..." << endl;	character Palejo;	cout << "Palejo has been created with random stats!" << endl;	cout << "You will now enter Reshad's stats. Please enter values between 1 and 20." << endl;	cout << "Enter Reshad's strength:" << endl;	cin >> inputStrength;	cout << "Enter Reshad's dexterity:" << endl;	cin >> inputDexterity;	cout << "Enter Reshad's stamina:" << endl;	cin >> inputStamina;	cout << "Enter Reshad's intellect:" << endl;	cin >> inputIntellect;	cout << "Enter Reshad's wisdom:" << endl;	cin >> inputWisdom;	cout << "Enter Reshad's charisma:" << endl;	cin >> inputCharisma;	cout << "Creating Reshad..." << endl << endl << endl;	character Reshad (inputStrength, inputDexterity, inputStamina, inputIntellect, inputWisdom, inputCharisma);		cout << "Here are Palejo's stats:" << endl;	cout << "strength:" << Palejo.GetStrength() << endl;	cout << "dexterity:" << Palejo.GetDexterity() << endl;	cout << "stamina:" << Palejo.GetStamina() << endl;	cout << "intellect:" << Palejo.GetIntellect() << endl;	cout << "wisdom:" << Palejo.GetWisdom() << endl;	cout << "charisma:" << Palejo.GetCharisma() << endl << endl << endl;	cout << "Here are Reshad's stats:" << endl;	cout << "strength:" << Reshad.GetStrength() << endl;	cout << "dexterity:" << Reshad.GetDexterity() << endl;	cout << "stamina:" << Reshad.GetStamina() << endl;	cout << "intellect:" << Reshad.GetIntellect() << endl;	cout << "wisdom:" << Reshad.GetWisdom() << endl;	cout << "charisma:" << Reshad.GetCharisma() << endl << endl << endl;	return 0;}


[Edited by - Palejo on July 16, 2006 1:50:36 PM]
I'll post the answers to Chapter 11 Quiz later (the lack of course book is a small hindrance on those).

Chapter 6 Quiz

1. What was the 'C' capability which allowed you to combine related variables?
Did this solve the problem of connecting data with behaviors?
Struct. No, C structs didn't have class methods.

2. How do you make a new type in C++?
class new_type { <class definition> };


3. What is a class?
A collection of related data and functionality.

4. What is another name for variables within a class? What is another name for
the member functions?
Data members. Methods.

5. What are the requisite parts of a class declaration? Show an example.
Keyword 'class' followed by the class name, and then the list of data
members and methods within a set of curly braces followed by a semicolon.

class Dog    {    unsigned short age;    unsigned short weight;    Bark();    }


6. What are the two things a class declaration tells your compiler?
What class is (its data members and methods), and how much memory is set
aside for each instance of the class.

7. When discussing naming conventions, what's the most important point to
remember?
Consistency within a single project.

8. What is an object?
An instance of a class.

9. How do you declare an object? Show an example.
Just like declaring any of the built-in types - type followed by the
object's name and semicolon.

Dog Blackie;


10. What operator is used to access members and methods of a class for objects
created on the stack.
Dot (.).

11. What is the difference between public and private members/methods within
a class? Which is the default?
Public means that they can be accessed from anywhere, while private allows
access from only within the class itself. Private is the default.

12. In general, should methods or members be private? Why?
Members should be private. It allows better encapsulation.

13. What is the primary way you initialize the member data of a class?
Through constuctor method.

14. How can you identify a constructor for a class?
Like any other method, except its name must be exactly the same as the
name of the class, and it has no return type (not even void).

15. What is a default constructor? How is it called.
Default constructor is an empty method. Class Object;

16. What does 'declaring a method of a class const' mean? What is the syntax
for such a declaration?
const methods can't change any of the data members.

return_value functionName() const;


17. In the context of classes and objects, what is a client?
A part of program that uses the class or object.

18. Where do class declarations go? Where do the member function definitions
go?
Declarations should be in separate header files. Method definitions should
be in the actual .cpp file.

19. When one class has a member variable which is an object of a different
class they are said to form a ___ relationship?
Has-a relationship.

20. What is the only difference between C++ classes and C++ structs?
In structs the data members and methods are public by default, while in
classes they're private.

EXERCISE 1:

character.h
#include <cstdlib>#include <iostream>// A Character class for something-like-D&D.class Character {public:	// Constructors - the default and one that takes all six attributes as 	// parameters.	Character();	Character(unsigned short str, unsigned short dex, unsigned short con,		      unsigned short itg, unsigned short wis, unsigned short cha);		// The "evil" getters and setters.	unsigned short getStrength() const;	void setStrength (unsigned short str);	unsigned short getDexterity() const;	void setDexterity (unsigned short dex);	unsigned short getConstitution() const;	void setConstitution (unsigned short con);	unsigned short getIntelligence() const;	void setIntelligence (unsigned short itg);	unsigned short getWisdom() const;	void setWisdom (unsigned short wis);	unsigned short getCharisma() const;	void setCharisma (unsigned short cha);	// Public printer method (should probably be returning string but we 	// haven't had strings yet).	void printCharacter() const;private:	// Private attribute variables (as in the actual D&D).	unsigned short Strength, Dexterity, Constitution,		           Intelligence, Wisdom, Charisma;		// Private helper method to do the randomizing.	void randomize();};


character.cpp
#include "character.h"Character::Character() {	randomize();}Character::Character(unsigned short str, unsigned short dex, unsigned short con, 					 unsigned short itg, unsigned short wis, unsigned short cha) {	if (str == 0 || str > 20 || dex == 0 || dex > 20 || con == 0 || con > 20 ||		itg == 0 || itg > 20 || wis == 0 || wis > 20 || cha == 0 || cha > 20) 	{		std::cout<<"Illegal attribute value(s)! Random character created!\n";		randomize();	}	else	{		Strength = str;		Dexterity = dex;		Constitution = con;		Intelligence = itg;		Wisdom = wis;		Charisma = cha;	}}unsigned short Character::getStrength() const{	return Strength;}void Character::setStrength(unsigned short str){	if (str >= 1 && str <= 20)		Strength = str;	else		std::cout<<"Illegal value for Stength\n";}unsigned short Character::getDexterity() const{	return Dexterity;}void Character::setDexterity(unsigned short dex){	if (dex >= 1 && dex <= 20)		Dexterity = dex;	else		std::cout<<"Illegal value for Dexterity\n";}unsigned short Character::getConstitution() const{	return Constitution;}void Character::setConstitution(unsigned short con){	if (con >= 1 && con <= 20)		Constitution = con;	else		std::cout<<"Illegal value for Constitution\n";}unsigned short Character::getIntelligence() const{	return Intelligence;}void Character::setIntelligence(unsigned short itg){	if (itg >= 1 && itg <= 20)		Intelligence = itg;	else		std::cout<<"Illegal value for Intelligence\n";}unsigned short Character::getWisdom() const{	return Wisdom;}void Character::setWisdom(unsigned short wis){	if (wis >= 1 && wis <= 20)		Wisdom = wis;	else		std::cout<<"Illegal value for Wisdom\n";}unsigned short Character::getCharisma() const{	return Charisma;}void Character::setCharisma(unsigned short cha){	if (cha >= 1 && cha <= 20)		Charisma = cha;	else		std::cout<<"Illegal value for Charisma\n";}void Character::printCharacter() const{	std::cout<<"\nStrength: \t"<<Strength<<	"\nDexterity: \t"<<Dexterity<<	"\nConstitution: \t"<<Constitution<<	"\nIntelligence: \t"<<Intelligence<<	"\nWisdom: \t"<<Wisdom<<	"\nCharisma: \t"<<Charisma<<std::endl;}void Character::randomize(){	Strength = (rand() % 20) + 1;	Dexterity =  (rand() % 20) + 1;	Constitution = (rand() % 20) + 1;	Intelligence = (rand() % 20) + 1;	Wisdom = (rand() % 20) + 1;	Charisma = (rand() % 20) + 1;}


main.cpp
#include <iostream>#include <ctime>#include "character.h"int main(){	srand((unsigned)time(NULL));	Character c1;	std::cout<<"Character #1 with printCharacter():";	c1.printCharacter();	Character c2 = Character (18, 15, 15, 12, 10, 13);	std::cout<<"\nCharacter #2 with getters: "<<		"\nStrength: \t"<<c2.getStrength()<<		"\nDexterity: \t"<<c2.getDexterity()<<		"\nConstitution: \t"<<c2.getConstitution()<<		"\nIntelligence: \t"<<c2.getIntelligence()<<		"\nWisdom: \t"<<c2.getWisdom()<<		"\nCharisma: \t"<<c2.getCharisma()<<std::endl;	return 0;}
This is my take on Exercise 1 – The random character generator

I know it gives the expected output but it would be nice if you could comment my syntax. Is my way of solving this problem good ? Have I picken up bad programming practice ?

Thanks

My header file:

#include <iostream>#include <stdlib.h>#include <time.h>// int randomNum = ( rand() % 20 ) + 1;class Character{	public:		Character(int Str, int Dex, int Con, int Int, int Wis, int Cha);		Character();		~Character();		int GetStr() const {return itsStr;}		int GetDex() const {return itsDex;}		int GetCon() const {return itsCon;}		int GetInt() const {return itsInt;}		int GetWis() const {return itsWis;}		int GetCha() const {return itsCha;}		void SetStr(int Str) {itsStr=Str;}		void SetDex(int Dex) {itsDex=Dex;}		void SetCon(int Con) {itsCon=Con;}		void SetInt(int Int) {itsInt=Int;}		void SetWis(int Wis) {itsWis=Wis;}		void SetCha(int Cha) {itsCha=Cha;}	private:	// Stands for Strength, Dexterity, Constitution, Intelligence, Wisdom, Charisma .	int itsStr, itsDex, itsCon, itsInt, itsWis, itsCha;};


My cpp file:

#include "Character.h"using namespace std;Character::Character(int Str, int Dex, int Con, int Int, int Wis, int Cha){	if	(Str<=20 && Str>=1 && Dex<=20 && Dex>=1 && Con<=20 && Con>=1 && Int<=20 && Int>=1 && Wis<=20 && Wis>=1 && Cha<= 20 && Cha>=1)	{		itsStr=Str;		itsDex=Dex;		itsCon=Con;		itsInt=Int;		itsWis=Wis;		itsCha=Cha;	}	else	{		itsStr=1;		itsDex=1;		itsCon=1;		itsInt=1;		itsWis=1;		itsCha=1;	}	}Character::Character(){	itsStr=( rand() % 20 ) + 1;	itsDex=( rand() % 20 ) + 1;	itsCon=( rand() % 20 ) + 1;	itsInt=( rand() % 20 ) + 1;	itsWis=( rand() % 20 ) + 1;	itsCha=( rand() % 20 ) + 1;	if	(itsStr>20 || itsStr<1 || itsDex>20 || itsDex<1 || itsCon>20 || itsCon<1 || itsInt>20 || itsInt<1 || itsWis>20 || itsWis<1 || itsCha>20 || itsCha<1)	{		itsStr=1;		itsDex=1;		itsCon=1;		itsInt=1;		itsWis=1;		itsCha=1;	}}Character::~Character(){}int main(){	srand( (unsigned)time( NULL ) );	Character Me(14,16,14,16,16,14);	Character You;	cout<<"**YOU**" <<endl <<"Strength: " <<You.GetStr() <<endl;	cout<<"Dexterity: " <<You.GetDex() <<endl <<"Constitution: " <<You.GetCon() <<endl;	cout<<"Intelligence: " <<You.GetInt() <<endl <<"Wisdom: " <<You.GetWis() <<endl;	cout<<"Charisma: " <<You.GetCha() <<endl <<endl;	cout<<"**ME**" <<endl <<"Strength: " <<Me.GetStr() <<endl;	cout<<"Dexterity: " <<Me.GetDex() <<endl <<"Constitution: " <<Me.GetCon() <<endl;	cout<<"Intelligence: " <<Me.GetInt() <<endl <<"Wisdom: " <<Me.GetWis() <<endl;	cout<<"Charisma: " <<Me.GetCha() <<endl <<endl;	cin.get();	return 0;}


[Edited by - Myotis on August 12, 2006 1:15:29 AM]
itsStr>20 && itsStr<1


This can never be true. Perhaps you mean || ?
Yes, sorry I replaced the && with ||
i've been following the workshop for the last couple of weeks and would like to say thanks to all those involved - its a great help.

i have a couple of questions about the character generating exercise.

is it necessary to include the Set accessors (eg void SetDexterity (int dexterity); ) or is this just good practice?

we have #include <iostream> in the header file so do we need to include it
in the main.cpp file as well?

thanks
dave
You guys are helping me beyond imagine with this workshop. I have read so many books to learn c++ and it was hard to take it all in, but with your help I've been able to understand it.

Here is my random character, if you could please check my syntax ty.

//Character.h
#include <cstdlib>#include <iostream>using namespace std;class character{public:	// Default and main character	character();	character(unsigned short strength, unsigned short dexterity, unsigned short stamina, unsigned short intelligence, unsigned short wisdom, unsigned short charisma);	~character();	//accessors    unsigned short getStrength() const;	unsigned short getDexterity()const;	unsigned short getStamina() const; 	unsigned short getIntelligence() const;	unsigned short getWisdom() const;	unsigned short getCharisma() const;	void setStrength(unsigned short strength);	void setDexterity(unsigned short dexterity);	void setStamina(unsigned short stamina);	void setIntelligence(unsigned short intelligence);	void setWisdom(unsigned short wisdom);	void setCharisma(unsigned short charisma);private:	unsigned short Strength, Dexterity, Stamina, Intelligence, Wisdom, Charisma;};


//Character.cpp
#include "Character.h"//Class definitionscharacter::character(){	int randomStrength = ( rand() % 20 ) + 1;	int randomDexterity = ( rand() % 20 ) + 1;	int randomStamina = ( rand() % 20 ) + 1;	int randomIntelligence = ( rand() % 20 ) + 1;	int randomWisdom = ( rand() % 20 ) + 1;	int randomCharisma = ( rand() % 20 ) + 1;	Strength = randomStrength;	Dexterity = randomDexterity;	Stamina = randomStamina;	Intelligence = randomIntelligence;	Wisdom = randomWisdom;	Charisma = randomCharisma;}character::character(unsigned short strength, unsigned short dexterity, unsigned short stamina, unsigned short intelligence, unsigned short wisdom, unsigned short charisma){	if ( strength == 0 || strength > 20 || dexterity == 0 || dexterity > 20 || stamina == 0 ||		stamina > 20 || intelligence == 0 || intelligence > 20 || wisdom == 0 || wisdom > 20 ||		charisma == 0 || charisma > 20 )	{		std::cout << "Illegal values set" << std::endl;	}	else		Strength = strength;		Dexterity = dexterity;		Stamina = stamina;		Intelligence = intelligence;		Wisdom = wisdom;	    Charisma = charisma;}character::~character(){}//Get accessors definitionsunsigned short character::getStrength() const{	return Strength;}unsigned short character::getDexterity() const{	return Dexterity;}unsigned short character::getStamina()const{	return Stamina;}unsigned short character::getIntelligence() const{	return Intelligence;}unsigned short character::getWisdom() const{	return Wisdom;}unsigned short character::getCharisma() const{	return Charisma;}//Set Accessorsvoid character::setStrength(unsigned short strength){	Strength = strength;}void character::setDexterity(unsigned short dexterity){	Dexterity = dexterity;}void character::setStamina(unsigned short stamina){	Stamina = stamina;}void character::setWisdom(unsigned short wisdom){	Wisdom = wisdom;}void character::setCharisma(unsigned short charisma){	Charisma = charisma;}


//Main.cpp
#include <iostream>#include <ctime>#include "Character.h"int main(){	//random generator	srand ((unsigned)time(NULL));		//Integors for input values	int userStrength, userDexterity, userStamina, userIntelligence, userWisdom, userCharisma;    //Create random character	std::cout << "Creating random character, please hold..." << std::endl;	character Human;	std::cout << "Random Human character created!" << std::endl;	//Get input from user	std::cout << "You will enter stats for our Elf character." << std::endl;	std::cout << "Please enter values between 1-20." << std::endl;	std::cout << "Enter Strength: " << std::endl;	std::cin >> userStrength;	std::cout << "Enter Dexterity: " << std::endl;	std::cin >> userDexterity;	std::cout << "Enter Stamina: " << std::endl;	std::cin >> userStamina;	std::cout << "Enter Intelligence: " << std::endl;	std::cin >> userIntelligence;	std::cout << "Enter Wisdom: " << std::endl;	std::cin >> userWisdom;	std::cout << "Enter Charisma: " << std::endl;	std::cin >> userCharisma;	//Input stats for user character	std::cout << "Inputting stats, please hold..." << std::endl;	character Elf(userStrength, userDexterity, userStamina, userIntelligence, userWisdom, userCharisma);	//Output random stats	std::cout << "Human's stats are: " << std::endl;	std::cout << "Strength: " << Human.getStrength() << std::endl;	std::cout << "Dexterity: " << Human.getDexterity() << std::endl;	std::cout << "Stamina: " << Human.getStamina() << std::endl;	std::cout << "Intelligence: " << Human.getIntelligence() << std::endl;	std::cout << "Wisdom: " << Human.getWisdom() << std::endl;	std::cout << "Charisma: " << Human.getCharisma() << std::endl << std::endl << std::endl;	//Output user stats	std::cout << "Now outputting Elf's stats that you entered: " << std::endl;	std::cout << "Strength: " << Elf.getStrength() << std::endl;	std::cout << "Dexterity: " << Elf.getDexterity() << std::endl;	std::cout << "Stamina: " << Elf.getStamina() << std::endl;	std::cout << "Intelligence: " << Elf.getIntelligence() << std::endl;	std::cout << "Wisdom: " << Elf.getWisdom() << std::endl;	std::cout << "Charisma: " << Elf.getCharisma() << std::endl;	//end file	system("PAUSE");	return 0;}
Where can I find your answers to the Quiz/Exercise of these chapters jwalsh?

This topic is closed to new replies.

Advertisement