April 2, 2007 by typedef
Overloading
Having two methods that have the same name but different signatures. That is, different parameters or return values. These two methods would be in the same scope - such as part of the same class.
ex:
void serve(int amount);
int serve(int amount, int numPersons);
Overriding
Overriding happens when an sub-class has a different implementation of a method than its inherited class. This is useful because a class inherits methods from its parent but there are often times the class needs to modify the behavior of the base method. The two methods have the same name and signature. One implementation in the base-class and one in the sub-class.
Posted in Languages | No Comments »
March 30, 2007 by typedef
Summarized from: http://www.computing.dcu.ie/~renaat/projects/cvjava.html
- Java is slower. This seems to be the major sticking point for some people.
- Everything is in a class. No global functions or global data. No structs, enumerations, or unions.
- You wouldn’t have a header file with a class declaration. Functions are defined in the class.
- No closing semicolon for Java classes.
- No scope resolution operator (::). Java uses the dot operator for just about everything.
- Static method invocation is ClassName.MethodName();
- Java has primitive types:
- boolean
- byte
- char (16-bit)
- byte
- short
- int
- long
- float
- double
- Type checking is much stricter in Java. For example, conditionals must be boolean and not integral.
- No static character array strings. Everything converted to String objects
- >> arithmetic shift and >>> as logical shift
- all non-primitive objects are created using ‘new’
- No forward declarations are necessary
- No pointers such as in C++. But creating an object returns to you a reference, a restricted pointer.
- NO DESTRUCTORS. No need to call delete. Lifetime is not really determined by scope but by garbage collection.
- Java Does not support default arguments
- No ‘goto’ for unconditional jumps, but there is ‘break’ and there is ‘continue’
- Everything inherits from the base class Object… thus you end up with a tree instead of a forest like in C++
- Java has no templates. But Vector, Hash, and Stack hold Objects.
- Multithreading support through Thread class
- public, private, (protected not available in Java) are placed on each definition with the default being friendly.
- Inner class objects hold references to their outer class
- No inline functions but final functions act similarly
- ‘extends’ to indicate parent class and ’super’ to refer to the parent class’ methods
- Cannot specify the protection level of the base class
- Overridden methods cannot reduce protection of the original
- Java provides ‘interface’ which is a class with no members and no method definitions. Instead of extending, a class would ‘implement’ an interface. An interface cannot become an object. An ‘abstract’ class is similar but can have method definitions/implementations.
- No virtual keyword, all non-static methods use dynamic binding.
- No multiple inheritance. But one can use a combination of ‘extend’ and ‘implements’ (interfaces)
- Since there are no destructors, a ‘finally’ clause can be used in conjunction with a throw/catch exception handling block to perform cleanup
- Method overloading but no operator overloading
- Since objects are passed by reference there is a ‘clone()’ method to copy-by-value.
- constants are declared as static final
Posted in Languages | No Comments »
March 28, 2007 by typedef
Here are some things that C++ can do better than C, taken from my notes on Bjarne Stroupstrup’s “The C++ Programming Language”.
- Macros are no longer needed. Constants are defined by const or enum.
- Templates are useful to specify a family of function
- Namespaces are useful to avoid name clashes
- Declarations can happen anywhere in a function, not just at the top
- Dynamic memory management is simpler using classes such as vector or constructs like ‘new’ instead of ‘malloc()’
- Casts are seldom needed but in the rare cases they are, we have static_cast and reinterpret_cast
- The string class and vector classes will make you rejoice and loathe C-strings and arrays
- Stop crashing and catch some errors using throw-catch error handling
Some “silent” differences:
- A C function declaration with no parameters can take any number of arguments of any type. Not C++
- C function headers do not have to specify argument types and they can be placed optionally after the list of arguments. Not C++
- In C, when no type is specified, the type is assumed or defaulted to type int. Not in C++
Posted in Languages | No Comments »
March 25, 2007 by typedef
C++ was created to become a better C - to overcome its weaknesses. Bjarne Stroupstrup describes it as a general-purpose programming language that supports data abstraction, object-oriented programming, and generic programming.
Notice that it supports object-oriented programming. It does not force the user to use that technique. In fact, most programs (or all) written in C will be able to compile under a C++ compiler.
Well, is C++ Slower?
This seems to be the main argument of nay-sayers. That, and the C++ advancements add overhead. From my research, it’s not the case that any equivalent C program would run any faster than its C++ counterpart. Plus, the complexity of the program plays a factor, too. The fact is, compilers are getting better, programmers are enjoying the C++ enhancements, people care about other things besides performance, and the performance difference, if any, isn’t concerning.
What else do people care about besides performance?
- Shorter Code - additions like parameterized types make code shorter
- Development Time - shorter code, less to write, less time needed for development
- Extendibility - data abstraction and object-oriented programming can make your life easier when it’s time to do version 2 of your great program.
Some Other Differences
- Dynamic memory allocation is done differently (new/delete vs. malloc/free)
- C++ has a stronger standard library including templates, vectors, and strings.
- Exception handling
Posted in Languages | No Comments »