1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
|
---------- main.cpp ----------
#include <iostream>
#include "CStudent.h"
using namespace std;
int main()
{
//Testfall 1
CStudent student1("Muster", "Fritz", 123456);
cout << student1 << endl;
//Testfall 2
CStudent student2("Muster", "Luise");
cout << student2 << endl;
//Testfall 3
CStudent student3("Mayer", "Hans", 12);
cout << student3 << endl;
//Testfall 4
CStudent student4;
cout << student4 << endl;
//Testfall 5
CStudent student5(student1);
cout << student5 << endl;
//Testfall 6
student4.setName("Nachname", "Vorname");
cout << student4 << endl;
//Testfall 7
student4.setNumMatr(234567);
cout << student4 << endl;
//Testfall 8
student4.setNumMatr(12345678);
cout << student4 << endl;
return 0;
}
---------- CStudent.h ----------
#pragma once
#include <string>
#include <iostream>
class CStudent {
private:
int m_numMatr;
std::string m_firstname;
std::string m_surname;
public:
CStudent(std::string surname = "NoName", std::string firstname = "NoName", int numMatr = -1);
CStudent(CStudent& orig);
void print();
void setName(std::string firstname, std::string surname);
bool setNumMatr(int matr);
friend std::ostream& operator << (std::ostream& stream, CStudent& s);
};
std::ostream& operator << (std::ostream& stream, CStudent& s);
---------- CStudent.cpp ----------
#include <string>
#include "CStudent.h"
#include<iostream>
using namespace std;
CStudent::CStudent(string surname, string firstname, int numMatr)
{
m_surname = surname;
m_firstname = firstname;
m_numMatr = -1;
if(100000 <= numMatr && numMatr <= 999999)
m_numMatr = numMatr;
}
CStudent::CStudent(CStudent& orig)
{
m_surname = orig.m_surname;
m_firstname = orig.m_firstname;
m_numMatr = -1;
if(100000 <= orig.m_numMatr && orig.m_numMatr <= 999999)
m_numMatr = orig.m_numMatr;
}
void CStudent::setName( std::string firstname, std::string surname)
{
m_firstname = firstname;
m_surname = surname;
}
void CStudent::print()
{
cout << "CStudent@" << this << ": " << m_surname << ", " << m_firstname << " " << "(" << m_numMatr << ")" << endl;
}
bool CStudent::setNumMatr(int matr)
{
if(100000 <= matr && matr <= 999999)
m_numMatr = matr;
}
ostream& operator << (ostream& stream, CStudent& s)
{
stream << "CStudent@" << &s << ": " << s.m_surname << ", " << s.m_firstname << " " << "(" << s.m_numMatr << ")";
return stream;
} |