🎉 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 - OO Analysis, Design, & Porgramming (Ch. 6 & Ch. 11)

Started by
75 comments, last by Dbproguy 16 years, 1 month ago
ChurchSkiz thanks for the info, it makes perfect sense now. Cheers.

- Scattered.
Advertisement
Warning: I am not a tutor.

[Advanced]There is a fairly substantial difference between functions and class methods in C++. This is because a method is a function with an implicit extra parameter: a pointer to the object. So, in a way this:

class Dog{  unsigned int age;  unsigned int weight;public:  int getAge();  int getWeight();  // etc...};int Dog::getAge(){  return age;}int Dog::getWeight(){  return weight;}


Can, in a way, be interpreted as this:
struct Dog{  unsigned int age;  unsigned int weight;};int Dog__getAge(Dog * dog){  return dog->age;}int Dog__getWeight(Dog * dog){  return dog->weight;}


In fact, a lot of plain C code will end up emulating that type of behavior. You'll have a struct and a set of functions designed to operate on that struct. The most noticeable difference between regular functions and member functions is most apparent when dealing with function pointers which you can read about here:

http://www.newty.de/fpt/index.html

vs. pointers to member functions which you can read about here:

http://www.parashift.com/c++-faq-lite/pointers-to-members.html
[/Advanced]

Hope this is all correct and helps!

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Quote: Original post by Jeromy Walsh
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.


I was working on the exercise and I have everything working except I am still a little confused on the constructor / destructor. Can someone explain it a little better?



Sshado,

What is it your confused about exactly? The purpose of a constructor/destructor? Or what should be in the constructor/destructor for the exercise you referenced?

If you're confused about what a constructor/destructor is for, re-read the chapter. If you're confused about what goes in the constructor, then post what you *think* goes in, and we'll help you as best we can after we see the direction you're going.

Cheers!
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
Hi sorry to revive an old post but luckily I found out a workshop had been created on the book and compiler I am using. I was wondering if Mr. Walsh would be so kind as to answer a question if you have the time?

Well here it is.

On listing 6.9 are their any typos in the book? I have errors while compiling. Specifically "...must have class/struct/ union" i have 3 of those errors and 2 errors saying "MyRectangle is an undeclared identifier" I'm sure I can figure out the latter 2 errrs if I look over the code long enough as I get these often and I'm usually able to fix them.


Thanks in Advance.
Could you post the code and the error messages?
I don't have the book but can check you code nonetheless

minus
I got through chapter 6 okay, I forgot the semi-colon after the class declaration in the exercises though, which caused 4 errors, none of which I understood because they didn't really relate to what the problem really was.

Anyways I figured it out eventually by checking around on msdn then taking a second and third-look at some code demonstrated in the book.

I hope you all have fun now!
Dbproguy/Marcus


**ADDED TO POST**

I finished chapter 11 as well, this wasn't a sit at the computer thing really it was just read late at night before I went to bed.

Have fun now.

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

This topic is closed to new replies.

Advertisement