LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-07-2009, 12:41 PM   #1
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
C++ Alternatives


I wanted to to a few projects that need a compiled object-oriented language, so I tried C++ and can't say I like it. C is great but it is not OO. So I was windering if there is some other compiled language that is OO, has a free/open source compiler for Linux, and is preferably backward-compatible with C?
 
Old 12-07-2009, 12:58 PM   #2
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,046

Rep: Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100
Quote:
Originally Posted by MTK358 View Post
I tried C++ and can't say I like it. C is great but it is not OO.
I don't know if any other language could be a better fit for your requirements.

What didn't you like about C++?

Lots of people dislike C++ because of features that are similar to C. But you said "C is great".

Lots of people, who like C, dislike C++ because it was their first introduction to OO and they don't actually like OO. But you think you want OO.

Lots of people dislike C++ because of all the confusing detailed rules inside the obscure advanced features (such as partial specialization of templates). If that is what bothered you, you had a bad introduction to C++. If you don't need the power of such features, you can program in C++ without being bothered by their existence. If you do need the power, some hypothetical language with a more self consistent design for such features would be great if it existed. But it doesn't. If you need such features and good performance, C++ is the only choice.
 
Old 12-07-2009, 12:59 PM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by MTK358 View Post
I wanted to to a few projects that need a compiled object-oriented language, so I tried C++ and can't say I like it. C is great but it is not OO. So I was windering if there is some other compiled language that is OO, has a free/open source compiler for Linux, and is preferably backward-compatible with C?
I think you'd better look at languages not from "C" family.

Look at, say,
http://en.wikipedia.org/wiki/Eiffel_...mming_language .
 
Old 12-07-2009, 01:17 PM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Original Poster
Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by johnsfine View Post
I don't know if any other language could be a better fit for your requirements.

What didn't you like about C++?

Lots of people dislike C++ because of features that are similar to C. But you said "C is great".

Lots of people, who like C, dislike C++ because it was their first introduction to OO and they don't actually like OO. But you think you want OO.

Lots of people dislike C++ because of all the confusing detailed rules inside the obscure advanced features (such as partial specialization of templates). If that is what bothered you, you had a bad introduction to C++. If you don't need the power of such features, you can program in C++ without being bothered by their existence. If you do need the power, some hypothetical language with a more self consistent design for such features would be great if it existed. But it doesn't. If you need such features and good performance, C++ is the only choice.
For example, I once wrote a derived class from another class that had different constructors, and C++ complained about it. But I don't want them to have the same constructors! In fact, the base class's constructors were completely irrelevant for the derived class!

Java's pass-by-reference makes much more sense and wastes less computing power. Usually when I want to copy an object I want a handle that can modify the original. This can be done in C++ with pointers, but you have to worry about the original going out of scope.

C++ references seem very confusing, how do I know if it's a reference or variable if the syntax is the same? Even if you get past this, where do I use references and where do I use pointers?

Mainly because the above things I said, it is very difficult to write a program that feels simple and solid, because there is either too much ways to do it or no way to do it. Programming in C++ feels more like some poor hack than making a solid program.
 
Old 12-07-2009, 01:48 PM   #5
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,046

Rep: Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100
Quote:
Originally Posted by MTK358 View Post
For example, I once wrote a derived class from another class that had different constructors, and C++ complained about it. But I don't want them to have the same constructors! In fact, the base class's constructors were completely irrelevant for the derived class!
I think you misunderstood the problem.

A derived class normally has different constructors than the base class. You cannot inherit constructors (I would like C++ better if you could).

A derived class must invoke the constructor of the base class (either explicitly or implicitly). But that should be true in any OO language.

Quote:
Java's pass-by-reference makes much more sense and wastes less computing power.
Java references are simpler to use, but they usually waste more computing power than C++ references.

Quote:
Usually when I want to copy an object I want a handle that can modify the original. This can be done in C++ with pointers, but you have to worry about the original going out of scope.
So why do you like C?

You just described one of the major reasons many programmers dislike both C and C++. But how can you dislike that in C++ but like C?

Quote:
C++ references seem very confusing, how do I know if it's a reference or variable if the syntax is the same?
The fact that the syntax is the same between value and reference is the strength of the reference feature of C++. But I can see how you might be uncomfortable with it before getting used to it.

