[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: OT: C++ Newbie and KDE/QT



On Tue, Jun 26, 2001 at 10:12:56PM +0200, Brendon wrote:
| On Tuesday 26 June 2001 19:14, D-Man wrote:
| > On Tue, Jun 26, 2001 at 12:06:50PM +0200, Brendon wrote:
| > | Does anyone know of a good site where QT/KDE programming is explained?
| > | And what did you start with when learning C++?
| >
| > I don't use KDE and I don't like Qt's LnF so I don't know about that
| > part.  As for learning C++ -- it is big and complicated and the little
| > details will get you.  I would recommend starting out with an easier
| > language to get the basics of programming down first.  Then move into
| > C++ once you understand how to program.  I highly recommend Python as
| > an easy, powerful, and clean language to learn.  It also allows you to
| > choose the most appropriate paradigm -- you can start out procedurally
| > (simpler) and move into OO (class-based) when you are ready for it.
| > Alan Gauld has an excellent tutorial for beginners at
| > http://www.crosswinds.net/~agauld.  Python also has bindings to Qt
| > (PyQt) so you can do Qt/KDE programming using Python and forget about
| > the headaches that C++ can give you.  If you decide to try python,
| > check out the tutor mailing list, it is very helpful
| > (tutor@python.org, http://mail.python.org/mailman/listinfo/tutor).
| 
| i'll check it out. just another question, what advantages and disadvantages 
| does python have over C++? Also, is C++ really that complicated to learn? In 

Python is dynamically typed.  This means you don't need to type (with
the keyboard, that is) the type (kind) that each identifier will be.
The syntax is clean and really easy to read.  Pointers and memory
management is taken care of for you.  You can use the interactive
interpreter to try small snippets of code and see the result
immediately.  High-level concepts such as lists and dicts are built in
to the language.  A list is like an array that can hold different
kinds of things and will resize when needed.  A dict is also called a
hashtable, map and associative array.  They are _very_ useful.  Also
in Python functions and classes are first class objects.  This
combined with the dynamic typing makes using callbacks (very typical
in GUI programming) quite easy and doesn't involved any extra
complicated syntax.  As a side effect of this many things that are
non-trivial in C or C++ (or even Java) take so few lines of Python
code that it is almost ridiculous.  (For example quite a few Design
Patterns in the GoF book are trivial or built-in to Python while they
have non-trivial C++/Java implementations)

C++ has things like templates, pointers, no built-in lists or dicts.
Also functions are not first class objects.  To handle callbacks a
pointer to a function must be used.  This, combined with static type
checking, makes callbacks harder to use and harder to read.  In C++
you must also deal with memory management.  If you don't "delete"
objects then you have a memory leak.  If you delete it too soon you
will get a segmentation fault later on (if you're lucky!).  Also be
careful not to run off the end of an array, especially while writing
to it.

The other major difference is that Python will give you a stack trace
when you have an error, along with a message telling you what the
error was.  With a compiled language like C or C++,
"Segmentation Fault - core dumped" is all the information you will
get.  Then you need to load the program and core dump into a debugger
to examine the stack trace.  Also with Python the dev cycle is
edit-run.  With C and C++ it is edit-compile-run-load in debugger.

| the learning process i haven't run into any problems (except understanding 
| pointers.. kept on getting confused because of the different uses of  * ). 
| Though when looking at the source of Konverse, i was a little overwhelmed. 
| But I presumed this was because i didnt know how to work with QT.
| 
| > You may also want to try Java.  It is basically C++ with pointers,
| > manual memory management, and freestanding functions removed.  Also
| > the class definition conicides with the declaration (not in 2 separate
| > files).  Its syntax is nearly identical, yet it simplifies quite a few
| > things.  IMO Python is much better designed, much easier to use, and
| > more powerful (and flexible) than Java.
| 
| But also awfully slow was it not? 

Take a look at this report that was in IEEE Computer magazine about a
year ago :
    http://wwwipd.ira.uka.de/%7Eprechelt/documents/jccpp_tr.pdf


The key to it is not that C is faster than Python, or something like
that.  The speed of an application is more dependent on the
programmer's skill than it is on the language.  Sure, each Python
statement is slower that a C or C++ statement.  This is obvious since
Python is implemented in C, thus takes several C statements for a
single Python statement.  If you talk about Jython, then Python is
implemented in Java which is implemented in C.  This is all irrelevant
though.  80% of the execution time is spent in 20% of the code, maybe
less.  So if most of the program is "slow", it doesn't matter as long
as the right parts are fast.  Also the majority of GUI programs sit
around doing nothing waiting for the user to click a button.  The user
is much slower than any programming language implementation.

I could also claim that C is slow -- it takes many times longer for
_any_ software to execute than for hardware to operate (think of
implementing the operation in hardware instead).  To say that
"language X is slower than language Y" has no meaning.  The real
question is "is this app TOO slow?".  That can only be determined by
building and running the app.  Also, experienced programmers will tell
you that you never know where the most time is spent -- it is always
where you least expect it.  Don't optimize prematurely, run the
program using a profiler to find out where the hot spots are first.

Once you find those hot spots, you may decide that you can't make the
python code any faster.  Then you write a C or C++ extension module to
implement that part of the data crunching a little faster.  Then you
get the best of both worlds -- you get the ease of use and maintenance
that Python provides while getting the compiler optimizations of C or
C++ in the (small) speed critical sections.

You may have been asking about Java in your question.  Java is in
between C/C++ and Python for execution speed, but uses hordes of
memory.  The garbage collector is getting better in new JVMs though.
One advantage C, C++, and Java have over Python as far as execution
speed is concerned is their inflexibility (also called static type
checking).  The compiler knows beforehand exactly what objects you have,
where in the object a particular piece of data is and how big the
object needs to be.  Many things in C and C++ are computed offsets.
Array indexing is one such example.  Also the compiler can make
assumptions about your data because you must tell it, for example,
this is an 'int'.  With Python everything is dynamic.  The compiler
knows very little about the data at compile time, thus the runtime
must work extra hard.

| I suppose Python -> Java -> C++ would be the right order to learn,
| considering the apparent ease of python, demand for java and
| complexity of C++. Any comments?

If you want to learn them all, then yes that's a good order.  Many
people find that once they know python they no longer want to use C++
or Java :-).  I'm one of those.  I don't mind C, C++ or Java iff I am
doing some low-level or speed critical code.  I don't want to write an
entire application, and certainly not a GUI in those languages.

Java is in between C++ and Python.  It has much of the feel and
operation of C++ (without pointers, 'delete', templates) but it is
bytecode-compiled and then interpreted.  It gives you a stacktrace
when you screw something up, like walk off the end of an array.  It
tries to be a high level language, but it feels much lower level.


The important issues when choosing a programming language are :

    o  does it suit the problem space well -- perl and python are better for
       string processing than C

    o  what is the development time?  Many people will say that
       development time using a high level language (such as Python)
       is much lower than using a low level language (such as C or
       C++).  You also get fewer obscure bugs such as memory leaks,
       etc.

    o  what is the ease of use of the platform?

Pick a language that suits what you want to do.  I find that python
works very well in most cases.

I hope I didn't ramble too much and good luck with your programming,
-D



Reply to: