Header files .. Best programming practice? and class question

Started by
5 comments, last by Qw3r7yU10p! 19 years, 10 months ago
Hi I just want to know what would the best programming practice be. I have: initd3d.cpp //Not a class but might convert it later on initd3d.h splash.cpp // A class splash.h Now both pairs need the Direct3d9 (d3dx.h) device. What would the best use be IE:


                            d3dx.h
                              |
            ------------------------------------------
            |                                         |
         initd3d.h   ------>pass d3ddevice9 --->   splash.h

OR

                             d3dx.h
                               |
                            initd3d.h
                               |
                            splash.h (extern the d3ddevice)

OR

Can someone suggest the best way. IE in compile time or programming practice.
BTW while I'm asking, whats the diff between private and protected. I don't like using classes ,too much overhead coding for my taste. I don't want to discount something because it causes me extra work. Yet I see a lot of ppl using classes. Is using classes that much better? Thanks Talib
Try, try and fucking try again.
Advertisement
I usually have a common header with typedefs and common directx headers along with the windows one, since they're not going to change for a long time.

By the way, you don't have to include your custom header, just forward declare it to cut down on a lot of compile times when changing a single line because it stops a chain from occuring if everything includes something else.

Using classes is up to you, some say it makes the project more organized, some say it's constraining.
Meant to say, forward declare another class from another class header file, but that's only if you're declararing it as a pointer or reference(?) within it.
As to the original question, I would personally pass it (make sure you pass a pointer or a reference), but don't worry about it - such little details shouldn't get you stuck - it doesn't really make a difference either way. Don't worry so much about "good programming practice", as long as your code is very understandable (that being said, a lot of good programming practices can make your code more understandable).

As for classes, it depends on the programmer, I think. I personally use classes heavily, but if you don't really see any point in using them, then you probably won't really take advantage of them, so there'd be no real point.

The only difference between private and protected is that private members cannot be accessed by derived classes while protected ones can. If you're not using any inheritence, just use private.
Zorx (a Puzzle Bobble clone)Discontinuity (an animation system for POV-Ray)
Quote:Original post by Talib

I don't like using classes ,too much overhead coding for my taste. I don't want to discount something because it causes me extra work. Yet I see a lot of ppl using classes. Is using classes that much better?


It's mainly a tool to make certain things easier to do, and to make the code easier to manage. But it's not the only tool you have, so you don't have to use them.

I'd say you should definitely learn to use them. Learn what classes can and can't do, because then you can use them or not, but you'll be able to recognize when you run into a situation where classes would make things easier for you.
Quote:Original post by Talib
I don't like using classes ,too much overhead coding for my taste. I don't want to discount something because it causes me extra work. Yet I see a lot of ppl using classes. Is using classes that much better?

Thanks
Talib

a class is nothing more than tool for creating new types that can be conviently used as built-in types. A well-chosen set of user defined types can make a program more concise.

You can create user defined types without classes but there can be problems. Imagine making a stack type without classes you would most probably start by having some data structure say an array to represent the stack and it would most likely be either be in global/namespace (or in C declared static so it's only available in that file scope).

Then you would define some functions to represent the operations of the stack. Now imagine if you wonted to be able to have more than one stack without exposing it's representation, and other operations that involed more than one stack can see where this is going?

So a class is nothing more than a language feature to facilitate the creation of user-defined types, it can have the same schematics as built-in types and can even be as efficient as built-types (if it's a concrete abstraction).
Quote:Original post by Talib
I don't like using classes ,too much overhead coding for my taste. I don't want to discount something because it causes me extra work. Yet I see a lot of ppl using classes. Is using classes that much better?


Better than what? If you don't use classes then you're pretty much just using C.

C++ introduced classes because it made it easier to do object oriented programming than in C on its own. If you want to follow some of the prinicples of object oriented design then C++ is better than C because it does a lot of the hard work for you.

Classes allow:

Encapsulation: you can make data private and only accessible in a limited manner.

Inheritance: you can derive a class from another class and re-use that base code. You can end up coding less.

Polymorphism: classes derived from a base class can be used wherever the base class was expected. This is a development of function pointers but is done in a tidier manner. You don't have to code it yourself. You can introduce new functionality without having to repeat code. If you wanted to express the kind of relation in C it would be difficult.

There are other features to C++ but these are the basics that classes bring you.

Am I coherent? I'm feeling tired - been up for 11 hours since 6:30 and been on a train for 5 1/2 hours.

This topic is closed to new replies.

Advertisement