User forums > General (but related to Code::Blocks)

Too dumb to get started?

<< < (9/9)

mdelfede:

--- Quote from: Newbie0815 on August 02, 2006, 12:22:54 am ---isn´t that what I´m doing? with the only difference that I have a concrete project to work on, rather than dry studying the theory?

--- End quote ---
Here I agree, for me too it's easier learning doing something that makes me happy when I see it does something useful, instead of coding only small samples. But... keep in mind 2 points :
1- without the theory (which you can learn while doing your test project) you'll not go too far.
2- your result has many, many and many chances to become something horrible and impossible to mantain. so, take it as-is, something to practice on and NOT something beautiful and bug-free. You'll notice it at the end when you'll think "why have I coded it like this ?" That happens to me when I'm in a hurry to see it working; when it's done, I start correcting again and again, up to I give up and rewrite from scratch.


--- Quote from: Newbie0815 on August 02, 2006, 12:22:54 am ---and what about my actual question? how to "translate" code from other compilers? Theres not THAT much different between borland and gcc, I just think since this must be a pretty common problem for those who use prewitten libraries and classes to speed up their development, there should be an easier way than go through the code line by line

--- End quote ---

As I told you, Borland has some non-standard constructs that you simply can't translate to pure C++, at least for C++Builder 6 which I know.
For example, in C++Builder you can do such a thing :


--- Code: ---class Test
{
  private :
    int anInteger;

    void setInteger(int i) { if (i >= 0) anInteger = i ; }

  public :
    __property IntProp = {read = anInteger, write = setInteger} ;
};

Test test ;

test.IntProp = 2 ;
int j = test.IntProp ;

--- End code ---