In any non trivial programming language, the meaning of a chunk of code depends on things defined elsewhere. When I switched from C to C++, I was initially put off by the fact that meanings depend on distant definitions a lot more in typical C++ code than C and there are many more ways they can depend (such as whether some argument was declared by value or reference). But I soon got used to it, and appreciate the extra ways to localize design decisions.

Quote:
Even if you get past this, where do I use references and where do I use pointers?
A lot of the choice between pointers and references is a matter of style and preference. If C++ references bother you, I think you can use pointers in almost all cases without losing much. The difference between pointers and references exists primarily at compile time (not at run time).

In more advanced programming (such as overloading operators and defining templated methods) there are things that really need references

Quote:
there is either too much ways to do it or no way to do it.
If there is no way, you're either confused or what you want probably couldn't be done directly in other languages either.

There certainly are too many ways to do almost anything in C++ (and I expect you don't even know about half the ways yet). I think you can learn a good subset of C++ to work with.

Last edited by johnsfine; 12-07-2009 at 01:58 PM.
 
Old 12-07-2009, 02:02 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
One major consideration should always be the "social value" of the language. For example, you'll want a language used by lots of people if you want others to be involved eventually. Not only that, but the language should match the problems you're solving, as far as what other people might use. You don't have to "follow the crowd", but if you want help and you want people to gain from your source code, you should definitely take other programmers into consideration. If you're code is "yours forever" then you don't have to worry about this.

Along the same lines, you should look at the trends in language usage. I hate to say this, but Java's still on the rise, although C and C++ aren't going anywhere anytime soon. You might also want to look into the common languages used in Linux such as python, perl, lisp, etc.
Kevin Barry
 
Old 12-07-2009, 03:10 PM   #7
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Original Poster
Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by johnsfine View Post
A derived class must invoke the constructor of the base class (either explicitly or implicitly). But that should be true in any OO language.
Not so in Java.

Quote:
Originally Posted by johnsfine View Post
You just described one of the major reasons many programmers dislike both C and C++. But how can you dislike that in C++ but like C?
I guess that was more of a comparison to Java.

Quote:
Originally Posted by johnsfine View Post
A lot of the choice between pointers and references is a matter of style and preference.
It still seems kind of strange to have two ways of doing exactly the same thing.

Quote:
Originally Posted by ta0kira
One major consideration should always be the "social value" of the language. For example, you'll want a language used by lots of people if you want others to be involved eventually. Not only that, but the language should match the problems you're solving, as far as what other people might use. You don't have to "follow the crowd", but if you want help and you want people to gain from your source code, you should definitely take other programmers into consideration. If you're code is "yours forever" then you don't have to worry about this.
Yeah, I was trying to avoid "obscure" languages because of this.

Quote:
Originally Posted by ta0kira
Along the same lines, you should look at the trends in language usage. I hate to say this, but Java's still on the rise, although C and C++ aren't going anywhere anytime soon. You might also want to look into the common languages used in Linux such as python, perl, lisp, etc.
Kevin Barry
The main reason I want something that is a subset of C is because many of my programs will rely on C libraries (e.g. Xlib). And it must be OO (this is almost the only reason I am not using C).

Last edited by MTK358; 12-07-2009 at 03:14 PM.
 
Old 12-07-2009, 03:23 PM   #8
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,046

Rep: Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100
Quote:
Originally Posted by MTK358 View Post
Not so in Java.
Of course it is true in Java. Either the derived class constructor explicitly invoked the based class constructor, or the language does it for you.
Java is a tiny bit less strict in its rules for invoking the base class constructor. But it still has to be executed.

Quote:
It still seems kind of strange to have two ways of doing exactly the same thing.
It isn't exactly the same thing. In more advanced programming, the power of references that cannot be duplicated by pointers comes into play.

Even in simple programming, the two different syntaxes for one behavior are a useful choice. Do you think C, Java, etc. should not have for loops because the meaning is the same as a while with a couple extra lines? Should C not have X[n] syntax because the meaning is the same as *(X+n)?
 
Old 12-07-2009, 03:28 PM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Original Poster
Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by johnsfine View Post
Of course it is true in Java. Either the derived class constructor explicitly invoked the based class constructor, or the language does it for you.
Java is a tiny bit less strict in its rules for invoking the base class constructor. But it still has to be executed.
So it calls the superclass's constructor automatically or what? if so, how does it know the parameters?

I always thought that the superclass's constructor is not executed unless you call super() in the subclass's constructor.

Quote:
Originally Posted by johnsfine View Post
It isn't exactly the same thing. In more advanced programming, the power of references that cannot be duplicated by pointers comes into play.

Even in simple programming, the two different syntaxes for one behavior are a useful choice. Do you think C, Java, etc. should not have for loops because the meaning is the same as a while with a couple extra lines? Should C not have X[n] syntax because the meaning is the same as *(X+n)?
Maybe it will make sense, I probably haven't got to that level yet.
 
Old 12-07-2009, 04:07 PM   #10
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,046

Rep: Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100
Quote:
Originally Posted by MTK358 View Post
So it calls the superclass's constructor automatically
Yes. See 8.8.7 in Java Language specification, especially the sentence starting "If a constructor body does not begin with an explicit constructor invocation".

http://java.sun.com/docs/books/jls/t...ses.html#8.8.7

It appears something I used and appreciated long ago in Java was incorrect. So Java is not less strict than C++ about the base class constructor.

It was very convenient in Java to call super() after doing some initial processing of the calling arguments of the current constructor, even selecting an overload of super() at run time inside the current constructor based on argument values (not types). I have wished for that feature in C++.

But Java the language specification says you can't do that, so I guess the Java implementation I was using when I was able to do a little work before calling super() was wrong.

The semantic rules for calling the base class constructor are much more similar between C++ and Java than I thought. The syntax is not similar.

Last edited by johnsfine; 12-07-2009 at 04:09 PM.
 
Old 12-07-2009, 04:41 PM   #11
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Original Poster
Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
If it is called automatically, how does it know what parameters to use if there isn't a no-parameter constructor for the base class?
 
Old 12-07-2009, 04:52 PM   #12
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,962
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
And off of the java slant this is taking: have you considered
"Objective C"?


Not that I know much about it ...
 
Old 12-07-2009, 05:06 PM   #13
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Original Poster
Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
It's highly dependent on this nextstep/openstep/gnustep stuff, I don't understand that. Maybe it's OK.
 
Old 12-07-2009, 05:07 PM   #14
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by MTK358 View Post
It still seems kind of strange to have two ways of doing exactly the same thing.
The two are quite a bit different actually, although the underlying structure is essentially identical.

You can nest pointers as far as you want (*******) but you can't do so with references; a reference modifier can only be used once, and it must come last. References can never change (using defined behavior, anyway,) which is why they can be used as if they were an object. References require either a valid object or a dereference (good or bad) to create, whereas a pointer isn't initialized automatically, meaning it could point anywhere. This means a reference is in general more valid, although the object it points to can disappear, just like with a pointer. Other than that, you really need to perform a bad cast or dereference a bad pointer to get a bad reference. Lastly, a bad dynamic_cast of a pointer returns NULL, but it causes an exception with a bad reference cast.
Kevin Barry

Last edited by ta0kira; 12-07-2009 at 05:09 PM.
 
Old 12-08-2009, 03:38 AM   #15
rizwanrafique
Member
 
Registered: Jul 2006
Distribution: Debian, Ubuntu, openSUSE, CentOS
Posts: 147

Rep: Reputation: 19
Quote:
Originally Posted by MTK358 View Post
If it is called automatically, how does it know what parameters to use if there isn't a no-parameter constructor for the base class?
Can't remember exactly how C++ doesn now but it's an OO principle so I'll be very surprised if it different across languages.

Only "default" constructors are called automatically. That is, constructors with no parameters. If don't provide an implementation of such a constructor the compiler creates one for you if there is no other constructor available for that class. If you do provide an implementation but don't invoke the paren't default constructor then one is invoked for you. And that is always the first call in child's constructor.

If there is a constructor with parameters in parent but you haven't invoked it explicitly then compiler will complain that it can't find a default constructor and you haven't invoked one manually. This mind you is "exactly" how java behaves. As I said, I haven't tested it in C++ in a long time but I'll be very surprised if its different in principle (except some implementation details).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MailScanner alternatives cygnus-x1 Linux - Networking 1 11-28-2006 12:28 AM
trying to find alternatives acey99 Linux - Hardware 1 11-24-2006 10:03 AM
Vi alternatives Gins Linux - General 9 09-05-2005 10:29 AM
Alternatives to x? ampex189 Linux - Software 9 03-22-2005 01:21 PM
Alternatives matahchuah Linux - Software 1 02-17-2004 05:08 PM


All times are GMT -5. The time now is 10:49 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration