dburyak

C/C++ overloading

Jul 8th, 2021
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include <cstdio>
  2.  
  3. void printMe(int i) {
  4.   printf("%d\n", i);
  5. }
  6.  
  7. void printMe(const char * str) {
  8.   printf("%s\n", str);
  9. }
  10.  
  11. typedef struct person_s {
  12.   char const * firstName;
  13.   char const * lastName;
  14.   int yearOfBirth;
  15. } person_t;
  16.  
  17. typedef struct book_s {
  18.   char const * title;
  19.   person_s const * author;
  20.   int numOfPages;
  21. } book_t;
  22.  
  23. void printMe(person_t const * person) {
  24.   printf("person.firstName   : %s\n", person->firstName);
  25.   printf("person.lastName    : %s\n", person->lastName);
  26.   printf("person.yearOfBirth : %d\n", person->yearOfBirth);
  27. }
  28.  
  29. void printMe(book_s const * book) {
  30.   printf("book.title      : %s\n", book->title);
  31.   printf("book.numOfPages : %d\n", book->numOfPages);
  32.   printf("book.author     : \n");
  33.   printMe(book->author);
  34. }
  35.  
  36. // -----------------
  37. int main() {
  38.   printMe(42);
  39.   printMe("hello");
  40.  
  41.   person_t johnDoe = { .firstName = "John", .lastName = "Doe",
  42.     .yearOfBirth = 1900 };
  43.   printMe(&johnDoe);
  44.  
  45.   person_t melville = { .firstName = "Herman", .lastName = "Melville",
  46.     .yearOfBirth = 1819 };
  47.   book_t mobyDick = { .title = "Moby Dick", .author = &melville,
  48.     .numOfPages = 500 };
  49.   printMe(&mobyDick);
  50.  
  51.   return 0;
  52. }
  53.  
Add Comment
Please, Sign In to add comment