-
Notifications
You must be signed in to change notification settings - Fork 2
/
implementStringClass.cpp
100 lines (87 loc) · 2.02 KB
/
implementStringClass.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
author : aniket
*/
#include "iostream"
using namespace std;
class stringTemp
{
public:
char *s;
int length;
//default constructor
stringTemp()
{
s = NULL;
}
//parameterised constructor
stringTemp(char input[])
{
length= 0;
//cal the length of string
while(input[length] !='\0'){
length++;
}
this->s = new char[length];//assign the memory to the pointer
int i =0;
//stored the string inside the pointer.
while(i != length){
s[i] = input[i];
i++;
}
}
//desturctor
~stringTemp()
{
if (s != NULL)
{
delete s;
s = NULL;
}
}
//copy constructor
stringTemp(const stringTemp &obj)
{
this->s = obj.s;
this->length = obj.length;
}
//move constructor
stringTemp(stringTemp &&obj)
{
cout<<"I am in a move constructor"<<endl;
this->s = obj.s;
this->length = obj.length;
obj.length = 0;
obj.s = NULL;
}
//string length
int slength()
{
return this->length;
}
//assign operator
stringTemp &operator=(const stringTemp &obj)
{
this->s = obj.s;
this->length = obj.length;
return *this;
}
};
int main()
{
stringTemp obj("aniket");
cout <<"length of obj string : "<<obj.slength()<<endl;//calculate the length of string
stringTemp obj1= obj;//copy constructor(copy the string)
cout <<"length of obj1 string : "<<obj1.slength()<<endl;
stringTemp obj2;
obj2 = obj1;//assign operator(assign the string)
cout <<"length of obj2 string : "<<obj2.slength()<<endl;
stringTemp obj5= move(obj1);//move constructor(move the string)
cout <<"length of obj5 string : "<<obj5.slength()<<endl;
}
/* output -
length of obj string : 6
length of obj1 string : 6
length of obj2 string : 6
I am in a move constructor
length of obj5 string : 6
*/