Even if I find it a beautiful construct (wondering why it's not included in Ansi C++....), that's NOT standard. In pure C++ you write :


--- Code: ---class Test
{
  private :
    int anInteger;

  public :
    void setInteger(int i) { if (i >= 0) anInteger = i ; }
    int getInteger(void) { return anInteger ; }

};

Test test ;

test.setInteger(2) ;
int j = test.getInteger() ;

--- End code ---

As you see, even with this small code sample, you must change a lot. In C++ builder the __property construct is used widely, as are other non standard constructs. When you use the IDE to make an app skeleton, it gives you code full of such nonstandard constructs. Believe me, takes LOOOONG to convert to pure ansi C++.

Ciao

Max

kagerato:

--- Quote from: Der Meister ---Just to be precise: C is *NOT* a functional programming language. It's an imperative (or procedural, if you like this word more) language. Functional programming languages are languages like Haskell or Lisp.
--- End quote ---

Correct.  C is sometimes mistakenly referred to as a functional language because the basic unit your imperative statements are organized into is called a function.  Many people (including myself) fail to make the proper distinction.

Semantics regarding this usually do not come into play because there are hardly any functional languages in widespread use outside the academic world.


--- Quote from: kidmosey ---And as for the rest of your article... Well said!
--- End quote ---

--- Quote from: PDEE ---Wow kagerato, great post. I learnt something from that. Thanks
--- End quote ---

I simply give my best -- there's no point in saying anything if you don't say something meaningful, I feel.


--- Quote from: Newbie0815 ---I got lost almost right away, mostly because there are no 2 tutorials for the same compiler, from videos at MSN how to write applications in visual C++ to plain C++ code I can use in CodeBlocks, they are not compatible, CodeBlocks allows me to import almost any format, but mixing them doesn´t work
--- End quote ---

Absolutely no clue what you're trying to say.  "Formats"?  Source code is normally stored as ASCII text (or occasionally, using an ANSI codepage or UTF-8 instead).  As for C++ "dialects", read further.


--- Quote from: Newbie0815 ---I´ve noticed, most of them are written in VB or VC, I´ve even downloaded and installed Visual Studio Express, but for one the code is so way different from gcc, almost an entirely new language and the options are pretty much limited to what the compiler offers
--- End quote ---

The standard method of GUI programming using Visual C++ is MFC, the Microsoft Foundation Classes.  As the name suggests, they are an object-oriented wrapper around the win32 API.

That doesn't have much, if anything, to do with the kind of code and samples you appear to be dealing with.  I'm not sure where you picked up the idea that every compiler has its own C++ dialect, completely incompatible with another's.  To a very limited degree, that was true ten years ago.  It's not a matter of concern these days, as C++ has gradually become more and more standardized.

Visual Basic 6 is not in any way compatible with C or C++ code.  It's an entirely different language, one that I would recommend newcomers stay away from.

Visual Basic .NET doesn't have nearly as many glaring problems as VB6.  However, using it ties you to the .NET platform.  (C# has alternate implementations and uses.)  Since the changes made to Visual Basic for the purpose of transforming it into a .NET-compatible language have eliminated several of the original benefits of the language and platform, C# has seen much greater adoption.

By "Visual Studio Express", I believe you mean "Visual C++ Express".  Upon last examination, the entire Visual Studio suite was not available for free as a single entity.  Rather, there are separate packages for each language.

I don't recommend using Visual Studio -- it's huge and it provides little that a free IDE like CodeBlocks does not (assuming we're working with C++).  Furthermore, the Express editions are, in essence, a marketing lure for the .NET platform and other Microsoft-dominated technologies.


--- Quote from: Newbie0815 ---Thats where I´m stuck now, I found just ONE tutorial that explains it short and easy at http://www.c-worker.ch/tuts/wstut_op.html (in german) but that one is written for borland and my attempts to translate it to gcc have failed so far, meaning I can´t even start implementing it into my project

Maybe someone here can help me out? How to mix code written for different compilers or how to translate it?

The source for both, is direct available on the page

Client: http://www.c-worker.ch/tuts/sock.c
Server: http://www.c-worker.ch/tuts/socksrv.c
--- End quote ---

This code is not "written for borland".  It's win32 C, specifically using the winsock library.  It will build using any C compiler, so long as 1.) the win32 headers are available and 2.) there are no syntax or linking errors.

I successfully compiled "sock.c" using gcc and this command:


--- Code: ---gcc sock.c -o sock.exe -lws2_32
--- End code ---

The linker command must go on the end there.  (Took me a few minutes to remember this.  Had it specified before the source file and was wondering why linker errors were appearing.  The GCC man page warns you about this -- always a good idea to read the documentation.)

The fact that you have to explicitly link (using the -l switch, for GCC) to particular libraries is simply a part of C/C++ programming.  This little winsock example fails just as spectacularly in Visual C++ if you fail to specify the library correctly.

This example is simple because it's a console application using winsock to provide basic networking facilities.  Nevermind that it hardly does anything, let alone provide a "simple chatserver".  Don't deceive yourself into thinking it's a simple matter to scale basic examples into production-quality complete applications.


--- Quote from: Newbie0815 ---Object oriented programming isn´t new to me (...)
--- End quote ---

--- Quote from: Newbie0815 ---So I know what it is about, I know specially in win32 applications theres a LOT of inherited stuff from the libraries
--- End quote ---

Oh? You don't seem to know what inheritance is.  Inheritance does not mean "included from header files".  Inheritance is basing a sub-class on the methods and fields of its parent.  Using the win32 API does not involve inheritance.  You missed the part about win32 being written in C, and hence lacking any object-orientation.


--- Quote from: Newbie0815 ---I still don't like the idea of global and local variables, but I do understand the concept (...)
--- End quote ---

In C, minimizing global variables is essential to preventing name conflicts and increasing the overall readability of the code.  It's a core concept.

In C++, you don't use global variables, period.  Furthermore, encapsulating classes effectively by minimizing the number of public fields and methods is a critical part of writing good code.  Strong usage of encapsulation leads to easy isolation of bugs, among other benefits.


--- Quote from: Newbie0815 ---and what about my actual question? how to "translate" code from other compilers? Theres not THAT much different between borland and gcc (...)
--- End quote ---

Concerning the referenced code, there is no difference.  No compiler-specific feature has been used.  Indeed, it's rare to find an instance of it in C code to begin with.

Your problem is simple.  Either you do not have your compiler installed correctly, or you do not know how to use it (or the IDE, whichever is relevant).

marfig:
I quit. This is a clear case of "you will see" :)

kidmosey:
A few quick comments on encapsulation that were missed earlier.

1. It prevents arbitrary setting of variables.  Instead of the user/programmer setting a member "weight" to a negative value, you just add a constraint to the SetWeight method.  You can make it throw an exception, return an error value, or automatically set the value to zero.

2. You may want certain values to be read-only.

3. (this one was mentioned) You may want to change something later without changing the interface.  Say you have a member m_total.  If you set this as public, you will never be able to do things like returning "width * height" to optimize the size of your class (this also works the other way around).

4. Last I checked, you cannot set class members as virtual.

I'm sure I missed several more, but reading this thread made me tired :D

mdelfede:

--- Quote from: kidmosey on August 02, 2006, 07:32:17 am ---A few quick comments on encapsulation that were missed earlier.

1. It prevents arbitrary setting of variables.  Instead of the user/programmer setting a member "weight" to a negative value, you just add a constraint to the SetWeight method.  You can make it throw an exception, return an error value, or automatically set the value to zero.

2. You may want certain values to be read-only.

3. (this one was mentioned) You may want to change something later without changing the interface.  Say you have a member m_total.  If you set this as public, you will never be able to do things like returning "width * height" to optimize the size of your class (this also works the other way around).


--- End quote ---

A little comment about... I think that Borland with his language extensions made encapsulation better with properties, taking the advantages of both member variables (simpler use in expressions, clearer code) and getters/setters (good encapsulation, optimizations and calculations on-the-fly).
I'd really like such a construct in future c++ standard.

Ciao

Max

Navigation

[0] Message Index

[*] Previous page

Go to full version