Für Vorlesungen, bitte die Webseite verwenden. https://flavigny.de/lecture
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

165 linhas
3.9KB

  1. \documentclass[uebung]{../../../lecture}
  2. \title{IPI: Übungsblatt 10}
  3. \author{Samuel Weidemaier, Christian Merten}
  4. \begin{document}
  5. \begin{tabular}{|c|m{1cm}|m{1cm}|m{1cm}|m{1cm}|@{}m{0cm}@{}}
  6. \hline
  7. Aufgabe & \centering A1 & \centering A2 & \centering A3 & \centering $\sum$ & \\[5mm] \hline
  8. Punkte & & & & & \\[5mm] \hline
  9. \end{tabular}
  10. \begin{aufgabe} Lesen und Schreiben von Dateien
  11. \begin{lstlisting}[language=C++, title=readfiles.cpp, captionpos=b]
  12. #include <fstream>
  13. #include <iostream>
  14. #include <string>
  15. using namespace std;
  16. int main(int argc, char* argv[]) {
  17. // check if correct argument count
  18. if (argc < 2) {
  19. printf("Dateiname erwartet\n");
  20. return 1;
  21. }
  22. // get filename from argument list
  23. string filename = argv[1];
  24. // open input file
  25. ifstream fileIn;
  26. fileIn.open(filename + ".txt");
  27. // open output file
  28. ofstream fileOut;
  29. fileOut.open(filename + "-a.txt");
  30. // check if we could open the files
  31. if (!fileIn.good() || !fileOut.good()) {
  32. // could not open files
  33. cout << "Konnte Datei " + filename + ".txt nicht lesen" << endl;
  34. return 1;
  35. }
  36. string input; // variable containing line
  37. int lineNo = 1; // count lines
  38. while(getline(fileIn, input)) { // while there is still a line left
  39. // write out line with prepended line number to output file
  40. fileOut << to_string(lineNo) + ": " + input << endl;
  41. // increment line counter
  42. lineNo += 1;
  43. }
  44. // close files
  45. fileIn.close();
  46. fileOut.close();
  47. }\end{lstlisting}
  48. \end{aufgabe}
  49. \begin{aufgabe} Fehler sind im Quellcode markiert
  50. \begin{lstlisting}[language=C++, title=Fehlersuche, captionpos=b]
  51. #include <iostream>
  52. class A
  53. {
  54. public:
  55. int ap;
  56. void X();
  57. private:
  58. int aq;
  59. void aX();
  60. };
  61. class B : public A
  62. {
  63. public:
  64. int bp;
  65. void Y();
  66. private:
  67. int bq;
  68. void bY();
  69. };
  70. class C : public B
  71. {
  72. public:
  73. int cp;
  74. void Z();
  75. private:
  76. int cq;
  77. void cZ();
  78. };
  79. void A::X() { };
  80. void A::aX() { };
  81. void B::bY() { };
  82. void C::Z() { };
  83. void B::Y()
  84. {
  85. bq = bp;
  86. aq = ap; // Fehler: A::aq ist privat, also in B nicht sichtbar
  87. bY();
  88. }
  89. void C::cZ()
  90. {
  91. ap = 1;
  92. bp = 2;
  93. cq = 3;
  94. X();
  95. Y();
  96. aX(); // Fehler: A::aX() ist privat, also in C nicht sichtbar
  97. }
  98. int main()
  99. {
  100. A a; B b; C c;
  101. a.X();
  102. b.bY(); // Fehler: B::bY() ist privat, also hier nicht sichtbar
  103. c.cp = 4;
  104. c.bp = 1;
  105. c.ap = 2;
  106. c.aq = 5; // Fehler: A::aq ist privat, in C nicht sichtbar und erst recht nicht hier
  107. b.ap = c.ap;
  108. return 0;
  109. }\end{lstlisting}
  110. \end{aufgabe}
  111. \begin{aufgabe}
  112. Fehler wurden wegkommentiert mit Begründung dahinter, hinter den
  113. korrekten Aufrufen steht jeweils, welche Implementation aufgerufen wird.
  114. \begin{lstlisting}[language=C++, title=Virtuelle Methoden, captionpos=b]
  115. // A a; A abstrakte Klasse
  116. B b;
  117. C c;
  118. A* pa=&b;
  119. B* pb=&c;
  120. float x = 1.2;
  121. pa->a(); // A::a()
  122. pa->va(); // B::va()
  123. pa->a(1); // B::a(int)
  124. // pa->c(); A::c() private, hier nicht sichtbar
  125. // pa->b(); pa ist vom Typ A* und kennt deswegen b() nicht
  126. // pa->vb(); pa ist vom Typ A* und kennt deswegen vb() nicht
  127. pa->a(x); // B::a(int), hier wird double x implizit zu int gecastet
  128. // pb->a(); pb ist vom Typ B*, hier ist A::a() ueberladen durch
  129. // entweder B::a(double) bzw. B::a(int), deswegen existiert B::a() nicht
  130. pb->va(); // C::va()
  131. pb->a(1); // B::a(int)
  132. // pb->c(); A::c() private, deswegen hier nicht sichtbar
  133. pb->b(); // B::b()
  134. pb->vb(); // B::vb()
  135. pb->a(x); // B::a(double)
  136. pa = &c;
  137. pa->a(); // A::a()
  138. pa->va(); // C::va()
  139. pa->a(1); // B::a(int)
  140. // pa->c(); A::c() private, hier nicht sichtbar
  141. // pa->b(); pa ist vom Typ A*, kennt deswegen b() nicht
  142. // pa->vb(); pa ist vom Typ A*, kennt deswegen vb() nicht
  143. pa->a(x); // B::a(int), hier wird double x implizit zu int gecastet\end{lstlisting}
  144. \end{aufgabe}
  145. \end{document}