CONVERT C TO CPP

[color=red]
HI everyone I go tthis source code from the web & i would like to use it as a base for my minproject.Unfortunately it is written in c & I am required to have it in c++. Can anyone suggest tips for converting it into a cpp program?I have enclosed it as an attachment.

Thanks

P.S It is an old non standard code & will not work on most modern compilers
[/color]

Comments

  • First of all, dos.h and graphics.h won't work on modern compilers as you say. Most of the code won't work on a standard C nor C++ compiler.

    Apart from that, there are just minor things to correct, not much is different between the languages, and most
    C++ compilers can compile C code (but with warnings). Here are some things I suggest:

    - Remove the BOOLEAN typedef together with TRUE and FALSE, and use C++ bool instead.

    - You need to use explicit typecasts in many cases, because C++ enforces stricter typecasts than C. For example, a C++ compiler might expect you to explicitly pass a "const char*" instead of a char* to a function etc. This is all part of good programming practice in C, so it should be there anyway.

    The code also contains several serious bugs where if- and for statements lack {}.
  • : First of all, dos.h and graphics.h won't work on modern compilers as
    : you say. Most of the code won't work on a standard C nor C++
    : compiler.
    :
    : Apart from that, there are just minor things to correct, not much is
    : different between the languages, and most
    : C++ compilers can compile C code (but with warnings). Here are some
    : things I suggest:
    :
    : - Remove the BOOLEAN typedef together with TRUE and FALSE, and use
    : C++ bool instead.
    :
    : - You need to use explicit typecasts in many cases, because C++
    : enforces stricter typecasts than C. For example, a C++ compiler
    : might expect you to explicitly pass a "const char*" instead of a
    : char* to a function etc. This is all part of good programming
    : practice in C, so it should be there anyway.
    :
    : The code also contains several serious bugs where if- and for
    : statements lack {}.

    [color=red]Thanks Lundin
    That is a good starting point for me.Now could you give me suggestions to implement classes in the above code.Thats the problem which was bugging me all the time.
    P.S dont bother about the errors it runs perfectly well on my old compiler
    Thanks a lot
    [/color]
  • If you want proper object orientation you will have to rewrite the whole program from scratch. Use one class for each item you are drawing. Rectangle, text etc. Maybe one class for mouse handling, and so on.
  • : If you want proper object orientation you will have to rewrite the
    : whole program from scratch. Use one class for each item you are
    : drawing. Rectangle, text etc. Maybe one class for mouse handling,
    : and so on.
    :
    [color=blue]
    Okay will do that
    Thanks
    [/color]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories