🎉 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, 2 months ago
Quote: Original post by J0Be
Where can I find your answers to the Quiz/Exercise of these chapters jwalsh?


I second that question. I am having to go back and reread Chapter 6 myself, got a little confused on some of the information. I should also go back and reread Chapter 5 I think, because I believe that is what is causing my confusion. First I read about function prototypes and defining the functions, thought I grasped that. After reading this chapter however, I think I am confusing declaring classes vs. functions (I suppose I mean the structure/syntax of each).

Anyhow, being able to compare the "correct" answers to my answers would be helpful. As well as, where can I fina a "correct" implementation of the Random Character Generator? I know there are lots of different ways to do it, I would just like to see how one of the tutors had coded it. Thanks for the workshop, if it hadnt been for my college, I would have been on board since the beginning!!!

Shawn

Advertisement
Chapter 6.

1) What was the 'C' capability which allowed you to combine related variables? Did this solve the problem of connection data with behaviors?
A) That capability was a struct, which could be made available as a new data type through the use of typedef statement. It didn't solve the problem of connecting data with behaviors because struct's' and the functions that operate on them aren't cohesive wholes; functions can only be found by reading the header files for the libraries available and looking for those with the new type as parameter.

2) How do you make a new type in C++?
A) You make a new type in C++ by declaring a class.

3) What is a class?
A) A class is a collection of variables--ofter of different types--combined with a set of related functions.

4) What is another name for variables within a class? What is another name for the member functions?
A) Another name for a variable in a class is a 'data member', a member function can be called a 'method'.

5) What are the requisite parts of a class declaration? Show an example.
A) To declare the class, use the class keyword followed by the class name, an opening brace, and then list of the data member and methods of that class. End the declaration with a closing brace and a semicolon.
Example: class myClass
{
public:
int myInt;
std::string myString;

void myFunction();
};

6) What are the two things a class declaration tells your compiler?
A) It tells the compiler how big it is, without reserving any memory yet, and what data members and functions it has declared.

7) When discussing naming conventions, what's the most important point to remember?
A) That you as a programmer MUST name all your member variables, member functions and classes.

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

9) How do you declare an object? Show an example.
A) You write the name of the class which is then followed by the name of the class object. If my class would be called myGame, then I could declare the object as follows: myGame TotalWar;

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

11) What is the difference between public and private members/methods within a class? Wich is default?
A) Private members can be accessed only within methods of the class itself. Public members can be accessed through any object of the class. All members of a class are private by default.

12) In general, should methods or members be private? Why?
A) Members should be kept private while in general, methods are public, the main reason is to keep your progam easy to maintain. It gives your code a longer life because design changes don't make your program obsolete.

13) What is the primary way you initialize the member data of a class.
A) By using a constructor.

14) How can you identify a constructor far a class?
A) It has the same name as the class and has no return value, not even void.

15) What is a default constructor? How is it called?
A) A default constructor is a constructor without parameters and is called by the compiler automatically if you haven't created one of your own.

16) What does "declaring a method of a class const" mean? What is the syntax for such a declaration?
A) It means that you promise not to change the member function during it's call. The syntax looks like this for example: int myFunc() const;

17) In the context of classes and objects, what is a client?
A) Clients are the parts of the program that create and use objects of your class.

18) Where do class declarations go? Wher do the member function definitions go?
A) Declarations go intoo an .h or .hpp file, member definitions go intoo a .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?
A) A "has-a" relationship.

20) What is the only difference between C++ classes and C++ structs?
A) Class members and methods are by default private, with structs, they're public by default.

Chapter 11.

1) What's the purpose of a model?
A) The purpose is to create a meaningfull abstraction of the real world in a simplified manner but accuratly enough so that it can be used to predict the behaviour of things in the real world.

2) What is the basic philosophy of iterative design/development?
A) The basic philosophy is to start with a concept; an idea of what you might want to buld and develop it further step by step and iterate back to a previous step(s) to expand the design.

3) What's the difference between waterfall vs.iterative development?
A) In waterfall development, the output from one stage becomes the input to the next, problem with this is, you can't go a step back. With iterative development, you start with an idea and build upon that idea by iterating threw several stages and going back to a previous stage(s) when needed.

4) What are the 6 steps listed in your book for iterative development?
A) 1) Conceptualization.
2) Analysis.
3) Design.
4) Implementation.
5) Testing
6) Rollout.

5) What happens if you lose sight of the vision of your product?
A) Then, almost surely, your project is doomed.

6) What is a use case?
A) It is a high-level description of how the product will be used. Use cases drive not only the analysis, but they also drive the design, they help you determine the classes, and they are especially important in testing the product.

