Probleme beim Umschreiben von C in C++ Code

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Probleme beim Umschreiben von C in C++ Code

    Mein Vorhaben ist, wie in der Ueberschrift erwaehnt, meinen C Code in C++ Code umzuschreiben. Ist eine einmalige Sache, aber mein Unwissen in C++ hilft mir da nicht gerade. Koennte vielleicht jemand so freundlich sein und mir ein wenig helfen?
    Hier mal einen der Codeabschnitte:

    C-Quellcode

    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include "date.h"
    4. mdate create_d(unsigned day, unsigned month, unsigned year)
    5. {
    6. //mdate* date = (mdate*)malloc(sizeof(mdate));
    7. mdate date;
    8. date.day=day;
    9. date.month=month;
    10. date.year=year;
    11. return date;
    12. }
    13. mdate copy_d(mdate d)
    14. {
    15. return create_d(d.day,d.month,d.year);
    16. }
    17. int datecmp( const mdate *d1, const mdate *d2)
    18. {
    19. if (d1->year < d2->year)
    20. return -1;
    21. if (d1->year > d2->year)
    22. return 1;
    23. //year is equal
    24. if (d1->month < d2->month)
    25. return -1;
    26. if (d1->month > d2->month)
    27. return 1;
    28. //year and month are equal
    29. if (d1->day < d2->day)
    30. return -1;
    31. if (d1->day > d2->day)
    32. return 1;
    33. //the dates are equal
    34. return 0;
    35. }
    36. void print_d(mdate d)
    37. {
    38. printf("%u.%u.%u",d.day,d.month,d.year);
    39. }
    Alles anzeigen


    und hier noch eine Headerdatei:

    Quellcode

    1. #ifndef __DATE_H__
    2. #define __DATE_H__
    3. /* a struct to represent a date! of course there is an existing implementation in the C standard :)
    4. * */
    5. struct date_{
    6. unsigned year;
    7. unsigned month;
    8. unsigned day;
    9. } ;
    10. typedef struct date_ mdate;
    11. /* creates a date, currently their is no check, whether the given date is valid; 31.02.2000 would make no sense :)
    12. * */
    13. mdate create_d(unsigned day, unsigned month, unsigned year);
    14. mdate copy_d(mdate d);
    15. /*allows to compare two dates, the function returns
    16. * 0 if the dates are equal
    17. * 1 if d1 > d2
    18. * -1 otherwise
    19. * */
    20. int datecmp(const mdate *d1, const mdate *d2);
    21. /*prints the date to stdout day.month.year*/
    22. void print_d(mdate d);
    23. #endif
    Alles anzeigen
  • Mein urspruengliches Programm speichert und loescht daten in/aus einem array, eine sortierfunktion nach namen und Geburtjahr wurde implementiert. Ich dachte ich poste erstmal nur diesen Abschnitt. Ich will das Programm so umschreiben, dass ich es in C++ in identischer weise nutzen kann. I
  • Umschreiben von C nach C++
    reicht es, wenn der Compiler das ohne Schluckauf frisst?
    Sollen minimale weitergehende Änderungen gemacht werden? Beispiel: Header, oder in dem Quelltext -> printf()
    Oder soll noch weiter C++ Stilmittel genutzt werden? Für Letzteres denke ich ist dein gezeigter Quelltext nicht ausreichend.

    Zeig uns mal wie du das angehst und wo deine Schwierigkeiten sind - d.h. wir wollen Quelltext der schon bearbeitet ist sehen :!:

    MfG bcc-fan