Skip to content
Snippets Groups Projects
Commit fd0e26bf authored by Peter Gerwinski's avatar Peter Gerwinski
Browse files

Beispiele 30.3.2023

parent 1e54b385
No related branches found
No related tags found
No related merge requests found
Showing
with 588 additions and 9 deletions
#include <stdio.h>
void print (const char *s)
{
printf ("%s", s);
}
void print (int i)
{
printf ("%d", i);
}
int main ()
{
print ("Hello, world!\n");
print (42);
print ();
}
#include <stdio.h>
hello ()
{
printf ("Hello, world!\n");
return 42;
}
main ()
{
hello ("Wörldt");
return 0;
}
No preview for this file type
...@@ -442,16 +442,19 @@ ...@@ -442,16 +442,19 @@
\lstinline{class}: standardmäßig \lstinline{private} \lstinline{class}: standardmäßig \lstinline{private}
\medskip \medskip
\item \item
\lstinline{friend}-Funktionen und -Klassen \dots
\medskip % \lstinline{friend}-Funktionen und -Klassen
\item % \medskip
Klasse als Namensraum:\\ % \item
\lstinline{static}-"`Member"'-Variable\\ % Klasse als Namensraum:\\
\lstinline{static}-"`Methoden"'\\ % \lstinline{static}-"`Member"'-Variable\\
Deklarationen von z.\,B.\ Konstanten und Typen % \lstinline{static}-"`Methoden"'\\
% Deklarationen von z.\,B.\ Konstanten und Typen
\end{itemize} \end{itemize}
\end{frame} \end{frame}
\iffalse
\addtocounter{subsection}{-1} \addtocounter{subsection}{-1}
\subsection{Objekte: Konstruktoren und Destruktoren} \subsection{Objekte: Konstruktoren und Destruktoren}
...@@ -491,8 +494,6 @@ ...@@ -491,8 +494,6 @@
\end{itemize} \end{itemize}
\end{frame} \end{frame}
\iffalse
\subsection{Templates} \subsection{Templates}
\begin{frame}[fragile] \begin{frame}[fragile]
......
#include <stdio.h>
#define n 5
int prime[n] = { 2, 3, 5, 7, 11 };
int main (void)
{
for (int i = 0; i < n; i++)
printf ("%d\n", prime[i]);
return 0;
}
#include <stdio.h>
const int n = 5;
int prime[n] = { 2, 3, 5, 7, 11 };
int main (void)
{
for (int i = 0; i < n; i++)
printf ("%d\n", prime[i]);
return 0;
}
#include <stdio.h>
const int n = 5;
int main (void)
{
printf ("n = %d\n", n);
n = 6;
printf ("n = %d\n", n);
return 0;
}
#include <stdio.h>
const int n = 5;
int main (void)
{
printf ("n = %d\n", n);
int *p = &n;
*p = 6;
printf ("n = %d\n", n);
return 0;
}
#include <stdio.h>
const int n = 5;
int main (void)
{
printf ("n = %d\n", n);
const int *p = &n;
*p = 6;
printf ("n = %d\n", n);
return 0;
}
#include <stdio.h>
typedef struct
{
int type;
} t_base;
typedef struct
{
int type;
int content;
} t_integer;
typedef struct
{
int type;
char *content;
} t_string;
int main (void)
{
t_integer i = { 1, 42 };
t_string s = { 2, "Hello, world!" };
t_base *object[] = { &i, &s };
return 0;
}
#include <stdio.h>
typedef struct
{
int type;
} t_base;
typedef struct
{
int type;
int content;
} t_integer;
typedef struct
{
int type;
char *content;
} t_string;
int main (void)
{
t_integer i = { 1, 42 };
t_string s = { 2, "Hello, world!" };
t_base *object[] = { (t_base *) &i, (t_base *) &s };
return 0;
}
#include <stdio.h>
typedef struct
{
int type;
} t_base;
typedef struct
{
int type;
int content;
} t_integer;
typedef struct
{
int type;
char *content;
} t_string;
int main (void)
{
t_integer i = { 1, 42 };
t_string s = { 2, "Hello, world!" };
t_base *object[] = { (t_base *) &i, (t_base *) &s };
for (int i = 0; i < 2; i++)
if (object[i]->type == 1)
printf ("Integer: %d\n", object[i]->content);
else if (object[i]->type == 2)
printf ("String: \"%s\"\n", object[i]->content);
return 0;
}
#include <stdio.h>
typedef struct
{
int type;
} t_base;
typedef struct
{
int type;
int content;
} t_integer;
typedef struct
{
int type;
char *content;
} t_string;
int main (void)
{
t_integer i = { 1, 42 };
t_string s = { 2, "Hello, world!" };
t_base *object[] = { (t_base *) &i, (t_base *) &s };
for (int i = 0; i < 2; i++)
if (object[i]->type == 1)
printf ("Integer: %d\n", (t_integer *) object[i]->content);
else if (object[i]->type == 2)
printf ("String: \"%s\"\n", (t_string *) object[i]->content);
return 0;
}
#include <stdio.h>
typedef struct
{
int type;
} t_base;
typedef struct
{
int type;
int content;
} t_integer;
typedef struct
{
int type;
char *content;
} t_string;
int main (void)
{
t_integer i = { 1, 42 };
t_string s = { 2, "Hello, world!" };
t_base *object[] = { (t_base *) &i, (t_base *) &s };
for (int i = 0; i < 2; i++)
if (object[i]->type == 1)
printf ("Integer: %d\n", ((t_integer *) object[i])->content);
else if (object[i]->type == 2)
printf ("String: \"%s\"\n", ((t_string *) object[i])->content);
return 0;
}
#include <stdio.h>
typedef struct
{
int type;
} t_base;
typedef struct
{
int type;
int content;
} t_integer;
typedef struct
{
int type;
char *content;
} t_string;
void print_object (t_base *this)
{
if (this->type == 1)
printf ("Integer: %d\n", ((t_integer *) this)->content);
else if (this->type == 2)
printf ("String: \"%s\"\n", ((t_string *) this)->content);
}
int main (void)
{
t_integer i = { 1, 42 };
t_string s = { 2, "Hello, world!" };
t_base *object[] = { (t_base *) &i, (t_base *) &s };
for (int i = 0; i < 2; i++)
print_object (object[i]);
return 0;
}
#include <stdio.h>
#define T_BASE 0
#define T_INTEGER 1
#define T_STRING 2
typedef struct
{
int type;
} t_base;
typedef struct
{
int type;
int content;
} t_integer;
typedef struct
{
int type;
char *content;
} t_string;
void print_object (t_base *this)
{
if (this->type == T_INTEGER)
printf ("Integer: %d\n", ((t_integer *) this)->content);
else if (this->type == T_STRING)
printf ("String: \"%s\"\n", ((t_string *) this)->content);
}
int main (void)
{
t_integer i = { T_INTEGER, 42 };
t_string s = { T_STRING, "Hello, world!" };
t_base *object[] = { (t_base *) &i, (t_base *) &s, NULL };
for (int i = 0; object[i]; i++)
print_object (object[i]);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define T_BASE 0
#define T_INTEGER 1
#define T_STRING 2
typedef struct
{
int type;
} t_base;
typedef struct
{
int type;
int content;
} t_integer;
typedef struct
{
int type;
char *content;
} t_string;
void print_object (t_base *this)
{
if (this->type == T_INTEGER)
printf ("Integer: %d\n", ((t_integer *) this)->content);
else if (this->type == T_STRING)
printf ("String: \"%s\"\n", ((t_string *) this)->content);
}
t_integer *new_integer (int i)
{
t_integer *p = malloc (sizeof (t_integer));
p->type = T_INTEGER;
p->content = i;
return p;
}
t_string *new_string (char *s)
{
t_string *p = malloc (sizeof (t_string));
p->type = T_STRING;
p->content = s;
return p;
}
int main (void)
{
t_base *object[] = { (t_base *) new_integer (42),
(t_base *) new_string ("Hello, world!"),
NULL };
for (int i = 0; object[i]; i++)
print_object (object[i]);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define T_BASE 0
#define T_INTEGER 1
#define T_STRING 2
typedef struct
{
int type;
} t_base;
typedef struct
{
int type;
int content;
} t_integer;
typedef struct
{
int type;
char *content;
} t_string;
typedef union
{
t_base base;
t_integer integer;
t_string string;
} t_object;
void print_object (t_object *this)
{
if (this->base.type == T_INTEGER)
printf ("Integer: %d\n", this->integer.content);
else if (this->base.type == T_STRING)
printf ("String: \"%s\"\n", this->string.content);
}
t_object *new_integer (int i)
{
t_object *p = malloc (sizeof (t_integer));
p->integer.type = T_INTEGER;
p->integer.content = i;
return p;
}
t_object *new_string (char *s)
{
t_object *p = malloc (sizeof (t_string));
p->string.type = T_STRING;
p->string.content = s;
return p;
}
int main (void)
{
t_object *object[] = { new_integer (42),
new_string ("Hello, world!"),
NULL };
for (int i = 0; object[i]; i++)
print_object (object[i]);
for (int i = 0; object[i]; i++)
free (object[i]);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
} t_base;
typedef struct
{
int content;
} t_integer;
typedef struct
{
char *content;
} t_string;
typedef union
{
t_base base;
t_integer integer;
t_string string;
} t_object;
void print_object (t_object *this)
{
// if (this->base.type == T_INTEGER)
// printf ("Integer: %d\n", this->integer.content);
// else if (this->base.type == T_STRING)
// printf ("String: \"%s\"\n", this->string.content);
}
t_object *new_integer (int i)
{
t_object *p = malloc (sizeof (t_integer));
p->integer.content = i;
return p;
}
t_object *new_string (char *s)
{
t_object *p = malloc (sizeof (t_string));
p->string.content = s;
return p;
}
int main (void)
{
t_object *object[] = { new_integer (42),
new_string ("Hello, world!"),
NULL };
for (int i = 0; object[i]; i++)
print_object (object[i]);
for (int i = 0; object[i]; i++)
free (object[i]);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
} t_base;
typedef struct
{
int content;
} t_integer;
typedef struct
{
char *content;
} t_string;
typedef union
{
t_base base;
t_integer integer;
t_string string;
} t_object;
void print_object (t_object *this)
{
printf ("Wenn dies eine Integer ist, dann lautet sie: %d.\n", this->integer.content);
printf ("Wenn dies ein String ist, ist er %zu Zeichen lang.\n",
strlen (this->string.content));
}
t_object *new_integer (int i)
{
t_object *p = malloc (sizeof (t_integer));
p->integer.content = i;
return p;
}
t_object *new_string (char *s)
{
t_object *p = malloc (sizeof (t_string));
p->string.content = s;
return p;
}
int main (void)
{
t_object *object[] = { new_integer (42),
new_string ("Hello, world!"),
NULL };
for (int i = 0; object[i]; i++)
print_object (object[i]);
for (int i = 0; object[i]; i++)
free (object[i]);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment