🎉 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, 2 months ago
Let me just say that I'm pleasantly surprised that chapter 11 even exists in this text. So many books just teach the syntax, but chapter 11 gets into best practice methodologies that are just as important to learn as the programming language employed in a project. I use many of these methodologies/practices in my day job (business software), so it's good to see them being emphasized in the workshop.

Props to Jeromy for bundling chapters six and eleven together. Good quiz questions and exercise too!

(oh, btw... are we supposed to submit/post our answers anywhere, or is this a self-graded affair?)

---

Additionally, I have one question with regard to the Exercise...

I noticed that if I tried to instantiate a Character like so:

  Character elf();


...the compiler barfed when I tried to build. However, when I removed the parens it built fine:

  Character elf;


I was a little surprised, actually -- what's going on there? I'm coming from a C# background where I'm used to doing the former construct.

Thanks!

[Edited by - recombinant on August 25, 2006 12:30:25 AM]
evan k. stone | recombinant---------------------------------------aspiring game + audio programmersf bay area, ca, usa
Advertisement
The problem with 'Character elf();' is that it's not declaring a variable named elf of type Character. It's declaring a function named elf that returns a Character. In C++, anything that can be a function declaration is a function declaration. For that reason, 'Character elf;' is equivilant to what you intended with 'Character elf();'
The usual way to call a non-default constructor is 'Character elf = Character(parameters);'. This will construct a Character, then copy-construct elf. The compiler should optimize the temporary away. Just 'Character elf(parameters);' usually works as well - as long as the compiler can't mistake it for a function declaration.
Thanks for the explanation... and I just wanted to clarify that I was mistaken in my comparison of "Character elf()" in C#, since what I'm used to is actually closer to using the new operator in C++... my bad. :P

Anyway, thanks again for the clarification!
evan k. stone | recombinant---------------------------------------aspiring game + audio programmersf bay area, ca, usa
[opinion]
Since you mentioned new, though you may already know this, I'll say it for the benefit of anyone else that may be reading just to clarify.

If you are going to be passing an object, it is always better to use a pointer instead of an actual object.

For example, if you had an addCharacter function, and you want to pass it a character to add, you should pass it as a pointer. The reason is, if you try to pass it as the object itself:

int addCharacter(Character myChar)
{
...
}

Character bob = Character();
addCharacter(bob);

You are wasting space because when you send bob, it is actually being copied, so you know have two copies of bob, taking up twice as much room as necessary.

If you create the function to take a pointer to a character:

int addCharacter(Character * myChar)
{
...
}

Character bob = new Character();
addCharacter(bob);

You save space because instead of sending a copy of bob to the function, you are only sending the memory address of bob. Not only does it save space, but it lets you modify bob from either the bob you created, or the one you sent to the function.

So, let's say that when you call addCharacter() it adds the passed character into a vector. Then, you have a getCharacter function which allows you to receive the character. We will assume it is:

Character * getCharacter(int index)
{
...
}

Then you can call something like getCharacter(0) (we will assume bob is at index 0 because he was the first one we sent.) You could call getCharacter(0)->setName("George") to change the name of the character. Then, the character stored by the addCharacter() function, and the one reference by bob, both get their name changed to George because they are actually the same object.

A basic rule of thumb I use is that if it isn't a standard data type (int, char, double, float, bool) or a string (unless the string is typically long), pass it as a pointer.

It'll save a lot of memory, and often, a lot of hassle.
[/opinion]

[Edited by - samanime on August 27, 2006 4:43:49 PM]
First; it would have been appropriate to use opinion tags around your entire post.

Quote:
If you are going to be passing an object, it is always better to use a pointer instead of an actual object.