7) What is a domain expert?
A) A domain expert is someone with expertise in the domain(area) of business for which you are creating the product.

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

9) What are the 6 questions you should ask yourself about the actors when determining use cases, according to your book?
A) 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 sytem 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) A domain model is a document that captures all you know about the domain(the field of business you are working in).

11) What is a scenario?
A) A scenario is a description of a specific set of circumstances that distinguish amoung the various elements of the use case.

12) What are the nine items, according to your book, should you include when documenting a scenario?
A) 1. Preconditions - What must be true for the scenario to begin.
2. Triggers - What even causes the scenario to begin.
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 activities 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 - What must be true when the scenario is complete.

13) What is system analysis?
A) System analysis is the process of collecting all the details for the systems with which you will interact.

14) What is the simplistic technique, according to your book, for determining the classes in your project?
A) The simplified technique is by writing out the use-case scenarios and then creating a class for every noun.

15) What are the three areas of concern for the Static Model of your classes?
A) 1. Responsibilities - each class should be responsible for one thing.
2. Attributes .
3. Relationships.

16) What is the most important principle when deciding the responsibilities of a class?
A) That each class must have one and only one responsibility.

17) What are the two types of diagrams shown in the book for determining how classes interact?
A) 1. Sequence diagram.
2. Collaboration diagram.

// Character.h file#include <iostream>class Character{public:	Character();	Character(int strength, int dexterity, int stamina, int intellect, int wisdom, int charisma);	~Character();	int GetStrength() const;	int GetDexterity() const;	int GetStamina() const;	int GetIntellect() const;	int GetWisdom() const;	int GetCharisma() const;	int ValidInput(int value);		// method will check whether value is within it's min- and maximum range.	int RandomNumGen();				// will generate a number between the min- and maximum range.	void PrintValues() const;	private:	int m_Strength;	int m_Dexterity;	int m_Stamina;	int m_Intellect;	int m_Wisdom;	int m_Charisma;	const static int m_MAX = 20;	const static int m_MIN = 0;};


// Character.cpp-file.#include <cstdlib>#include "Character.h"Character::Character(){	m_Strength = RandomNumGen();		m_Dexterity = RandomNumGen();		m_Stamina = RandomNumGen();		m_Intellect = RandomNumGen();		m_Wisdom = RandomNumGen();		m_Charisma = RandomNumGen();}Character::Character(int strength, int dexterity, int stamina, int intellect, int wisdom, int charisma){		m_Strength = ValidInput(strength);	m_Dexterity = ValidInput(dexterity);	m_Stamina = ValidInput(stamina);	m_Intellect = ValidInput(intellect);	m_Wisdom = ValidInput(wisdom);	m_Charisma = ValidInput(charisma);}Character::~Character() {}int Character::GetStrength() const{	return m_Strength;}int Character::GetDexterity() const{	return m_Dexterity;}int Character::GetStamina() const{	return m_Stamina;}int Character::GetIntellect() const{	return m_Intellect;}int Character::GetWisdom() const{	return m_Wisdom;}int Character::GetCharisma() const{	return m_Charisma;}int Character::ValidInput(int value){	if(value > m_MIN && value < m_MAX)		return value;	else		return (0);}int Character::RandomNumGen(){	int randomNum = ( rand() % m_MAX ) + 1;	return randomNum;}void Character::PrintValues() const{	std::cout << "Strength:\t " << m_Strength << " Points." << std::endl		<< "Dexterity:\t " << m_Dexterity << " Points." << std::endl		<< "Stamina:\t " << m_Stamina << " Points." << std::endl		<< "Intellect:\t " << m_Intellect << " Points." << std::endl		<< "Wisdom:\t\t " << m_Wisdom << " Points." << std::endl		<< "Charisma:\t " << m_Charisma << " Points." << std::endl;}


// Exampl()e(s) and exercise(s) from the book: Sams Teach Yourself// Authors: Jesse Liberty and Bradley Jones// Combination with the online tutorial classes from Jeromy Walsh on Gamedev.// Required headers#include <ctime>#include "Character.h"int main(){	// Call this ONCE, in your main function to seed the random number generator	srand( (unsigned)time( NULL ) );	Character Tom(15, 13, 14, 13, 34, 5);	Character Johan;	std::cout << "The values for Tom are: \n";	Tom.PrintValues();	std::cout << "\n\nThe values for Johan are:\n" 		<< "Strength:\t " << Johan.GetStrength() << " Points." << std::endl		<< "Dexterity:\t " << Johan.GetDexterity() << " Points." << std::endl		<< "Stamina:\t " << Johan.GetStamina() << " Points." << std::endl		<< "Intellect:\t " << Johan.GetIntellect() << " Points." << std::endl		<< "Wisdom:\t\t " << Johan.GetWisdom() << " Points." << std::endl		<< "Charisma:\t " << Johan.GetCharisma() << " Points." << std::endl;	return 0;}
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?
Structures, it didn't solve the problem though.

