next up previous
Next: A short C++/OO tutorial: Up: OO Previous: OO

OO FAQ

Toggle Background


  1. Why should I use object orientation?
    Object oriented programming techniques provide you with a higher level of abstraction. Coding becomes more powerful. Programs become more readable.

  2. What exactly is object orientation?
    Consider the following example: You want to implement a matrix multiplication. Unfortunately, there is no builtin type "Matrix" in your language (there is no builtin type "Matrix" in C++ either). What can you do? A typical FORTRAN solution would be:
      REAL A, B, C
      INTEGER AROWS, ACOLS, BROWS, BCOLS, CROWS, CCOLS
      DIMENSION A(AROWS, ACOLS), B(BROWS, BCOLS), C(CROWS, CCOLS)
      call matmult(A, B, C, AROWS, ACOLS, BROWS, BCOLS, CROWS, CCOLS)
    
    You might ask: "What is wrong with that?" There is nothing wrong but it is ugly and not optimal with respect to coding efficiency. So what is the problem? The problem is that you have to know about the internal representation of a matrix at any time you use it. As a consequence you have to keep your mind on details which are far below the abstraction level you usually think at. If you look into typical scientific program packages (FORTRAN) you will most likely find subroutine calls with very long lists of parameters (I have seen a hundred parameters). This is a typical symptom of a lack of abstraction. You might say: "But I was told to use parameters instead of common blocks!" If you have nothing better this is true: You should use parameter passing instead common blocks. However, having a hundred parameters in one subroutine call is not really funny. Have you ever tried to understand such a program later?

    Polemically speaking one might say that the highest level of abstraction FORTRAN provides is that of the basic arithmetic operations (+,-,*,/), functions and numbers. This is much too less. Theoretical physics and mathematics are in fact a playground for high level abstractions. If you use FORTRAN you restrict yourself to a pocket calculator.

    Turning back to the above example what you really meant is:

    Matrix A, B, C;
    	C = A*B;
    
    Exactly this is possible with C++. Now you might argue that FORTRAN90 provides you with the type "Matrix" and you can do something similar. That might be true. But the point is: FORTRAN90 does not provide you with the power to implement and integrate anything new as if it were an intrinsic element of the language. In other words: Within C++ there are hardly any intrinsic high level data types but C++ provides you with a very powerful grammar enabling you to integrate whatever you can think of. There is much more about object orientation but this is at the heart of it.

    Finally, let me show another example from coupled cluster:

    Expression	T = ClusterOp(1) + ClusterOp(2) + ClusterOp(3);	// T_1 + T_2 + T_3
    Expression	Heff;
    	Heff = exp(-T)*H*exp(T);
    
    This really works... You can try it out here.

  3. Why C++?
    C++ does not perfectly fulfill the object oriented paradigm. However, it is the native language of UNIX and provides a basis for writing close to machine (efficient) but machine independent software. Major benefits of C++ are:
    1. Type safeness
    2. High expressive power
    3. "Total freedom" if you wish (You may do anything you like. Including "dirty tricks".)
    4. "by professionals for professionals". That is: Full power, full risk...
    5. Industrial standard

  4. Why not C# or Java?
    C# and Java both miss the "full freedom" you have with C++. This may be an advantage if you want to implement business applications (where errors may be rather catastrophic) but it may be at the same time a serious limitation with respect to high performance "on the edge" scientific programming. Furthermore, in the beginning these languages missed support for "templates" (that are parametric types) which are rather powerful. In the meantime templates are very well settled and mature within C++.

  5. I heard C++ is slow. Is this true?
    First we recall from theoretical computer science that there is no language faster than another. You need a certain (very tiny) set of elementary operations (such as loops, conditionals, variables, assignments) and you may implement any algorithm you can think of. Speed is a question of your algorithm and the compiler. The language determines the level of convenience for the programmer. However, there may be performance issues if you do not know what you do. There are two major causes of inefficient programs:
    1. You make excessive use of virtual functions within an inner loop.
    2. You excessively copy large objects.
    The former is most likely a flaw in your object design. You should think about using static (templates) instead of dynamic polymorphism. The latter may most easily be eliminated by using call by const references ("xyz(const XYZ &)") or (encapsulated) pointers.

  6. Some people call programming with C++ pathetic. Is that true?
    A C++ program typically consists of many declarative statements. This is necessary because it tells the compiler (and any human being (at later time including yourself)) what your self-defined objects are and how they can be used. A declarative statement actually produces no code.

    Declarative statements in FORTRAN are for example

      INTEGER a
      DIMENSION a(10)
    
    Declarative statements in C++ for the above Matrix example could be
    class Matrix {
    	Matrix operator * (const Matrix &) const;
    ...
    };
    
    This actually does nothing but tells the compiler (and anyone else) that two matrices may be multiplied returning a new Matrix.

  7. Is object orientation a substitution for algorithmic intelligence?
    No, it is definitively not! If your program is too slow you should first think about your algorithm and than about the compiler. Not about your language.

  8. Are there any drawbacks from using C++?
    There are a few drawbacks:
    1. The grammar is rather complex. Learning (and understanding) C++ takes more time than learning FORTRAN.
    2. Designing objects needs some experience and a deep insight to the problem to be implemented. The object design is crucial for a project and has do be done at the beginning before any line of code is produced. There are tools to do this (e. g. UML).
    3. For large projects and deeply nested templates compile time (not run time) may become an issue.

  9. What about interoperability with existing FORTRAN code or libraries?
    You may call any library (including high performance numerical packages like LAPACK and BLAS) from a C++ program. However, I think a "soft" migration from an existing completely procedural (FORTRAN) code does not make sense. You should enter the design phase and implement the major part from scratch. This is not due to the lack of interoperability of C++ but because you should switch to the object paradigm completely (and after knowing and understanding the object oriented paradigm you will probably prefer to do so yourself).

  10. What about the object oriented paradigm in economy and professional computer companies?
    The object oriented paradigm is widely used in economy. Most software companies use the object oriented paradigm or even design an object oriented language based on C++ (Sun: Java, Microsoft: C#). There are special reasons (restricted "freedom" disabling you to do something really stupid creating an insecure program) why they designed there own language. Many large applications are written in an object oriented language (Microsoft Office, Linux KDE environment, computer games etc.).


next up previous
Next: A short C++/OO tutorial: Up: OO Previous: OO
Michael Hanrath 2006-05-02