Erstes C++ Programm

Neue Frage »

Auf diesen Beitrag antworten »
Tany Erstes C++ Programm

Guten Morgen , ich habe gerade probleme bei einer Aufgabe:

Eigenschaften
Attribut
Beschreibung
m_numMatr
Sechsstellige Matrikelnummer des Studenten
m_firstname
Vorname des Studenten
m_surname
Nachname des Studenten
Verhalten
Methode
Beschreibung
CStudent (1)
Erzeugt ein neues Studentenobjekt. Wenn die Matrikelnummer nicht im gültigen Bereich liegt und
CStudent (2) nicht gleich -1 ist, setzt er das Attribut auf den Wert -1.
Erzeugt ein neues Studentenobjekt als Kopie eines bereits vorhandenen Studenten-Objektes.
print
Zeigt die Adresse und die aktuellen Attribut-Werte des Studenten-Objektes auf dem Bildschirm an. Beispiel: CStudent@0012FF10: Muster, Fritz (123456)
setName
Ändert Namen und Vornamen des Studenten.
setNumMatr
Ändert die Matrikelnummer. Hinweis: Da die Methode ein Attribut der Klasse verändert, müssen auch bei ihrer Implementierung die Zusicherungen, die für dieses Attribut gelten, beachtet werden. Sollten die Zusicherungen verletzt werden, wird die Matrikelnummer nicht
operator << geändert und eine Warnung auf dem Bildschirm angezeigt, die aussagt, wie eine korrekte Matrikelnummer auszusehen hat.
Zeigt das Studentenobjekt in der gleichen Form wie print auf dem Bildschirm an.
a) Definieren Sie die Klasse CStudent gemäß dem UML-Klassendiagramm in der Datei CStudent.h und implementieren Sie die Methoden der Klasse in der Datei CStudent.cpp.
b) Schreiben Sie ein Testprogramm, das die korrekte Funktionsweise der Klasse nachweist. Program-mieren Sie dazu die in der Tabelle angegebenen Testfälle und zeigen Sie den Zustand des jeweiligen Objektes mit der print-Methode an. Dabei wird jede Methode überprüft. Falls es Parameterwerte gibt, die Probleme verursachen könnten, werden mehrere Testfälle definiert:
Testfall
Beschreibung
erwartetes Verhalten
1
Konstruktor-Aufruf mit drei korrekten Parametern (Muster, Fritz, 123456)
Objekt wird erzeugt, Attributwerte stimmen mit Parameterwerten überein
2
Konstruktor-Aufruf mit 2 Parametern (Muster, Luise)
Objekt wird erzeugt, Attributwerte für Vor- und Nachnamen stimmen mit Parameterwerten überein, Matrikelnummer ist -1
3
Konstruktor-Aufruf mit ungültiger Matrikelnummer (Meyer, Hans, 12)
wie Testfall 2
4
Konstruktor-Aufruf ohne Parameter
Alle Attributwerte sind mit den Defaultwerten belegt
5
Copy-Konstruktor-Aufruf
Objekt wird erzeugt und hat die gleichen Attributwerte wie das Original
6
setName-Aufruf
Attributwerte für Vor- und Nachname stimmen mit Parameterwerten überein
7
setNumMatr-Aufruf mit gültiger Matrikelnummer (234567)
Die Matrikelnummer ist geändert
8
setNumMatr-Aufruf mit ungültiger Matrikelnummer (12345678)
Die Matrikelnummer ist unverändert.
9
Ausgabe des Objektes aus Testfall 3 auf cout mit Operator <<
Anzeige wie Testfall 3.



Ich bin noch gerade im 1 Testfall und habe leider ein paar Fehlermeldungen die ich einfach nicht beseitigen kann.

Header File:
code:
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:
#ifndef CSTUDENT_H_
#define CSTUDENT_H_
#include <string>
#include <iostream>
using namespace std

class CStudent {
private:
	int m_numMatr;
	std::string m_firstname;
	std::string m_surname;
	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 ostream& operator (out stream&, CStudent& s);



};
   ostream& operator (out stream&, CStudent& s);


#endif /* CSTUDENT_H_ */


Cpp file:

code:
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:
  

#include <string>
#include "CStudent.h"
#include<iostream>

CStudent::CStudent(surname = "Muster", firstname = "Fritz", numMatr = 123456){
	m_surname = surname;
	m_firstname = firstname;

	if(numMatr == -1 | 100000 <= nuMatr<= 999999){

		m_numMatr = numMatr;

		else {

			return false;
		}
	}





};



Fehlermeldungen:

Description Resource Path Location Type
'out' was not declared in this scope CStudent.h /CStudent line 29 C/C++ Problem

Description Resource Path Location Type
's' was not declared in this scope CStudent.h /CStudent line 29 C/C++ Problem
Description Resource Path Location Type
expected ';' at end of member declaration CStudent.h /CStudent line 24 C/C++ Problem
Description Resource Path Location Type
expected ';' before 'class' CStudent.h /CStudent line 14 C/C++ Problem
Description Resource Path Location Type
expected ')' before 'stream' CStudent.h /CStudent line 24 C/C++ Problem
Description Resource Path Location Type
expected constructor, destructor, or type conversion before '(' token CStudent.cpp /CStudent line 14 C/C++ Problem
Description Resource Path Location Type
expected primary-expression before '&' token CStudent.h /CStudent line 29 C/C++ Problem
Description Resource Path Location Type
expected type-specifier before '(' token CStudent.h /CStudent line 24 C/C++ Problem


Ich weiss es sind sehr viele Fehler .

Aber hoffentlich kann mir jemand helfen.


Im Uml Diagramm war die Zusicherung für den konstruktor:

numMatr == -1 | 100000 <= nuMatr<= 999999


Für Hilfe wäre ich dankbar.
 
Auf diesen Beitrag antworten »
eulerscheZahl

Ich weiß nicht, welchen Compiler du verwendest, im Folgenden behebe ich die Fehler des g++:
Zitat:
CStudent.h:7:1: error: expected ';' before 'class'

nach using namespace std fehlt ein ;
aber da du ohnehin std:: schreibst, kannst du die Zeile löschen (std::ostream muss ergänzt werden)

Zitat:
CStudent.h:16:32: error: expected type-specifier before '(' token


Zitat:
CStudent.h:11:2: error: 'CStudent::CStudent(std::string, std::string, int)' is private

weil du public: vergessen hast

Zitat:
CStudent.cpp:5:19: error: expected constructor, destructor, or type conversion before '(' token

hier fehlt using namespace std;, außerdem ist dein Konstruktorkopf falsch
CStudent::CStudent(string surname, string firstname, int numMatr)
du hast den Datentyp vergessen, außerdem kommen Defaultwerte nur in den Header.

Zitat:
if(numMatr == -1 | 100000 <= nuMatr<= 999999)

Tippfehler: nuMatr -> numMatr
-1 | 100000 macht ein bitweises oder, das logische geht mit ||
dann hast du da noch eine }, die zu spät zugeht und ein return im Konstruktor, das da nicht stehen darf.

code:
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:
#ifndef CSTUDENT_H_
#define CSTUDENT_H_
#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);


#endif /* CSTUDENT_H_ */

------------------------------------------------------

#include <string>
#include "CStudent.h"
#include<iostream>
using namespace std;

CStudent::CStudent(string surname, string firstname, int numMatr)
{
	m_surname = surname;
	m_firstname = firstname;

	if(numMatr == -1 || 100000 <= numMatr && numMatr <= 999999)
	{
		m_numMatr = numMatr;
	}
}
Auf diesen Beitrag antworten »
Tany

Ich habe leider immer noch Fehlermeldungen .

Habe paar in den Griff bekommen aber immer noch probleme.

Die Ide ist Eclipse.



Description Resource Path Location Type
'out' was not declared in this scope CStudent.h /CStudent line 31 C/C++ Problem
's' was not declared in this scope CStudent.h /CStudent line 31 C/C++ Problem
declaration of 'operator<<' as non-function CStudent.h /CStudent line 26 C/C++ Problem
declaration of 'operator<<' as non-function CStudent.h /CStudent line 31 C/C++ Problem
expected ';' at end of member declaration CStudent.h /CStudent line 26 C/C++ Problem
expected ')' before 'stream' CStudent.h /CStudent line 26 C/C++ Problem
expected primary-expression before '&' token CStudent.h /CStudent line 31 C/C++ Problem
suggest parentheses around '&&' within '||' [-Wparentheses] CStudent.cpp /CStudent line 22 C/C++ Problem
Suggested parenthesis around expression '100000 <= numMatr && numMatr <= 999999' CStudent.cpp /CStudent line 22 Code Analysis Problem


code:
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:

#ifndef CSTUDENT_H_
#define CSTUDENT_H_
#include <string>
#include <iostream>
using namespace std;

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  << (out stream&, CStudent& s);



};
   std::ostream& operator << (out stream&, CStudent& s);


#endif /* CSTUDENT_H_ */








code:
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:

#include <string>
#include "CStudent.h"
#include<iostream>
using namespace std;

CStudent::CStudent(string surname , string firstname , int numMatr ){
	m_surname = surname;
	m_firstname = firstname;




	if(numMatr == -1 || 100000 <= numMatr && numMatr <= 999999){

		m_numMatr = numMatr;


	}



}





Irgendwas scheint auch mit der if Bedingung nicht zu stimmen ?

Der Compiler meckert da. verwirrt

Kannst du mir helfen?
Auf diesen Beitrag antworten »
eulerscheZahl

Ich habe dir einen funktionerenden Header gegeben, es ist bei dir ihn einzubinden:
Zitat:
friend std::ostream& operator << (out stream&, CStudent& s);

das out muss ostream heißen

die if-Bedingung stimmt so, der Compiler rät nur zur Klammerung (ist aber nicht nötig, da && die höhere Priorität hat).
So sollte die Warnung weggehen:
if(numMatr == -1 || (100000 <= numMatr && numMatr <= 999999))

Sag mal, du bist doch auch Man23 und Jassy, oder?
 
Auf diesen Beitrag antworten »
Tany

Hi euler bevor ich weiter mit dem testen in der main funktion beginnen wollte , wollte ich kurz mal nachfragen ob meine implementierungen soweit der Aufgabenstellung entsprechend in Ordnung ist?



code:
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:

#include <string>
#include "CStudent.h"
#include<iostream>
using namespace std;

CStudent::CStudent(string surname , string firstname , int numMatr ){
	m_surname = surname;
	m_firstname = firstname;




	if(numMatr == -1 ||( 100000 <= numMatr&&numMatr <= 999999)){

		m_numMatr = numMatr;


	}



}

CStudent::CStudent( CStudent& orig){

	m_surname = orig.m_surname;
		m_firstname = orig.m_firstname;




		if(m_numMatr == -1 ||( 100000 <= m_numMatr&&m_numMatr <= 999999)){

			m_numMatr = orig.m_numMatr;


		}

}

		void CStudent::setName( std::string firstname, std::string surname){
			m_firstname = firstname;

			m_surname = surname;




		}

		CStudent::CStudent(){

		}



P:S

Tany alias Jassy großes Grinsen
Auf diesen Beitrag antworten »
Tany

Ich wollte ausserdem den 1 Testfall machen ,aber irgendwie funzt es nicht:

int main(){

CStudent z1;

CStudent z1("Muster", "Fritz", 123456);



}


Description Resource Path Location Type
'CStudent' was not declared in this scope main.cpp /CStudent line 12 C/C++ Problem
expected ';' before 'z1' main.cpp /CStudent line 12 C/C++ Problem
expected ';' before 'z1' main.cpp /CStudent line 14 C/C++ Problem


Was ist falsch ?

Ich verstehe das nicht.
Auf diesen Beitrag antworten »
eulerscheZahl

Dann muss ich die Aufgabe ja doch lesen unglücklich
Nochmal zum alten Code:
code:
1:
2:
3:
if(numMatr == -1 ||( 100000 <= numMatr&&numMatr <= 999999)){
    m_numMatr = numMatr;
}

Und was, wenn die Nummer außerhalb des gewünschten Wertebereichs liegt?
Dann steht da irgendwas drin.

code:
1:
2:
3:
4:
5:
6:
7:
CStudent::CStudent( CStudent& orig)
{
    m_surname = orig.m_surname;
    m_firstname = orig.m_firstname;
    if(m_numMatr == -1 ||( 100000 <= m_numMatr&&m_numMatr <= 999999))
        m_numMatr = orig.m_numMatr;
}

Hier musst du (bei if...) natürlich den Bereich von orig.m_numMatr prüfen.

Mein .cpp soweit:
code:
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:
#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;
}
Auf diesen Beitrag antworten »
Tany alias Jassy

So würde jetzt meine cpp aussehen:


code:
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:
#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;
	}
	else {

		return false;
	}
}

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;
}


Und was, wenn die Nummer außerhalb des gewünschten Wertebereichs liegt?

Da habe ich ja jetzt einfach return false rein geschrieben .

Aber der compiler meckert jetzt.
Was kann ich denn sonst schreiben ?

Woher soll ich das wissen ?
Es steht ja nichts in der Aufgabe.

Weisst du eigentlich auch warum mein compiler beim testen in der main meckert?

Ich dachte das es richtig wäre? verwirrt
Auf diesen Beitrag antworten »
eulerscheZahl

Ein Konstruktor hat keinen Rückgabewert, da kannst du kein return false schreiben.
mit
code:
1:
2:
3:
m_numMatr = -1;
if(100000 <= numMatr && numMatr <= 999999)
    m_numMatr = numMatr;

Wird als Martikelnummer erst -1 geschrieben und dann - wenn der Wertebereich passt - mit der echten Nummer überschrieben.
Wenn die Nummer falsch ist, bleibt die -1 stehen.

Was an deiner main falsch ist, kann ich dir erst sagen, wenn ich sie gesehen habe.
Auf diesen Beitrag antworten »
Tany

Ich glaube du hast einer meiner Beiträge übersehen :

Hier nochmal:
code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
int main(){

CStudent z1;

CStudent z1("Muster", "Fritz", 123456);



}

Auf diesen Beitrag antworten »
eulerscheZahl

Tatsächlich geschockt
Du versuchst zwei Variablen mit dem selben Namen anzulegen, das kann nicht gutgehen.
code:
1:
2:
3:
4:
int main()
{
    CStudent z1("Muster", "Fritz", 123456);
}
Auf diesen Beitrag antworten »
Tany

Wie macht man das sonst ?

Kannst du mir das kurz erklären ?
Auf diesen Beitrag antworten »
eulerscheZahl

code:
1:
2:
3:
4:
5:
6:
7:
8:
int main()
{
    CStudent z1("Muster", "Fritz", 123456);
    CStudent z2; //Variable anlegen
    z2 = CStudent(z1); // bei der bereits angelegten Variablen keinen Datentyp mehr angeben
//alternativ in einem:
//CStudent z2(z1);
}
Auf diesen Beitrag antworten »
Tany

int main(){



int main()
{
CStudent z1("Muster", "Fritz", 123456);
CStudent z2; //Variable anlegen
z2 = CStudent(z1);




}


Mir werden trotzdem fehler angezeigt:

Description Resource Path Location Type
a function-definition is not allowed here before '{' token main.cpp /CStudent line 15 C/C++ Problem
expected '}' at end of input main.cpp /CStudent line 23 C/C++ Problem

Aber wieso muss man das überhaupt in einer Variablen ablegen ?

Kann man den Konstruktor nicht einfach mit seinem Namen aufrufen oder so?
Auf diesen Beitrag antworten »
eulerscheZahl

Du bist im Begriff, in der main() eine main() zu definieren.

CStudent z1; //legt eine Variable mit Namen z1 an
CStudent z1("Muster", "Fritz", 123456); //legt auch eine Variable mit Namen z1 an
die beiden können nicht in der gleichen Hierarchieebene existieren.
Auf diesen Beitrag antworten »
Tany

Oh ja das war nur ein versehen :

Trotzdem geht es nicht.

int main()
{
CStudent z1("Muster", "Fritz", 123456);
CStudent z2; //Variable anlegen
z2 = CStudent(z1);




}

Description Resource Path Location Type
'CStudent' was not declared in this scope main.cpp /CStudent line 16 C/C++ Problem
'z1' was not declared in this scope main.cpp /CStudent line 18 C/C++ Problem
'z2' was not declared in this scope main.cpp /CStudent line 18 C/C++ Problem
expected ';' before 'z1' main.cpp /CStudent line 16 C/C++ Problem
expected ';' before 'z2' main.cpp /CStudent line 17 C/C++ Problem
Auf diesen Beitrag antworten »
eulerscheZahl

Also, bei mir geht es.
Zitat:
'CStudent' was not declared in this scope

sicher, dass du den Header eingebunden hast?
Auf diesen Beitrag antworten »
Tany

#include "CStudent.h"
#include <string>
#include <iostream>
using namespace std;



int main()
{
CStudent z1("Muster", "Fritz", 123456);
CStudent z2; //Variable anlegen
z2 = CStudent(z1);




}

Nur noch das jetzt .

Man braucht echt viel geduld.

Description Resource Path Location Type
call of overloaded 'CStudent()' is ambiguous main.cpp /CStudent line 19 C/C++ Problem
Auf diesen Beitrag antworten »
eulerscheZahl

Ach ja, das hatte ich dir zwar gelöscht, aber vergessen zu erwähnen:
code:
1:
2:
3:
CStudent::CStudent(){

}

Du hast schon
CStudent(std::string surname = "NoName", std::string firstname = "NoName", int numMatr = -1);
durch die Defaultwerte ist der andere Konstruktor darin enthalten, du schreibst also zweimal die selbe Funktion. Jetzt weiß der Compiler nicht, welche der beiden er nehmen soll.

Das zieht sich wirklich, über Skype oder ähnliches ginge es schneller.
Auf diesen Beitrag antworten »
Tany

Meinst du das ich einen default konstruktor anlegen soll?

P:S

Hast du irgendwie ICQ oder so ?
Oder kannst du dir anlegen ?
Auf diesen Beitrag antworten »
eulerscheZahl

Du hast den Konstruktor schon, du hast ihn sogar doppelt.
Den Teil, den ich im letzten Beitrag zitiert habe, musst du löschen.

ICQ: 418863782, schon ewig nicht mehr genutzt smile
Auf diesen Beitrag antworten »
Tany

ok .

Ich mache mir einen account bei icq und meld mich dann .
Auf diesen Beitrag antworten »
Tany

Würde das für den 2testfall so gehen ?

#include "CStudent.h"
#include <string>
#include <iostream>
using namespace std;



int main()
{
CStudent z1("Muster", "Fritz", 123456);
CStudent z2; //Variable anlegen
z2 = CStudent(z1);

CStudent z3("Meyer","Hans",12);
CStudent z4(z3);





}
Auf diesen Beitrag antworten »
eulerscheZahl

Sieht gut aus.

Was ist aus icq geworden? Ich hatte eine Anfrage von ******525, da antwortet aber keiner mehr.
Auf diesen Beitrag antworten »
tany

Keine Ahnung die nachrichten scheinen nicht anzukommen.

Hast du msn messenger?
Auf diesen Beitrag antworten »
eulerscheZahl

Download läuft...
Auf diesen Beitrag antworten »
tany

Kannst mir dann ja mail nennen.

Können bei skype chatten.
Auf diesen Beitrag antworten »
eulerscheZahl

Damit auch spätere Leser etwas davon haben, hier das Ergebnis des Chats:

code:
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;
}
 
Neue Frage »
Antworten »


Verwandte Themen

Die Beliebtesten »
Die Größten »
Die Neuesten »