2. How do you make a new type in C++?
making the class declaration, class myClass { }

3. What is a class?
It is a type, it combines several variables, functions and types to make one object.

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

5. What are the requisite parts of a class declaration? Show an example.
The name, opening brace the data members and methods then a closing brace and a semi-colon after the closing brace.

6. What are the two things a class declaration tells your compiler?
It tells the compiler what the class is and what it can do (example, the class Cat, it tells the compiler what a cat is and what it can do)

7. When discussing naming conventions, what’s the most important point to remember?
To use one easy-to-remember naming convention so while you code you aren't try ing to figure out what the case is of a certain variable, etc.

8. What is an object?
A variable, the type being the class.

9. How do you declare an object? Show an example.
Just as you declare a variable, if the class was named cat,
Cat Frisky;

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

11. What is the difference between public and private members/methods within a class? Which is the default?
Private data members and methods can only be access from other methods within the class. Public data members and methods can be access from anywhere in the program, unless public or private is specified it's always private by default.

12. In general, should methods or members be private? Why?
Methods should generally be public and data members private.

13. What is the primary way you initialize the member data of a class?
Setting a constructor, and then assigning values to the data members in the constructor.

14. How can you identify a constructor for a class?
Declare it in the class declaration just with the class name and no type, with the parameters listed as usual in a function. Then you can make it a function basically and all that is within the function will happen when the class is defined.

15. What is a default constructor? How is it called.
A constructor that does nothing, called just as Cat(); in the example of the class cat

16. What does “declaring a method of a class const” mean? What is the syntax for such a declaration?
That promises that the method will not change any values of the class.

17. In the context of classes and objects, what is a client?
The function which calls the class/object.

18. Where do class declarations go? Where do the member function definitions go?
The declarations should be at the beginning, or in a header file. the function definitions should go in the main source file.

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?
They could not have methods/functions.
--Dbproguy - My Blog - Tips, Opinions and Reviews about C++, Video Games, and Life
Chapter 11 Quiz

1. What’s the purpose of a model?
To know what you are doing when making a program.

2. What is the basic philosophy of iterative design/development?
Starting with an idea and expanding on it.

3. What’s the difference between waterfall vs. iterative development?
The waterfall method only features the 4 categories, analysis design implmentation and test, but you cannot go in reverse order and it isn't so great. iterative development features a full 6 steps (including conceptualization where you start with a small idea)

4. What are the 6 steps listed in your book for iterative development.
Conceptualization, analysis, design, implementation, testing and rollout

5. What happens if you lose sight of the vision of your product?
Your product is doomed

6. What is a use case?
A description of the different possible uses of the product.

7. What is a domain expert?
Someone who deals with the domains of the product and knows how to do it well.

8. What is an actor?
The external objects which will use the product, like customers and bankers for an atm.

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

10. What is a domain model?
A document that captures all needed knowledge about the domain.

11. What is a scenario?
Something that can happen, all possible scenario's must be found and dealt with.

12. What are the 9 items, according to your book, should you include when documenting a scenario?
Preconditions, Triggers, Actions, Results, Feedback received, Looping and what causes the loop to end, Logical flow of the scenario, What makes the scenario end, Postconditions

13. What is systems analysis?
The process of collectiong details which will ineract with the product

14. What is the simplistic technique, according to your book, for determining the classes in your project?
Write out the use-case scenarios then make a class for every noun.

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?
Understanding what each class does.

17. What are the two types of diagrams shown in the book for determining how classes interact?
Aggregation and Composition.

