Experienced coders read. ( Common/efficient writing )

This is a discussion on Experienced coders read. ( Common/efficient writing ) within the C++ Programming forums, part of the General Programming Boards category; K. The reason I asked for experienced coders is because I am haveing a hard time decideing how a 'class' ...

  1. #1
    0x01
    Join Date
    Sep 2001
    Posts
    88

    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?
    Last edited by knave; 04-23-2003 at 02:24 PM.

  2. #2
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    nananana homeeeeeeeework nanana

  3. #3
    0x01
    Join Date
    Sep 2001
    Posts
    88
    nananana homeeeeeeeework nanana
    ???

    I keep re-reading the "nananana" part of your reply and I am trying hard to control myself right now. No this isn't homework.... where the $$$$ did you get "homework" out of my thread?

    And by replying with what you did, you just declared my thread NULL or VOID. I don't think anyone will reply to it now. Thanks you $$$$ing idiot.

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    The first way with all those classes are better because they are reusable and you can add specific member functions to them. You should definatly use a contructor.

    One of the shortfalls of C++ (I think) is the lack of properties for classes. GetColor() and SetColor() is the way to do it (unless you have VC++ where you can have properties using a microsoft specific way). There is also another way. Provide all the data as public and have a Refresh() function which updates it all. Thats an alternative way, has its advantages and disadvantages.

    Everything you asked is really project-dependent.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    i agree with Speedy, it is project dependent.

    for question 1, it depends on if you plan to use the other classes for other programs that just need that class, but if you only need it for this, then it is programmer dependent.

    for question 2: it depends how you want to do it. it is a little clearer to other programmers that may use/look at your code if you have the CreateWindow() fxn.

    for question 4: private is to be able to keep everything encapsulated. data members should really be private, with modifier and accessor fxns used to get and set the data. i.e. in the dimension class, height and width should be private. you can have it public, but private is better for info hiding purposes.

    for question 5: caps are dependent upon programmer discretion. it all depends how you want to do it.

    for question 3: again project and programmer dependent. if you feel it should be an object, then make it one.

  6. #6
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    ^agrees with above and also...

    the important thing is to be consistent. if you decide to use caps for classes then use caps for all classes in your project. if you decide to make member functions start with a lower case letter than make all member functions start.... i think you get the picture.

  7. #7
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Aim for an interface which is complete and minimal. If you want good examples of how to design an interface, have a look at the STL. (Although it's not perfect; for instance std::string has two member functions size() and length() which do the same thing)

    I would add constructors to your structs.
    for example:
    Code:
    struct COORD
    {
       COORD(int _x, _y) : x(_x), y(_y) {};
       int x, y;
    };
    Then you can use the following (easier, in my opinion) syntax:
    COORD myCoord(9,5);
    or even
    myWindow.Create(COORD(9, 5) ...);
    Another thing *I* would do is take most of the parameters from "DrawWindow" and add them to your constructor. In your constructor you should store them locally and provide set/get member functions.
    Last edited by Eibro; 04-23-2003 at 09:50 PM.

  8. #8
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Perspective
    ^agrees with above and also...

    the important thing is to be consistent. if you decide to use caps for classes then use caps for all classes in your project. if you decide to make member functions start with a lower case letter than make all member functions start.... i think you get the picture.
    Eh, I think you'll find a lot of programmers use caps for naming small data structs (like COORD or RECT) but not complete classes.

  9. #9
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Originally posted by Eibro
    Eh, I think you'll find a lot of programmers use caps for naming small data structs (like COORD or RECT) but not complete classes.
    yeah, what i gave was just an example. as long as the programmer uses caps for all small data structs and whatever else for all complete classes, they are being consistent.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Have u Coders Read "the Art of Computer Programming"
    By Liam Battle in forum C++ Programming
    Replies: 17
    Last Post: 05-14-2002, 01:00 PM
  2. Read Array pro!!Plz help!!
    By Supra in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 03:49 PM
  3. Doing a read while in an intr handler
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-14-2002, 04:20 PM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 03:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21