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

Einführung in C++, 23.6.2016

parent d7800298
No related branches found
No related tags found
No related merge requests found
Showing
with 441 additions and 0 deletions
Software-Entwicklung für eingebettete Systeme
=============================================
Einführung in C++
~~~~~~~~~~~~~~~~~
:) Streams: überladener Operator "<<"
:) Objektorientierte Programmierung:
:) virtuelle Methoden
:) Konstruktoren
:) Vererbung und Polymorphie
:) Zugriffsrechte
:) Destruktoren
:) Referenzen
:) Templates
:) STL
:) auto-Typen
:) spezielle Zeiger
:) Exceptions
Spezielle Zeiger
~~~~~~~~~~~~~~~~
Problem: Zeiger auf Objektinstanz; der Zeiger hat begrenzte Gültigkeit
#include <iostream>
using namespace std;
int main ()
{
int a = 42;
int b = 0;
if (b == 0)
{
cout << "Irgendwas ist schiefgelaufen: Ganzzahldivision durch Null" << endl;
}
else
{
int c = a / b;
cout << "c = " << c << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main ()
{
try
{
int a = 42;
int b = 0;
if (b == 0)
throw "Ganzzahldivision durch Null";
int c = a / b;
cout << "c = " << c << endl;
}
catch (const char *msg)
{
cout << "Irgendwas ist schiefgelaufen: " << msg << endl;
}
return 0;
}
#include <iostream>
using namespace std;
struct node
{
int content;
node *next;
};
int main ()
{
node A, B, C, D;
A.content = 2;
A.next = &B;
B.content = 3;
B.next = &C;
C.content = 5;
C.next = &D;
D.content = 7;
D.next = NULL;
for (node *p = &A; p; p = p->next)
cout << p->content << endl;
return 0;
}
#include <iostream>
using namespace std;
struct node
{
int content;
node *next;
};
int main ()
{
node A, B, C, D;
A.content = 2;
A.next = &B;
B.content = 3;
B.next = &C;
C.content = 5;
C.next = &D;
D.content = 7;
D.next = NULL;
for (node *p = &A; p; p = p->next)
cout << p->content << endl;
node BO;
BO.content = 4;
BO.next = &C;
B.next = &BO;
for (node *p = &A; p; p = p->next)
cout << p->content << endl;
return 0;
}
#include <iostream>
using namespace std;
class node
{
public:
int content;
node *next;
void insertAfter (node *newNode);
};
void node::insertAfter (node *newNode)
{
if (newNode)
newNode->next = next;
next = newNode;
}
int main ()
{
node A, B, C, D;
A.content = 2;
A.next = &B;
B.content = 3;
B.next = &C;
C.content = 5;
C.next = &D;
D.content = 7;
D.next = NULL;
for (node *p = &A; p; p = p->next)
cout << p->content << endl;
node BO;
BO.content = 4;
B.insertAfter (&BO);
for (node *p = &A; p; p = p->next)
cout << p->content << endl;
return 0;
}
#include <iostream>
using namespace std;
template <typename contentType> class node
{
public:
contentType content;
node *next;
void insertAfter (node *newNode)
{
if (newNode)
newNode->next = next;
next = newNode;
}
};
int main ()
{
node <int> A, B, C, D;
A.content = 2;
A.next = &B;
B.content = 3;
B.next = &C;
C.content = 5;
C.next = &D;
D.content = 7;
D.next = NULL;
for (node <int> *p = &A; p; p = p->next)
cout << p->content << endl;
node <int> BO;
BO.content = 4;
B.insertAfter (&BO);
for (node <int> *p = &A; p; p = p->next)
cout << p->content << endl;
node <string> E, F;
E.content = "Hello, ";
E.next = NULL;
F.content = "world!";
F.next = NULL;
E.insertAfter (&F);
for (node <string> *p = &E; p; p = p->next)
cout << p->content << endl;
return 0;
}
#include <iostream>
using namespace std;
class Bewirker
{
public:
Bewirker ();
~Bewirker ();
};
Bewirker::Bewirker ()
{
cout << "Raumfahrzeug wird startbereit gemacht ... Moment ..." << endl;
}
Bewirker::~Bewirker ()
{
cout << "Raumfahrzeug wird wieder transportfähig gemacht ... Moment ..." << endl;
}
int main ()
{
Bewirker SaturnV;
return 0;
}
#include <iostream>
using namespace std;
void some_function ()
{
int *p = new int[1];
*p = 42;
/* ... */
cout << *p << endl;
delete p;
}
int main ()
{
some_function ();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
void some_function (void)
{
int *p = malloc (sizeof (int));
*p = 42;
/* ... */
printf ("%d\n", *p);
free (p);
}
int main (void)
{
some_function ();
return 0;
}
#include <iostream>
using namespace std;
int *some_function ()
{
int *p = new int[1];
*p = 42;
return p;
}
int main ()
{
int *p = some_function ();
cout << *p << endl;
delete p;
return 0;
}
#include <iostream>
#include <memory>
using namespace std;
shared_ptr <int> some_function ()
{
shared_ptr <int> p (new int[1]);
*p = 42;
return p;
}
int main ()
{
auto p = some_function ();
cout << *p << endl;
return 0;
}
#include <iostream>
#include <memory>
using namespace std;
shared_ptr <int> some_function ()
{
shared_ptr <int> p (new int[1]);
*p = 42;
return p;
}
int main ()
{
auto p = some_function ();
auto q = p;
cout << *p << endl;
cout << *q / 2 + 2 << endl;
return 0;
}
#include <iostream>
#include <memory>
using namespace std;
unique_ptr <int> some_function ()
{
unique_ptr <int> p (new int[1]);
*p = 42;
return p;
}
int main ()
{
auto p = some_function ();
auto q = p;
cout << *p << endl;
cout << *q / 2 + 2 << endl;
return 0;
}
#include <iostream>
#include <memory>
using namespace std;
unique_ptr <int> some_function ()
{
unique_ptr <int> p (new int[1]);
*p = 42;
return p;
}
int main ()
{
auto p = some_function ();
auto q = move (p);
cout << *p << endl;
cout << *q / 2 + 2 << endl;
return 0;
}
#include <iostream>
#include <memory>
using namespace std;
unique_ptr <int> some_function ()
{
unique_ptr <int> p (new int[1]);
*p = 42;
return p;
}
int main ()
{
auto p = some_function ();
cout << *p << endl;
auto q = move (p);
cout << *q / 2 + 2 << endl;
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector <int> prime;
prime.push_back (2);
prime.push_back (3);
prime.push_back (5);
prime.push_back (7);
prime.push_back (11);
for (int i = 0; i < 5; i++)
cout << prime[i] << endl;
return 0;
}
#include <iostream>
#include <list>
using namespace std;
int main ()
{
list <int> prime;
prime.push_back (2);
prime.push_back (3);
prime.push_back (5);
prime.push_back (7);
prime.push_back (11);
for (list <int> ::iterator p = prime.begin (); p != prime.end (); p++)
cout << *p << endl;
return 0;
}
#include <iostream>
using namespace std;
int main ()
{
int prime[] = { 2, 3, 5, 7, 11 };
for (int *p = prime; p != prime + 5; p++)
cout << *p << endl;
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector <int> prime;
prime.push_back (2);
prime.push_back (3);
prime.push_back (5);
prime.push_back (7);
prime.push_back (11);
for (vector <int> ::iterator p = prime.begin (); p != prime.end (); p++)
cout << *p << endl;
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment