summaryrefslogtreecommitdiffstats
path: root/Bachelor/Prog2/Codebeispiele/Student
diff options
context:
space:
mode:
authorSven Eisenhauer <sven@sven-eisenhauer.net>2023-11-10 15:11:48 +0100
committerSven Eisenhauer <sven@sven-eisenhauer.net>2023-11-10 15:11:48 +0100
commit33613a85afc4b1481367fbe92a17ee59c240250b (patch)
tree670b842326116b376b505ec2263878912fca97e2 /Bachelor/Prog2/Codebeispiele/Student
downloadStudium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.gz
Studium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.bz2
add new repoHEADmaster
Diffstat (limited to 'Bachelor/Prog2/Codebeispiele/Student')
-rw-r--r--Bachelor/Prog2/Codebeispiele/Student/StudDat.cpp35
-rw-r--r--Bachelor/Prog2/Codebeispiele/Student/Student.cpp49
-rw-r--r--Bachelor/Prog2/Codebeispiele/Student/Student.h26
3 files changed, 110 insertions, 0 deletions
diff --git a/Bachelor/Prog2/Codebeispiele/Student/StudDat.cpp b/Bachelor/Prog2/Codebeispiele/Student/StudDat.cpp
new file mode 100644
index 0000000..306ae62
--- /dev/null
+++ b/Bachelor/Prog2/Codebeispiele/Student/StudDat.cpp
@@ -0,0 +1,35 @@
+// Persistence of objects: Storing of Student-Objects
+// Author: Hans-Peter Weber
+// Date: 07.03.05
+
+#include <iostream>
+using std::cout;
+using std::endl;
+using std::ios;
+
+#include <fstream>
+using std::fstream;
+
+#include "Student.h"
+
+int main()
+{
+ Student stud;
+
+ cout << "Adresse von Student-Objekt: " << &stud << endl;
+ cout << "Groesse von Student-Objekt: " << sizeof( Student ) << " Byte" << endl;
+
+/* fstream outFile( "Studs.seq", ios::binary | ios::out ); // write
+ stud.set( "Hans Castorp", 578111, 23 );
+ stud.write( outFile );
+ stud.set( "Claudia Chauchat", 578666, 27 );
+ stud.write( outFile );
+ outFile.close();
+*/
+ fstream inFile( "Studs.seq", ios::binary | ios::in ); // read
+ while( stud.read( inFile ) )
+ stud.print();
+ inFile.close();
+
+ return 0;
+} \ No newline at end of file
diff --git a/Bachelor/Prog2/Codebeispiele/Student/Student.cpp b/Bachelor/Prog2/Codebeispiele/Student/Student.cpp
new file mode 100644
index 0000000..d58d6df
--- /dev/null
+++ b/Bachelor/Prog2/Codebeispiele/Student/Student.cpp
@@ -0,0 +1,49 @@
+// Student.cpp: Implementation of class Student.
+
+#include <iostream>
+using std::cout;
+using std::endl;
+using std::istream;
+using std::ostream;
+
+#include <string>
+using std::string;
+using std::getline;
+
+#include "Student.h"
+
+Student::Student()
+{
+}
+
+Student::~Student()
+{
+}
+
+void Student::set( string n, int m, int a )
+{
+ name = n;
+ matNr = m;
+ age = a;
+}
+
+void Student::print()
+{
+ cout << name << ", Matrikelnummer: " << matNr << ", Alter: " << age << endl;
+}
+
+ostream& Student::write( std::ostream& os ) const
+{
+// os.write( ( char* )&matNr, sizeof matNr ); // stores '578111' as an int in 4 Bytes
+// os << matNr; // stores '578111' as ASCII-Code in 6 Bytes!
+ os << name << '\0'; // write string
+ os.write( ( char* ) &matNr, 2 * sizeof( int ) ); // write 2 int starting at address of matNr
+ return os;
+}
+
+istream& Student::read( std::istream& is )
+{
+ getline( is, name, '\0' ); // read string
+ is.read( ( char* ) &matNr, 2 * sizeof( int ) ); // read 2 int starting at address of matNr
+ return is;
+} \ No newline at end of file
diff --git a/Bachelor/Prog2/Codebeispiele/Student/Student.h b/Bachelor/Prog2/Codebeispiele/Student/Student.h
new file mode 100644
index 0000000..3bcb426
--- /dev/null
+++ b/Bachelor/Prog2/Codebeispiele/Student/Student.h
@@ -0,0 +1,26 @@
+// Student.h: Interface for class Student.
+
+#if !defined STUDENT_H
+#define STUDENT_H
+
+#include <string>
+
+class Student
+{
+
+public:
+ Student();
+ ~Student();
+ void set( std::string, int, int );
+ void print();
+ std::ostream& write( std::ostream& ) const;
+ std::istream& read( std::istream& );
+
+private:
+ std::string name;
+ int matNr;
+ int age;
+
+};
+
+#endif