Labor für Technische Informatik Praktikum VSP KLC/SLZ Comparison between C/C++ and Java C/C++ Java #include <iostream> #include <stdlib.h> #include <time.h> import java.util.Date; using namespace std; // for cout,endl etc. public class LapTimer { private int clickCount; class LapTimer { private: int clickCount; public: LapTimer() { clickCount = 0; } public LapTimer() { clickCount = 0; } public String click() { Date tm = new Date(); void click(char* cbuffer) { time_t t_now; // Formatting numbers and // chars into a string return String.format( "Time: %02d:%02d:%02d", tm.getHours(), tm.getMinutes (), tm.getSeconds()); time(&t_now); struct tm* lt_now = localtime(&t_now); // Formatting numbers and // chars into a (C-) string sprintf(cbuffer, \ "Time: %02d:%02d:%02d", \ lt_now->tm_hour, lt_now->tm_min, lt_now->tm_sec); } public static void main(String[] args) { int iMax = 5; String cbuffer1 = null; } }; int main(int argc, char *argv[]) { // ^-length ^-argument array int iMax = 5; char cbuffer1[255]; if (args.length > 1) iMax = Integer.parseInt(args[0]); // args[0] is first parameter LapTimer myLapTimer = new LapTimer(); if (argc > 1) iMax = atoi(argv[1]); // argv[1] is first parameter for (int i=0; i<iMax; i++) { try { // milliseconds (!) Thread.sleep(1000);} catch (InterruptedException e){} LapTimer* myLapTimer = new LapTimer(); for (int i=0; i<iMax; i++) { sleep(1); // seconds (!) myLapTimer->click(cbuffer1); cout << cbuffer1 << endl; } } return 0; } } cbuffer1 = myLapTimer.click(); System.out.println(cbuffer1); System.exit(0); } Remarks: • getHours(), getMinutes() and getSeconds() from the Java class Date are deprecated. They are merely used here for better comparison to C. • Declarations and Definitions are normally written to individual files, i.e. header files (*.h ) and source files *.cpp) in C++. See example overpages. • Documentation on C und C++ library functions (time(), sprintf() etc.) can be found in the man-pages. Some of the functions used here are originally C functions. They are also available in C++. A more detailed introduction to C++ can be obtained here. Labor für Technische Informatik Praktikum VSP Breaking down the sample into header and source files Header file (LapTimer.h): #ifndef LAPTIMER_H_ #define LAPTIMER_H_ class LapTimer { public: LapTimer(); virtual ~LapTimer(); void click(char* cbuffer); }; #endif /* LAPTIMER_H_ */ Source file (LapTimer.cpp): #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; // for cout,endl etc class LapTimer { private: int clickCount; public: LapTimer() { clickCount = 0; } void click(char* cbuffer) { time_t t_now; time(&t_now); struct tm* lt_now = localtime(&t_now); // Formatting numbers and chars into a (C-) string sprintf(cbuffer, "Time: %02d:%02d:%02d", \ lt_now->tm_hour, lt_now->tm_min, lt_now->tm_sec); } }; int main(int argc, char *argv[]) { int iMax = 5; char cbuffer1[255]; // length and argument array if (argc > 1) iMax = atoi(argv[1]); // argv[1] is first parameter LapTimer* myLapTimer = new LapTimer(); for (int i=0; i<iMax; i++) { sleep(1); // seconds (!) myLapTimer->click(cbuffer1); cout << cbuffer1 << endl; } return 0; } KLC/SLZ