'Always' is much too strong a word. Prefer to pass by const&. When that is not possible, it may still not be appropriate to pass by pointer just because it's a class. Consider all the factors including the cost of copying the class, the cost of the extra layer of indirection, and the usage of the parameter in the function before you blindly banish pass-by-value. The size of the object is only indirectly important through the cost of copying the object. Remember, allocating stack space is 'free'.
Hi everyone.
I found this foroum and i am trying to catch up.
I have reached 5th chapter and i encoutered a problem with const.
When i right this the programm work fine
***************************
class Cat
{
public:
Cat(int initialAge);
~Cat();
int GetAge();
void SetAge(int age);
void Miaouh() ;

private:
int MyAge;
};
****************************
but when i add const like this
void Miaouh() const;
i get this
error C2511: 'void Cat::Miaouh(void)' : overloaded member function not found in 'Cat'

Any ideas?

By the way you workshop rocks :)
When you declare a function const, you have to specify the const in the definition too. 'void foo() const' and 'void foo()' are two different functions. When you write the implementation of the function, you have to specify which you mean.

From your error, I can deduct that you added the const to the header, and not to the CPP file.

Also, it's spelled 'meow'.
Thanks for the quick answer. I can't believe i missed that.

And where i am, we spell it miaouh. :)
Hi everyone, I've been reading the recommended text (online version, rev2) and have been stumped on some code.

While google-ing to find the answer I stumbled across this workshop and thought it would be the ideal place to get assistance (and provide assistance) while learning this complex language.

Although I'm reading rev2 of the text, I hope my question is still relevant...

In listing 6.3 - Implementing the methods of a simple class, there is a strange call I can't get my head around.

(I wont post the whole code to save space, but if it's required I'm more than happy to do so)

6:   class Cat                   // begin declaration of the class7:   {8:     public:                   // begin public section9:       int GetAge();           // accessor function10:      void SetAge (int age);  // accessor function11:      void Meow();            // general function12:    private:                  // begin private section13:      int itsAge;             // member variable14:  };15:16:  // GetAge, Public accessor function17:  // returns value of itsAge member18:  int Cat::GetAge()19:  {20:     return itsAge;21:  }


How is it possible to return "itsAge" on line 20 when no information has been passed to the function? I thought that when working with functions you needed to pass anything required in between the ()'s (like "int age" on line 10).

I guess my question, how does "int Cat::GetAge()" get information from the class declaration (int itsAge) without saying "int Cat::GetAge(int itsAge)"?

Any help clarifying this would be greatly appreciated.

- Scattered
Quote: Original post by Scattered
Hi everyone, I've been reading the recommended text (online version, rev2) and have been stumped on some code.

While google-ing to find the answer I stumbled across this workshop and thought it would be the ideal place to get assistance (and provide assistance) while learning this complex language.

Although I'm reading rev2 of the text, I hope my question is still relevant...

In listing 6.3 - Implementing the methods of a simple class, there is a strange call I can't get my head around.

(I wont post the whole code to save space, but if it's required I'm more than happy to do so)

*** Source Snippet Removed ***

How is it possible to return "itsAge" on line 20 when no information has been passed to the function? I thought that when working with functions you needed to pass anything required in between the ()'s (like "int age" on line 10).

I guess my question, how does "int Cat::GetAge()" get information from the class declaration (int itsAge) without saying "int Cat::GetAge(int itsAge)"?

Any help clarifying this would be greatly appreciated.

- Scattered


itsAge is a private member of Cat. Any function of a class receives all the private and public members of that class. So for example:

class Hotdog{public:    void DisplayStuff();    int itsHotness;private:    int itsDogness;};


When defining Hotdog::DisplayStuff(), the DisplayStuff function automatically has access to itsHotness and itsDogness. It doesn't need to be passed as a parameter because it is part of the class. However, if you did something like this:

void HotDog::DisplayStuff(int Bun){    itsDogness=Bun;    return itsDogness;}


Then you would need to pass a parameter because you're using a variable that does not exist inside the class.

It's important to note that if itsDogness or itsHotness was not set to a value inside the constructor or another member function, that it's value could be anything. The function just returns the value no matter what it is, it does not set the value.

This topic is closed to new replies.

Advertisement