You are not logged in.

#26 2009-03-11 10:51:47

k2t0f12d
Member
Registered: 2008-02-17
Posts: 31

Re: c or c++

I recommend learning languages in this order (whilst using GNU+Linux):

* Bash Shell Script
* Perl, Python, and/or Ruby
* C
* C++ (if at all)
* Lisp

I highly recommend learning Lisp at some stage even if you never use it very much.  You'll become a better programmer in any language for it.

Offline

#27 2009-03-11 12:12:05

genisis300
Member
From: Uk
Registered: 2008-01-15
Posts: 284

Re: c or c++

Thanks i've never looked at lisp,

Regards
Matthew


"is adult entertainment killing our children or is killing our children entertaining adults?" Marilyn Manson

Offline

#28 2009-03-11 13:46:34

bernarcher
Forum Fellow
From: Germany
Registered: 2009-02-17
Posts: 2,281

Re: c or c++

k2t0f12d wrote:

I recommend learning languages in this order (whilst using GNU+Linux):

* Bash Shell Script
* Perl, Python, and/or Ruby
* C
* C++ (if at all)
* Lisp

I highly recommend learning Lisp at some stage even if you never use it very much.  You'll become a better programmer in any language for it.

I'd like to add Eiffel. Although it currently is of rather academic value and not very well implemented in a Linux environment, if you want to really understand the intricacies of reliable object oriented software production, this is a must.

Most useful (but to my knowledge up to now nowhere really implemented--all implementations I know of use some assertion mechanism) is the "design by contract" principle which provides a big leap forward to reliable program production.

For a quick overview see e.g. this wikipedia article:http://en.wikipedia.org/wiki/Eiffel_(pr … _language)

Last edited by bernarcher (2009-03-11 13:48:51)


To know or not to know ...
... the questions remain forever.

Offline

#29 2009-03-20 01:18:06

jamesbannon
Member
From: Paisley, Scotland
Registered: 2008-10-24
Posts: 50

Re: c or c++

Umm, no you can't. C++ and C are different languages and are incompatible with each other.

Ummm... no, actually, they're not incompatible. It is perfectly possible to write programs in both languages that will compile and run perfectly happily when compiled with either compiler, especially if the compilers are from the same vendor. There are some differences in the relevant standards, sure, but this does not mean the languages are incompatible. If it did, then how is all the "legacy code" still running happily after being ported to the new standards?

ETA: And the rant? The guy obviously hasn't actually studied the writings of those who are good at it; e.g., Alexandrescu, Sutter, etc. There are reasons why C & C++ are the way they are, and these are decisions that will obviously affect the way in which programs are written.

Last edited by jamesbannon (2009-03-20 01:38:00)

Offline

#30 2009-03-20 16:11:38

Xilon
Member
Registered: 2007-01-01
Posts: 243

Re: c or c++

krigun wrote:
Xilon wrote:

I would advise against using C++. It might be beneficial to be familiar with it, but in my opinion it's a very ugly language. The most annoying thing is templates. Defining them seems like a hack - having to write the implementation in the "interface" (.h file). Using them isn't that great either.

That silly statement right there is just plain FUD. First of all, nobody forces your hand to create templates when you are writing code in C++. Second, I can't believe you said that C++ is THE HORROR, but Obj-C, now THERE is a real OO language for you. There are so many things wrong with Objective-C that I wouldn't even know where to start.

I have to disagree. When I used C++ and used templates, there was no way of writing the implementation in a .cpp file, it had to be in the same file as the interface. Maybe I was doing something wrong or the compiler (g++) didn't support it, either way, I couldn't do it. Indeed, writing templates is not forced, but a lot of C++ code uses templates, and if you write a library, you will most likely be pressured into using templates. There's a reason the concept of a "tasteful subset of C++" exists, as the LLVM devs put it.

Sure there are some problems with Objective-C. For instance I sometimes crave for operator overloading, and auto-boxing between C types and objects (e.g., int to NSNumber). In general Objective-C is a much nicer language than C++. The additional syntax to C is minimal, the messaging model is a lot nicer to "calling" functions, categories are quite useful, there are properties (no need to write setters and getters), etc. I'm sure there are more pros than cons (at least for me). In C++ however, I'm sure there are more cons than pros. That's not to say that C++ doesn't have it's place. It's a popular language for a reason. Objective-C's performance is inferior to C++'s. In most areas where C++ is widely used, it can also be avoided though.

Thinking about C++ a bit more, I remembered about D. Recently there's been a lot of talk about it, with druntime coming out and LLVM's compiler making progress. It might soon be a viable language to use. It sure does look nicer than C++, and has some interesting features.

jamesbannon wrote:

Umm, no you can't. C++ and C are different languages and are incompatible with each other.

Ummm... no, actually, they're not incompatible. It is perfectly possible to write programs in both languages that will compile and run perfectly happily when compiled with either compiler, especially if the compilers are from the same vendor.

Yes, it is possible to make C code work with a C++ compiler. The point is that you have to use the extern keyword in order to do so. C++ is not a superset of C, and therefor an unmodified C program will not always work correctly under a C++ compiler. When I said that they are incompatible I used the term loosely. They are similar languages and in general they are compatible, but there are many differences to be aware of.

TheBodziO wrote:

I'm curious about implementation of OO principles in C. Could you elaborate more about it or at least give a couple of links to some examples? I mean a little bit more than packing data and functions into a single struct.

The main thing to understand is that OOP is not a type of language, but a paradigm. OOP languages simply provide syntactic sugar to make dealing with objects easier. The very basic concept of OOP is that data is grouped with functions which act on that data (i.e., objects). In C this is very easily done using structs and functions which accept that struct as a parameter. This allows for abstraction and encapsulation - two principles of OOP. Inheritance is a little more tricky, and I haven't really experimented with it yet. It seems you can use casting to simulate this. Eg:

typedef struct _Object {
    int refCount;
} Object;

typedef struct _Person {
    Object isa;
    char *name;
} Person;

A Person object can then be cast to an Object and be used in the same way as an Object. I haven't actually tested this so I'm not sure. Polymorphism is a lot harder with this way of doing things.
Edit: This obviously only supports inheritance from a single "class" - this isn't necessarily a bad thing though.

I much prefer the message passing variant of OOP (ala Smalltalk and Self). This is harder to implement than the previous method, because you have to have a way of sending messages to objects. I don't know of a C library which does this. I thought GObject did, but it seems "messages" are for asynchronous programming. The Objective-C and Io runtimes, however, use messaging. I believe Ruby does also. By sending messages, it's much easier to implement things like inheritance and polymorphism (among other cool stuff). By using void*, for instance, you can have dynamic typing. Given a void *object, you don't care what type it is, you just want to send it the "bark" message (no need for C++ templates tongue).

I suppose the best thing to look at would be GObject. I haven't really looked into it myself, but it is interesting.

Last edited by Xilon (2009-03-20 16:23:31)

Offline

#31 2009-03-20 17:05:23

pizmooz
Member
Registered: 2009-01-11
Posts: 21

Re: c or c++

TheBodziO wrote:
Xilon wrote:
TheBodziO wrote:

If you want to write a program and see objects—choose c++.

Or Objective-C (a real superset of C), or just use loosely coupled functions operating on a single struct. All OOP principles can be emulated using C. Glibc, Objective-C, Python, Io, and a bunch of other languages/libraries are proof of this.

I agree about Objective-C but since a question was about C or C++ I suggested one of them to keep on topic.

I'm curious about implementation of OO principles in C. Could you elaborate more about it or at least give a couple of links to some examples? I mean a little bit more than packing data and functions into a single struct.

The linux kernel uses OO C in many places.  Take a look at http://lwn.net/images/pdf/LDD3/ch03.pdfCh3 in Linux Device Drivers (pg 49 under File Operations).  Basically you use a struct with function pointers as an object

Offline

#32 2009-03-21 03:29:08

jamesbannon
Member
From: Paisley, Scotland
Registered: 2008-10-24
Posts: 50

Re: c or c++

I have to disagree. When I used C++ and used templates, there was no way of writing the implementation in a .cpp file, it had to be in the same file as the interface. Maybe I was doing something wrong or the compiler (g++) didn't support it, either way, I couldn't do it. Indeed, writing templates is not forced, but a lot of C++ code uses templates, and if you write a library, you will most likely be pressured into using templates. There's a reason the concept of a "tasteful subset of C++" exists, as the LLVM devs put it.

That part of template programming (the export keyword) is not quite what Stroustrup envisaged when he described it. Separate compilation of templates is possible, but, AFAIK, no vendor has the exact strategy worked out yet. Even the few implementations of export that do exist all require the source code to be visible at the point of instantiation (and, if you think about it, that makes a great deal of sense - how is the compiler supposed to "know" how to check types in a template that might be instantiated well after it's designed?). Also, how come generics has been copied in many other languages, e.g., Java, C#, Haskell, after first being mooted in C++? Because it is useful. void* (or plain object) is no substitute for strong type-checking.

Offline

#33 2009-03-22 00:51:39

pauldonnelly
Member
Registered: 2006-06-19
Posts: 776

Re: c or c++

jamesbannon wrote:

Also, how come generics has been copied in many other languages, e.g., Java, C#, Haskell, after first being mooted in C++? Because it is useful. void* (or plain object) is no substitute for strong type-checking.

I don't think C++ was the first to come up with generic programming.

Offline

#34 2009-03-22 18:31:37

simongmzlj
Member
From: Canada
Registered: 2008-11-06
Posts: 135

Re: c or c++

I'm a fan of D. It does C++ how I would have done C++ in a very simple but powerful language.

Offline

#35 2009-03-22 19:36:09

jamesbannon
Member
From: Paisley, Scotland
Registered: 2008-10-24
Posts: 50

Re: c or c++

pauldonnelly wrote:
jamesbannon wrote:

Also, how come generics has been copied in many other languages, e.g., Java, C#, Haskell, after first being mooted in C++? Because it is useful. void* (or plain object) is no substitute for strong type-checking.

I don't think C++ was the first to come up with generic programming.

But it was one of the first.

Don't get me wrong, there's lots I don't like about C++: 'horrid, convoluted syntax; large changes in semantics for very closely related syntactical constructs (e.g., T x = v; and T x(v);); and don't get me started on the use of semi-colons as a statement terminator! However, we have to be sure we are comparing like with like. Comparing it to, say, Haskell or Lisp, is fundamentally wrong-headed.

Offline

#36 2009-03-24 05:25:15

Xilon
Member
Registered: 2007-01-01
Posts: 243

Re: c or c++

Xilon wrote:

Sure there are some problems with Objective-C. For instance I sometimes crave for operator overloading, and auto-boxing between C types and objects (e.g., int to NSNumber).

I'd just like to point out that apparently the Étoilé project has added support for auto-boxing (C types into NSValues and vice versa, afaik). I'm not sure on the details but that's pretty damn awesome.

Offline

#37 2009-04-22 10:50:23

b3n
Member
Registered: 2008-11-12
Posts: 20

Re: c or c++

jamesbannon wrote:

Umm, no you can't. C++ and C are different languages and are incompatible with each other.

Ummm... no, actually, they're not incompatible. It is perfectly possible to write programs in both languages that will compile and run perfectly happily when compiled with either compiler, especially if the compilers are from the same vendor. There are some differences in the relevant standards, sure, but this does not mean the languages are incompatible.

Just because "it is perfectly possible to write programs in both languages that will compile and run perfectly happily when compiled with either compiler" doesn't mean they are "compatible".

This is valid C and Perl:

#include <stdio.h>
#define do main()
do {
   printf("Hello World!\n");
}

but that doesn't mean C and Perl are the same language, or "compatible".

Offline

#38 2009-04-30 02:11:24

Intruder89
Member
From: Belgrade, Serbia
Registered: 2009-02-20
Posts: 28

Re: c or c++

Well writing objects in C ... is for practice only! Why use procedural language that wasn`t invented for OOP and make hell with your life?? C is a great language - for system things, terminal things - so NO objects (yes you can emulate ... but why?). C gives a programmer a lot of freedom C++ is strict! Trust I saw many programms crashing with C++ compiler only becouse he won`t allow things that C allows (especialy when working with pointers...).

So, choose language for you needs. Learn programming is not in sintax of language ... it`s in algorithams. In a way you are going to solve specific problem in efficient way (optimal memory usage, time needed for runing and ofcourse possabilities of language). And yes there is a reason C++ calls it self C++ (first its C .. then it`s little improved smile ). Now I know a lots of C++ programmers won`t aggree with me but good thing here is that we are all open for arguments.

Offline

Board footer

Powered by FluxBB