Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Straying to C++
#10
Quote:To all those QB programmers out there who are, like I was, terrified of the thought of having to learn a new programming language, fear not. C++ is unbelievably easy to learn.
I also used to say that C++ is long and tedious to program in, but there isn't really all that much more code to enter.

Glad to hear people branching out and getting into new languages (the more you know the better). A couple of things strike me as odd in the above quote however. Learning any language (procedural anyway) after you know one already is easy, its a matter of learning the new syntax. eg from QB to C:
Code:
FUNCTION myFunc%(x as INTEGER)
  IF x > 0 THEN
    myFunc% = 1
    EXIT FUNCTION
  ELSE
    myFunc% = 2
    EXIT FUNCTION
  END IF
END FUNCTION
Easily translates into C/C++ as:
Code:
int myFunc(int x) {
  if(x > 0) {
    return 1;
  else
    return 2;
  }
}
However, with a language such as C/C++ there is far more to know than meets the eye at first glance, for instance check out the piece of code I posted in the general section ("A List" thread), its perfectly valid ANSI C code, but its not easy to understand. C has many, many tricks and underlying concepts that can be very difficult to grasp (and no Im not just talking about pointers).

The other thing I find interesting is that you say you find you dont have to do much extra coding in C/C++. This can of course vary depending on how robust your code is. For example.
Code:
typedef struct {
  int a;
  int b;
  char c;
} mem_t;

mem_t *x;

/* Allocate some memory for the mem_t struct */
x = malloc(5);

/* Stuff goes here */

exit(0);
Thats valid working C code, its not too long either, but its not good C code. C and C++ dont do anything you dont ask them too, good stable C code is often longer. The memory allocation above should be replaced as follows:
Code:
if((x = malloc(sizeof(mem_t))) == NULL) {
  fprintf(stderr, "Couldnt allocate memory for mem_t struct\n");
  exit(1);
}

/* Stuff goes here */

free(x);
exit(0);

Note that for portability reasons errors should use fprintf(stderr, "") and memory should be explicitly freed (even though its not always necessary). These things are what makes C code longer than code from other languages such as QB and Java which provide either compiler or runtime protection against many things which C/C++ dont.

That said, C/C++ often allow to code things in very short form also, take the myFunc function above which can be coded in C as:
Code:
#define myFunc(x) ((x > 0) ? 1 : 2)
Of course, writing an entire C program in such terse form can get you into trouble, check out http://www.ioccc.org/ for examples.

Get a good book on C/C++ from your local library (not an internet tutorial) and read it from cover to cover. C and C++ are /very/ powerful languages, but they do take a bit more learning effort than many other languages if you want to use them effectively and correctyly. Happy coding :lol:
esus saves.... Passes to Moses, shoots, he scores!
Reply


Messages In This Thread
Straying to C++ - by fox - 02-12-2003, 07:11 PM
Straying to C++ - by SJ Zero - 02-12-2003, 08:21 PM
Re: Straying to C++ - by Piptol - 02-12-2003, 10:52 PM
Straying to C++ - by Agamemnus - 02-13-2003, 04:22 AM
Pset, print is easy!! - by fox - 02-13-2003, 05:19 AM
Straying to C++ - by Hexadecimal Disaster - 02-13-2003, 08:31 AM
hmm.... - by fox - 02-13-2003, 09:21 AM
Straying to C++ - by Hexadecimal Disaster - 02-13-2003, 09:48 AM
Straying to C++ - by relsoft - 02-14-2003, 10:57 AM
Straying to C++ - by LooseCaboose - 02-15-2003, 10:28 AM
Straying to C++ - by na_th_an - 02-15-2003, 07:18 PM
Straying to C++ - by Piptol - 02-15-2003, 08:32 PM
Straying to C++ - by toonski84 - 02-15-2003, 09:02 PM
Straying to C++ - by na_th_an - 02-15-2003, 09:43 PM
Straying to C++ - by Agamemnus - 02-15-2003, 10:53 PM
Straying to C++ - by na_th_an - 02-16-2003, 12:14 AM
Straying to C++ - by LooseCaboose - 02-18-2003, 04:09 AM
Straying to C++ - by na_th_an - 02-18-2003, 09:05 AM
Straying to C++ - by Hexadecimal Disaster - 02-18-2003, 09:30 AM
Straying to C++ - by LooseCaboose - 02-20-2003, 04:08 AM
Re: Straying to C++ - by fox - 05-25-2009, 06:13 AM
Re: Straying to C++ - by Ninkazu - 06-26-2009, 05:24 AM

Forum Jump:


Users browsing this thread: 2 Guest(s)