#include <stdlib.h>		//Header - required for the randomization#include <time.h>		//Header - required to get the time for the seed#include <iostream>		//The regular in out stream headerusing std::cout;using std::endl;		//Using the cout, endl and cin functions of the std libraryusing std::cin;class Character{public:	//constructors/destructor	Character();	Character (int str, int dex, int sta, int intel, int wis, int cha);	~Character(){}	//accessor functions Get	int GetStrength() { return itsStrength; }	int GetDexterity() { return itsDexterity; }	int GetStamina() { return itsStamina; }	int GetIntellect() { return itsIntellect; }	int GetWisdom() { return itsWisdom; }	int GetCharisma() { return itsCharisma; }	//accessor functions Set	void SetStrength(int str) { itsStrength = str; }	void SetDexterity(int dex) { itsDexterity = dex; }	void SetStamina(int sta) { itsStamina = sta; }	void SetIntellect(int intel) { itsIntellect = intel; }	void SetWisdom(int wis) { itsWisdom = wis; }	void SetCharisma(int cha) { itsCharisma = cha; }private:	int itsStrength;	int itsDexterity;	int itsStamina;	int itsIntellect;	int itsWisdom;	int itsCharisma;};//Random number functionint RandNum(int range, int start){	int randNum = ( rand() % range ) + start;	return randNum;}//The constructor, which adds a value to the variables by the given random numbers.Character::Character(){	itsStrength = RandNum(20,1);	itsDexterity = RandNum(20,1);	itsStamina = RandNum(20,1);	itsIntellect = RandNum (20,1);	itsWisdom = RandNum (20,1);	itsCharisma = RandNum (20,0);}//The overloaded constructor, initializing all the variablesCharacter::Character(int str, int dex, int sta, int intel, int wis, int cha){	if ((str >=1) && (str <= 20))	{		itsStrength = str;	}	else	{		cout << "Invalid Strength. Setting to a random number." << endl;		itsStrength = RandNum(20,1);	}	if ((dex >=1) && (str <= 20))	{		itsDexterity = dex;	}	else	{		cout << "Invalid Dexterity. Setting to a random number." << endl;		itsDexterity = RandNum(20,1);	}	if ((sta >=1) && (sta <= 20))	{		itsStamina = sta;	}	else	{		cout << "Invalid Stamina. Setting to a random number." << endl;		itsStamina = RandNum(20,1);	}	if ((intel >=1) && (intel <= 20))	{		itsIntellect = intel;	}	else	{		cout << "Invalid Intellect. Setting to a random number." << endl;		itsIntellect = RandNum(20,1);	}	if ((wis >=1) && (wis <= 20))	{		itsWisdom = wis;	}	else	{		cout << "Invalid Wisdom. Setting to a random number." << endl;		itsWisdom = RandNum(20,1);	}	if ((cha >=1) && (cha <= 20))	{		itsCharisma = cha;	}	else	{		cout << "Invalid Charisma. Setting to a random number." << endl;		itsCharisma = RandNum(20,1);	}}//Main functionint main(){	srand( (unsigned)time( NULL ) );	cout << "Welcome to the Random Character Generator" << endl;	cout << "   by Marcus Johnson" << endl << endl;	cout << "I will start off by creating a player named Bob with random stats." << endl;	cout << "Press enter to continue" << endl;	cin.get();	Character Bob;	cout << "Bob:" << endl << endl;	cout << "Strength: " << Bob.GetStrength() << endl;	cout << "Dexterity: " << Bob.GetDexterity() << endl;	cout << "Stamina: " << Bob.GetStamina() << endl;	cout << "Intellect: " << Bob.GetIntellect() << endl;	cout << "Wisdom: " << Bob.GetWisdom() << endl;	cout << "Charisma: " << Bob.GetCharisma() << endl << endl;	int strength, dexterity, stamina, intellect, wisdom, charisma;	cout << "Now you get to create the stats of Joe." << endl;	cout << "Strength: ";	cin >> strength;	cout << "Dexterity: ";	cin >> dexterity;	cout << "Stamina: ";	cin >> stamina;	cout << "Intellect: ";	cin >> intellect;	cout << "Wisdom: ";	cin >> wisdom;	cout << "Charisma: ";	cin >> charisma;	cout << endl;	Character Joe(strength, dexterity, stamina, intellect, wisdom, charisma);	cout << endl << endl << "Here's the stats of Bob and Joe." << endl;	cout << "Bob:" << "\t\tJoe:" << endl << endl;	cout << "Strength: " << Bob.GetStrength() << "\tStrength: " << Joe.GetStrength() << endl;	cout << "Dexterity: " << Bob.GetDexterity() << "\tDexterity: " << Joe.GetDexterity() << endl;	cout << "Stamina: " << Bob.GetStamina() << "\tStamina: " << Joe.GetStamina() << endl;	cout << "Intellect: " << Bob.GetIntellect() << "\tIntellect: " << Joe.GetIntellect() << endl;	cout << "Wisdom: " << Bob.GetWisdom() << "\tWisdom: " << Joe.GetWisdom() << endl;	cout << "Charisma: " << Bob.GetCharisma() << "\tCharisma: " << Joe.GetCharisma() << endl << endl;	cout << "Thanks for trying this.  Bye." << endl;}


[Edited by - Dbproguy on May 23, 2008 5:53:15 PM]
--Dbproguy - My Blog - Tips, Opinions and Reviews about C++, Video Games, and Life

This topic is closed to new replies.

Advertisement