Experienced coders read. ( Common/efficient writing )
K.
The reason I asked for experienced coders is because I am haveing a hard time decideing how a 'class' should be written , when a 'class' should be created, how a member variable of the 'class' should be accessed, the capitolization of 'classes', functions, and variables ( I will explain below ).
How should a class be written?
For instance; Lets write an ascii window class
(You won't see any "real" code here; like the actual drawing of the window:NONE OF THE CODE HERE HAS BEEN USED IN ANY PROGRAM)
Code:
class DIMENSION
{
public:
int height;
int width;
};
class COORD
{
public:
int X;
int Y;
}
class COLOR
{
public:
int foreground;
int background;
}
class WINDOW
{
public:
void SetColor( COLOR colors )
void SetSize( DIMENSION dimensions )
void SetCoord( COORD coords )
int GetSize();
ETC...........
..............
void CreateWindow(COORD coords, COLOR colors, DIMENSION dimensions etc... // or could be called DrawWindow() instead of CreateWindow (whatever).
private:
ETC.......
};
int main()
{
// "#" means any number
COLOR colors;
colors.foreground = #;
colors.background = #;
DIMENSION dimen;
dimen.height = #;
dimen.width = #;
COORD coords;
coords.X = #;
coords.Y = #;
// Create a WINDOW and then draw it.
WINDOW Pssh
Pssh.CreateWindow(coords, colors, dimen);
return 0;
}
Ok, I want anyone who wants to, to tell me what you immediatly thought was wrong (IT CAN BE ANYTHING!!) or what you think you would have done before reading the questions below.
MY questions
1st question: Would it have been better to place/create all the variables within the COORD, COLOR, and the DIMENSION's class into the WINDOW class?
For example:
Code:
class WINDOW
{
public:
void SetColors( etc..
void SetCoords( etc..
void SetDimensions( etc..
ETC..............
CreateWindow( int x, int y, int background, int foreground, int width, int height )
ETC..............
private:
int X;
int Y;
int foreground;
int background;
int width;
int height;
ETC.............
};
int main()
{
WINDOW Pssh;
// Ok, pay attention to how many arguments are passed,
// I have a question about that also.
Pssh.CreateWindow( 0,0, color#, color#, 30, 10 );
// That many arguments look... ugly... you know? Right?
// A bunch of numbers get passed.
return 0;
}
What do you think? Or what would you have done differently?
2nd question: Should I take advantage of the class's constuctor??
For example:
Instead of creating a member function that creates/draws the window ( CreateWindow( etc... ) )
I could use the constructor and declare, define and draw a window at the same time...
Code:
class WINDOW
{
public:
WINDOW( arguments ....); // useing the constructor instead of creating and useing the function CreateWindow()
~WINDOW(); // deconstructor
ETC...................
BACK INTO MAIN() it would look something like
// it would defined and drawn on to the screen
WINDOW Pssh( arguments would go here );
But... I get this feeling that tells me its NOT cool/good to do that.
I feel like that isn't common????? And it looks ugly.
3rd question: When writing a program, when is it I should say, "that needs to be a class"?
Is a drawing an ascii window a good excuse for writeing a class??
If yes, what would you place in the class? For instance; what variables would you create? Would you have functions that would return anything, if so, what?
I know this question is blunt... and its hard for me to explain exactly what I need help with. So if anyone has an example of a WINDOW class you created or anything similiar, I would appreciate it if you posted it up here so I could take a look at it. ( NO, I AM NOT ASKING YOU TO JUST START WRITING SOMETHING RIGHT NOW )
4th question: Why would I create functions like "SetVar()"?In other words why the hell would I make anything private??????
5th question: CAP's classes, functions, class types or????
I have noticed in many books, when createing a class they use the following capitolization.
Code:
class Window
{
etc...
}
But I also noticed in the MSVC++ compiler, that the classes use the following method
Code:
class WINDOW
{
etc...
}
To sum this thread up....
Basically, what is "quality" code? And whats common?