| @@ -0,0 +1,110 @@ | |||||
| \documentclass[uebung]{../../../lecture} | |||||
| \title{IPI: Übungsblatt 10} | |||||
| \author{Samuel Weidemaier, Christian Merten} | |||||
| \usepackage[]{listings} | |||||
| \usepackage{xcolor} | |||||
| \lstdefinestyle{mystyle}{ | |||||
| commentstyle=\color{gray}, | |||||
| language=C++, | |||||
| keywordstyle=\color{blue}, | |||||
| numberstyle=\tiny\color{gray}, | |||||
| stringstyle=\color{black}, | |||||
| basicstyle=\ttfamily\footnotesize, | |||||
| breakatwhitespace=false, | |||||
| breaklines=true, | |||||
| captionpos=b, | |||||
| keepspaces=true, | |||||
| numbers=left, | |||||
| numbersep=5pt, | |||||
| showspaces=false, | |||||
| showstringspaces=false, | |||||
| showtabs=false, | |||||
| tabsize=2 | |||||
| } | |||||
| \lstset{style=mystyle} | |||||
| \begin{document} | |||||
| \begin{aufgabe} | |||||
| siehe \textit{readfiles.cpp} | |||||
| \end{aufgabe} | |||||
| \begin{aufgabe} | |||||
| \begin{lstlisting}[language=C++, title=Fehlersuche, captionpos=b] | |||||
| #include <iostream> | |||||
| class A | |||||
| { | |||||
| public: | |||||
| int ap; | |||||
| void X(); | |||||
| private: | |||||
| int aq; | |||||
| void aX(); | |||||
| }; | |||||
| class B : public A | |||||
| { | |||||
| public: | |||||
| int bp; | |||||
| void Y(); | |||||
| private: | |||||
| int bq; | |||||
| void bY(); | |||||
| }; | |||||
| class C : public B | |||||
| { | |||||
| public: | |||||
| int cp; | |||||
| void Z(); | |||||
| private: | |||||
| int cq; | |||||
| void cZ(); | |||||
| }; | |||||
| void A::X() { }; | |||||
| void A::aX() { }; | |||||
| void B::bY() { }; | |||||
| void C::Z() { }; | |||||
| void B::Y() | |||||
| { | |||||
| bq = bp; | |||||
| aq = ap; // Fehler: A::aq ist privat, also in B nicht sichtbar | |||||
| bY(); | |||||
| } | |||||
| void C::cZ() | |||||
| { | |||||
| ap = 1; | |||||
| bp = 2; | |||||
| cq = 3; | |||||
| X(); | |||||
| Y(); | |||||
| aX(); // Fehler: A::aX() ist privat, also in C nicht sichtbar | |||||
| } | |||||
| int main() | |||||
| { | |||||
| A a; B b; C c; | |||||
| a.X(); | |||||
| b.bY(); // Fehler: B::bY() ist privat, also hier nicht sichtbar | |||||
| c.cp = 4; | |||||
| c.bp = 1; | |||||
| c.ap = 2; | |||||
| c.aq = 5; // Fehler: A::aq ist privat, in C nicht sichtbar und erst recht nicht hier | |||||
| b.ap = c.ap; | |||||
| return 0; | |||||
| } | |||||
| \end{lstlisting} | |||||
| \end{aufgabe} | |||||
| \end{document} | |||||
| @@ -0,0 +1,39 @@ | |||||
| #include <fstream> | |||||
| #include <iostream> | |||||
| #include <string> | |||||
| using namespace std; | |||||
| int main(int argc, char* argv[]) { | |||||
| // check if correct argument count | |||||
| if (argc < 2) { | |||||
| printf("Dateiname erwartet\n"); | |||||
| return 1; | |||||
| } | |||||
| // get filename from argument list | |||||
| string filename = argv[1]; | |||||
| // open input file | |||||
| ifstream fileIn; | |||||
| fileIn.open(filename + ".txt"); | |||||
| // open output file | |||||
| ofstream fileOut; | |||||
| fileOut.open(filename + "-a.txt"); | |||||
| // check if we could open the files | |||||
| if (!fileIn.good() || !fileOut.good()) { | |||||
| // could not open files | |||||
| cout << "Konnte Datei " + filename + ".txt nicht lesen" << endl; | |||||
| return 1; | |||||
| } | |||||
| string input; // variable containing line | |||||
| int lineNo = 1; // count lines | |||||
| while(getline(fileIn, input)) { // while there is still a line left | |||||
| // write out line with prepended line number to output file | |||||
| fileOut << to_string(lineNo) + ": " + input << endl; | |||||
| // increment line counter | |||||
| lineNo += 1; | |||||
| } | |||||
| // close files | |||||
| fileIn.close(); | |||||
| fileOut.close(); | |||||
| } | |||||
| @@ -0,0 +1,57 @@ | |||||
| class A { | |||||
| public: | |||||
| void a() { }; | |||||
| virtual void va() = 0; | |||||
| virtual void a(int n); | |||||
| private: | |||||
| void c() { }; | |||||
| }; | |||||
| class B : public A { | |||||
| public: | |||||
| void b() { }; | |||||
| virtual void vb(); | |||||
| void a(double d) { }; | |||||
| void a(int i) { }; | |||||
| virtual void va(); | |||||
| }; | |||||
| class C : public B { | |||||
| public: | |||||
| virtual void c(); | |||||
| void a(float) { }; | |||||
| virtual void a(); | |||||
| virtual void va(); | |||||
| }; | |||||
| int main() { | |||||
| // A a; A abstrakte Klasse | |||||
| B b; | |||||
| C c; | |||||
| A* pa=&b; | |||||
| B* pb=&c; | |||||
| float x = 1.2; | |||||
| pa->a(); | |||||
| pa->va(); | |||||
| pa->a(1); | |||||
| // pa->c(); A::c() private, hier nicht sichtbar | |||||
| // pa->b(); pa ist vom Typ A* und kennt deswegen b() nicht | |||||
| // pa->vb(); pa ist vom Typ A* und kennt deswegen vb() nicht | |||||
| pa->a(x); | |||||
| // pb->a(); pb ist vom Typ B*, hier ist A::a() überladen durch | |||||
| // entweder B::a(double) bzw. B::a(int) nicht durch B::a() | |||||
| pb->va(); | |||||
| pb->a(1); | |||||
| // pb->c(); A::c() private, deswegen hier nicht sichtbar | |||||
| pb->b(); | |||||
| pb->vb(); | |||||
| pb->a(x); | |||||
| pa = &c; | |||||
| pa->a(); | |||||
| pa->va(); | |||||
| pa->a(1); | |||||
| // pa->c(); A::c() private, hier nicht sichtbar | |||||
| // pa->b(); pa ist vom Typ A*, kennt deswegen b() nicht | |||||
| // pa->vb(); pa ist vom Typ A*, kennt deswegen vb() nicht | |||||
| pa->a(x); | |||||
| } | |||||