-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathString.cpp
81 lines (76 loc) · 1.83 KB
/
String.cpp
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
#include <iostream>
#include <string.h>
#include <assert.h>
using namespace std;
class String
{
public:
String(const char* s="");
String(const String& s);
String& operator=(const String& s);
int operator==(const String& s);
const char& operator[](int i){return chaine[i];}
~String(){delete chaine;}
friend ostream& operator<<(ostream& out,const String& s);
friend istream& operator>>(istream& out,String& s);
private:
char* chaine;
};
//*********** constructeur par défaut ************
String::String(const char* s){
chaine=new char[strlen(s)+1];
assert(chaine!=0);
strcpy(chaine,s);
}
//*********** constructeur ppar recopie ************
String::String(const String& s){
chaine=new char[strlen(s.chaine)+1];
assert(chaine!=0);
strcpy(chaine,s.chaine);
}
//*************** operator= ********************
String& String::operator=(const String& s){
if(this == &s) return *this;
delete chaine;
chaine=new char[strlen(s.chaine)+1];
assert(chaine!=0);
strcpy(chaine,s.chaine);
return *this;
}
//*************** operator== *******************
int String::operator==(const String& s){
if(this == &s) return 1;
return !strcmp(chaine,s.chaine);
}
//*************** operator<< *******************
ostream& operator<<(ostream& out,const String& s){
out<<s.chaine;
return out;
}
//*************** operator>> *******************
istream& operator>>(istream& in,String& s){
in>>s.chaine;
return in;
}
/*
test string
*/
int main()
{
String nom,prenom;
String nom2,prenom2;
cout<<"Nom:";
cin>>nom;
cout<<nom<<endl;
cout<<"Prenom:";
cin>>prenom;
cout<<prenom<<endl;
cout<<"Nom:";
cin>>nom2;
cout<<nom2<<endl;
cout<<"Prenom:";
cin>>prenom2;
cout<<prenom2<<endl;
cout<<(nom==nom2 && prenom==prenom2);
return 0;
}