-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringConstructor.cpp
More file actions
79 lines (68 loc) · 1.24 KB
/
Copy pathstringConstructor.cpp
File metadata and controls
79 lines (68 loc) · 1.24 KB
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
#include<iostream>
using namespace std;
#include<string.h>
class String
{
char* cStr;
int len;
public:
String()
{
cout<<"default constructor is called \n";
cStr=NULL;
len=0;
}
String(const char* p)
{
cout<<"parameterised constructor is called\n";
if(p==NULL)
{
cStr=NULL;
len=0;
}
else
{
len=strlen(p);
this->cStr=new char[len+1];
strcpy(cStr,p);
}
}
String(const String& ss)
{
cout<<"copy constructor is called \n";
if(ss.cStr==NULL)
{
cStr=NULL;
len=0;
}
else
{
len=strlen(ss.cStr);
cStr=new char[len+1];
strcpy(cStr,ss.cStr);
}
}
char* getString()
{
if(cStr==NULL)
{
cout<<"string is empty\n";
return NULL;
}
return cStr;
}
~String()
{
delete []cStr;
}
};
int main()
{
String s1("NAVEEN");
String s2=s1; //s2(s1) both are same
String s3;
cout<<s1.getString()<<endl;
cout<<s2.getString()<<endl;
cout<<s3.getString()<<endl;
return 0;
}