Page 1 of 2

General C++ OOP Problem

PostPosted: Mar 14, 2005 @ 6:40pm
by fzammetti

PostPosted: Mar 14, 2005 @ 6:46pm
by Structure

PostPosted: Mar 14, 2005 @ 6:58pm
by fzammetti

PostPosted: Mar 14, 2005 @ 8:10pm
by dan.p

PostPosted: Mar 14, 2005 @ 8:14pm
by fzammetti

PostPosted: Mar 14, 2005 @ 8:18pm
by Structure

PostPosted: Mar 14, 2005 @ 8:21pm
by fzammetti

PostPosted: Mar 14, 2005 @ 8:57pm
by Structure
As im here,

Yes its good coding practice for future extendibility, only leave it out if you have a good reason why a possible extended class should never override the functionality

PostPosted: Mar 14, 2005 @ 9:08pm
by fzammetti
That's what I figured. Kind of frustrating sometimes... I'm quite familiar with OOP techniques, but C++ is just different enough sometimes from Java to make C++ confusing when I'm doing Java all day :) Thanks again!

PostPosted: Mar 14, 2005 @ 9:12pm
by fzammetti
Oops, spoke to soon... I guess I don't fully understand the need for declaring the virtual method in the derived class (I understand the REASON for it, but not the NEED)...

If class A has a virtual method in it (defined with an empty method body), and class B extends A but does NOT implement that method, and then class C extends class B, wouldn't class C still be able to implement that method by virtue of it's parent class (B) extending class A?

It sounds like you guys are saying the inheritance of a virtual method ends with any class that doesn't implement it or define it as virtual itself, i.e., is lost to any derived classes after that down the hierarchy. Is that the case?

PostPosted: Mar 14, 2005 @ 9:51pm
by dan.p
I've only been coding in C++ for three months, but there's one thing I've learned the hard way:

Always apply good coding practices when programming in C or C++, otherwise you're in for trouble later on. C/C++ does whatever you tell it, even if it's blatantly fubard code.

Examples:

FLOAT fX = 1.25;
DWORD dwX = (DWORD) fX;

This one caused me quite a few problems and took me a while to figure out

#define FunMacro(x) x / 2;

// Somewhere in your code..
FunMacro(5 + 233);

Yet one reason why macros should be avoided, because errors are so easy to make with them if they aren't properly handled

So many things can happen, it's endless. Be careful. ;)

As for your particular virtual fun, I do believe the class inherited from must have virtual in front of the function if you want polymorphism to take effect. If you don't define a function as virtual in one of the derived classes, then the buck stops there, and fun things begin to happen. :P

PostPosted: Mar 14, 2005 @ 10:29pm
by Structure

PostPosted: Mar 14, 2005 @ 10:36pm
by fzammetti

PostPosted: Mar 14, 2005 @ 10:39pm
by dan.p
EDIT:
Quoted you wrong. Rephrasing what I said.

I don't find it silly to declare derived class functions virtual. If you think about it, by declaring a derived classes function virtual, you're saying "this function overrides the function in the derived class."

When programming you always want to be clear with what you're doing, even if it makes no difference if you left out a keyword or not. After all, we are dealing with a language, and you want to say what you're doing clearly. I'm reminded of a quote.. not sure exactly what it was, but something along the lines of "good code comments itself." I think that'd apply here.

Also, every compiler is different. Some compilers may treat virtual slightly different, and require it to be there. Although I find it unlikely, but you never know.

PostPosted: Mar 14, 2005 @ 10:43pm
by mlepage