SlideShare a Scribd company logo
C++ Algorithms
Prepared by
Mohammed Sikander
Team Lead
CranesVarsity
Mohammed Sikander www.cranessoftware.com
int main( )
{
char *ptr = "SIKANDER";
string str = "SIKANDER";
cout << sizeof(ptr) << endl;
cout << sizeof(str) << endl;
}
Mohammed Sikander www.cranessoftware.com
int main( )
{
char *ptr = "SIKANDER";
string str = "SIKANDER";
ptr[0] = ‘A’;
str[0] = 'A';
cout << ptr << endl;
cout << str << endl;
}
Mohammed Sikander www.cranessoftware.com
int main( )
{
char *ptr ;
string str ;
cin >> ptr;
cin >> str;
cout << ptr << endl;
cout << str << endl;
}
Mohammed Sikander www.cranessoftware.com
class MyString
{
char *ptr;
};
int main( )
{
MyString ms1;
cout << sizeof(ms1) << endl;
}
class MyString
{
char *ptr;
};
int main( )
{
MyString ms1;
cout << sizeof(ms1) << endl;
MyString ms2 ("ASDF");
}
Mohammed Sikander www.cranessoftware.com
class MyString
{
char *str;
public : MyString(const char *ptr)
{
int len = strlen(ptr);
str = new char[len + 1];
strcpy(str , ptr);
}
};
int main( )
{
MyString ms1;
cout << sizeof(ms1) << endl;
MyString ms2 ("ASDF");
}
class MyString
{
char *str;
public : MyString( )
{
str = NULL;
}
MyString(const char *ptr)
{
int len = strlen(ptr);
str = new char[len + 1];
strcpy(str , ptr);
}
};
Mohammed Sikander www.cranessoftware.com
class MyString
{
char *str;
public : MyString( )
{ str = NULL; }
MyString(char *ptr)
{
int len = strlen(ptr);
str = new char[len + 1];
strcpy(str , ptr);
}
char operator [ ](int index)
{
return str[index];
}
};
int main( )
{
MyString ms2 ("ASDF");
for(int i = 0 ; i < 4 ; i++)
{
ms2[i] = ms2[i] + 1;
cout << ms2[i] << endl;
}
}
int main( )
{
MyString ms2 ("ASDF");
for(int i = 0 ; i < 4 ; i++)
cout << ms2[i] << endl;
}
Mohammed Sikander www.cranessoftware.com
class MyString
{
char *str;
public : MyString( )
{ str = NULL; }
MyString(const char *ptr)
{
int len = strlen(ptr);
str = new char[len + 1];
strcpy(str , ptr);
}
char & operator [ ](int index)
{
return str[index];
}
};
int main( )
{
MyString ms2 ("ASDF");
for(int i = 0 ; i < 4 ; i++)
{
ms2[i] = ms2[i] + 1;
cout << ms2[i] << endl;
}
}
int main( )
{
MyString ms2 ("ASDF");
for(int i = 0 ; i < 4 ; i++)
cout << ms2[i] << endl;
}
Mohammed Sikander www.cranessoftware.com
MyString MyString ::operator +(const MyString &rhs) const
{
MyString temp;
int len = strlen(str) + strlen(rhs.str);
temp.str = new char[len + 1];
strcpy(temp.str , str);
strcat(temp.str , rhs.str);
return temp;
}
int main( )
{
MyString firstname = “MOHAMMED”;
MyString lastname = “SIKANDER”;
MyString fullname = firstname +lastname;
firstname.display() ;
lastname.display() ;
fullname.display();
}
class MyString
{
char *str;
public : //relevent constructors are provided
void display( )
{ cout << str << endl; }
MyString operator +(const MyString &rhs) const
};
Mohammed Sikander www.cranessoftware.com
class MyString
{
char *str;
public : //Assume require constructors are present
bool operator >(const MyString &rhs)
{ return strcmp(str , rhs.str) > 0 ? true : false; }
bool operator >=(const MyString &rhs)
{ return strcmp(str , rhs.str) >= 0 ? true : false;}
bool operator <(const MyString &rhs)
{ return strcmp(str , rhs.str) < 0 ? true : false; }
bool operator <=(const MyString &rhs)
{ return strcmp(str , rhs.str) <= 0 ? true : false; }
bool operator ==(const MyString &rhs)
{ return strcmp(str , rhs.str) == 0 ? true : false; }
bool operator !=(const MyString &rhs)
{ return strcmp(str , rhs.str) != 0 ? true : false; }
};
int main( )
{
MyString s1 = “BANGALORE”;
MyString s2= “DELHI”;
MyString s3 = “BANGALORE”;
cout << (s1 > s2 ? S1 : s2);
if(s1 == s3)
cout << “EQUAL”;
else
cout<<“NOT EQUAL”
}
Mohammed Sikander www.cranessoftware.com
class MyString
{
char *str;
public : MyString( )
{ str = NULL; }
MyString(const char *ptr)
{
int len = strlen(ptr);
str = new char[len + 1];
strcpy(str , ptr);
}
};
int main( )
{
MyString s1 = "SIKANDER";
return 0;
}
Run valgrind and detect memory leakage.
$valgrind ./a.out
Mohammed Sikander www.cranessoftware.com
class MyString
{ char *str;
public : MyString( )
{ str = NULL; }
MyString(const char *ptr)
{
int len = strlen(ptr);
str = new char[len + 1];
strcpy(str , ptr);
}
~MyString( )
{
delete [ ] str;
}
};
int main( )
{
MyString s1 = "SIKANDER";
return 0;
}
Run valgrind and detect memory leakage.
$valgrind ./a.out
Mohammed Sikander www.cranessoftware.com
class MyString
{
char *str;
public :
MyString(char *ptr)
{
cout <<“Constructor n”;
int len = strlen(ptr);
str = new char[len + 1];
strcpy(str , ptr);
}
~MyString( )
{
cout <<“Destructor ” << str;
delete [ ] str;
}
};
int main( )
{
MyString s1 = “SIKANDER”;
MyString s2 = s1;
}
Mohammed Sikander www.cranessoftware.com
class MyString
{
char *str;
public :
MyString(const char *ptr)
{
cout <<“Constructor n”;
int len = strlen(ptr);
str = new char[len + 1];
strcpy(str , ptr);
}
MyString(const MyString &old){
cout <<“Copy Constructor”;
str = new char[strlen(old.str) + 1];
strcpy(str , old.str);
}
~MyString( )
{
cout <<“Destructor n”;
delete [ ] str;
}
};
int main( )
{
MyString s1 = “SIKANDER”;
MyString s2 = s1;
}
Mohammed Sikander www.cranessoftware.com
class MyString
{ char *str;
public :
MyString( )
{ cout <<“Default Constructor n”;
str = NULL;
}
MyString(const char *ptr) {
cout <<“Constructor n”;
int len = strlen(ptr);
str = new char[len + 1];
strcpy(str , ptr);
}
~MyString( ) {
cout <<“Destructor n”;
delete [ ] str;
}
int main( )
{
MyString s1 = “SIKANDER”;
MyString s2;
s2 = s1;
}
MyString(const MyString &old){
cout <<“Copy Constructor”;
str = new char[strlen(old.str) + 1;
strcpy(str , old.str);
}
};
Mohammed Sikander www.cranessoftware.com
class MyString
{ char *str;
public :
MyString( )
{ cout <<“Default Constructor n”;
str = NULL;
}
MyString(const char *ptr) {
cout <<“Constructor n”;
int len = strlen(ptr);
str = new char[len + 1];
strcpy(str , ptr);
}
~MyString( ) {
cout <<“Destructor n”;
delete [ ] str;
}
MyString(const MyString &old){
cout <<“Copy Constructor”;
str = new char[strlen(old.str) + 1;
strcpy(str , old.str);
}
int main( )
{
MyString s1 = “SIKANDER”;
MyString s2;
s2 = s1;
}
MyString &operator = (const MyString &rhs)
{ cout <<“Assignment n”;
int len = strlen(rhs.str);
str = new char[len + 1];
strcpy(str , rhs.str);
return *this;
}
};
Mohammed Sikander www.cranessoftware.com
class MyString
{ char *str;
public :
MyString( )
{ cout <<“Default Constructor n”;
str = NULL;
}
MyString(const char *ptr) {
cout <<“Constructor n”;
int len = strlen(ptr);
str = new char[len + 1];
strcpy(str , ptr);
}
~MyString( ) {
cout <<“Destructor n”;
delete [ ] str;
}
MyString(const MyString &old){
cout <<“Copy Constructor”;
str = new char[strlen(old.str) + 1;
strcpy(str , old.str);
}
int main( )
{
MyString s1 = “SIKANDER”;
MyString s2 = “CHETHAN”;
s2 = s1;
}
MyString &operator = (const MyString &rhs)
{ cout <<“Assignment n”;
int len = strlen(rhs.str);
str = new char[len + 1];
strcpy(str , rhs.str);
return *this;
}
};
Mohammed Sikander www.cranessoftware.com
class MyString
{ char *str;
public :
MyString( )
{ cout <<“Default Constructor n”;
str = NULL;
}
MyString(const char *ptr) {
cout <<“Constructor n”;
int len = strlen(ptr);
str = new char[len + 1];
strcpy(str , ptr);
}
~MyString( ) {
cout <<“Destructor n”;
delete [ ] str;
}
MyString(const MyString &old){
cout <<“Copy Constructor”;
str = new char[strlen(old.str) + 1;
strcpy(str , old.str);
}
int main( )
{
MyString s1 = “SIKANDER”;
MyString s2 = “CHETHAN”;
s2 = s1;
}
MyString & operator = (const MyString &rhs)
{
this->~MyString();
int len = strlen(rhs.str);
str = new char[len + 1];
strcpy(str , rhs.str);
return *this;
}
};
Mohammed Sikander www.cranessoftware.com
class MyString
{ char *str;
public :
MyString( )
{ cout <<“Default Constructor n”;
str = NULL;
}
MyString(const char *ptr) {
cout <<“Constructor n”;
int len = strlen(ptr);
str = new char[len + 1];
strcpy(str , ptr);
}
~MyString( ) {
cout <<“Destructor n”;
delete [ ] str;
}
MyString(const MyString &old){
cout <<“Copy Constructor”;
str = new char[strlen(old.str) + 1;
strcpy(str , old.str);
}
int main( )
{
MyString s1 = “SIKANDER”;
MyString s2 = “CHETHAN”;
s2 = s2;
}
MyString & operator = (const MyString &rhs)
{
this->~MyString();
int len = strlen(rhs.str);
str = new char[len + 1];
strcpy(str , rhs.str);
return *this;
}
};
Mohammed Sikander www.cranessoftware.com
class MyString
{ char *str;
public :
MyString( )
{ cout <<“Default Constructor n”;
str = NULL;
}
MyString(const char *ptr) {
cout <<“Constructor n”;
int len = strlen(ptr);
str = new char[len + 1];
strcpy(str , ptr);
}
~MyString( ) {
cout <<“Destructor n”;
delete [ ] str;
}
MyString(const MyString &old){
cout <<“Copy Constructor”;
str = new char[strlen(old.str) + 1;
strcpy(str , old.str);
}
int main( )
{
MyString s1 = “SIKANDER”;
MyString s2 = “CHETHAN”;
s2 = s2;
}
MyString & operator = (const MyString &rhs)
{
if(this != &rhs)
{
this->~MyString();
int len = strlen(rhs.str);
str = new char[len + 1];
strcpy(str , rhs.str);
}
return *this;
}
};
Mohammed Sikander www.cranessoftware.com
class MyString
{ char *str;
public :
MyString( );
MyString(const char *ptr);
~MyString( );
MyString(const MyString &old);
friend ostream &operator << (ostream & , const MyString &);
friend istream &operator >> (istream & , MyString &);
};
int main( )
{
MyString s1;
MyString s2;
cout <<“Enter the string : “;
cin >> s1;
cout << s1<< endl;
}
ostream &operator << (ostream &out, const MyString &s)
{
out << s.str ;
return out;
}
istream &operator >> (istream &in, MyString &s)
{
char buffer[1024];
in >> buffer;
str = new char[strlen(buffer) + 1];
strcpy(str , buffer);
}
Mohammed Sikander www.cranessoftware.com
 For feedback and suggestions.
 Mail me at
mohammed.sikander@cranessoftware.com;

More Related Content

What's hot

Implementing stack
Implementing stackImplementing stack
Implementing stack
mohamed sikander
 
C++ Question on References and Function Overloading
C++ Question on References and Function OverloadingC++ Question on References and Function Overloading
C++ Question on References and Function Overloading
mohamed sikander
 
Inheritance and polymorphism
Inheritance and polymorphismInheritance and polymorphism
Inheritance and polymorphism
mohamed sikander
 
Container adapters
Container adaptersContainer adapters
Container adapters
mohamed sikander
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
mohamed sikander
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
mohamed sikander
 
C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st Study
Chris Ohk
 
C++ Programming - 11th Study
C++ Programming - 11th StudyC++ Programming - 11th Study
C++ Programming - 11th Study
Chris Ohk
 
Pointer level 2
Pointer   level 2Pointer   level 2
Pointer level 2
Mohammed Sikander
 
C programs
C programsC programs
C programs
Vikram Nandini
 
Double linked list
Double linked listDouble linked list
Double linked list
Sayantan Sur
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File
Harjinder Singh
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
Chris Ohk
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd Study
Chris Ohk
 
Double linked list
Double linked listDouble linked list
Double linked list
raviahuja11
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++
Bharat Kalia
 
3. chapter ii
3. chapter ii3. chapter ii
3. chapter ii
Chhom Karath
 
4. chapter iii
4. chapter iii4. chapter iii
4. chapter iii
Chhom Karath
 

What's hot (20)

Implementing stack
Implementing stackImplementing stack
Implementing stack
 
C++ Question on References and Function Overloading
C++ Question on References and Function OverloadingC++ Question on References and Function Overloading
C++ Question on References and Function Overloading
 
Inheritance and polymorphism
Inheritance and polymorphismInheritance and polymorphism
Inheritance and polymorphism
 
Container adapters
Container adaptersContainer adapters
Container adapters
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st Study
 
C++ Programming - 11th Study
C++ Programming - 11th StudyC++ Programming - 11th Study
C++ Programming - 11th Study
 
Pointer level 2
Pointer   level 2Pointer   level 2
Pointer level 2
 
C programs
C programsC programs
C programs
 
Double linked list
Double linked listDouble linked list
Double linked list
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd Study
 
Vcs16
Vcs16Vcs16
Vcs16
 
Double linked list
Double linked listDouble linked list
Double linked list
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++
 
C++ programs
C++ programsC++ programs
C++ programs
 
3. chapter ii
3. chapter ii3. chapter ii
3. chapter ii
 
4. chapter iii
4. chapter iii4. chapter iii
4. chapter iii
 

Viewers also liked

Kti irnawati baco akbid paramata
Kti irnawati baco akbid paramataKti irnawati baco akbid paramata
Kti irnawati baco akbid paramata
Operator Warnet Vast Raha
 
CROWNFUNDING
CROWNFUNDINGCROWNFUNDING
CROWNFUNDING
Dimitris Krigos
 
Turki saud
Turki saudTurki saud
Turki saud
turki saud
 
Sejarah benzena
Sejarah benzenaSejarah benzena
Sejarah benzena
Rofiq Nie
 
Experience Letter
Experience LetterExperience Letter
Experience Letter
Priyadarshani Jain
 
Metode penugasan fungsional
Metode penugasan fungsionalMetode penugasan fungsional
Metode penugasan fungsional
Sulistia Rini
 
Managing Conflicts
Managing ConflictsManaging Conflicts
Managing Conflictsikonick
 
Askeb bayi fisiologis 7 langkah
Askeb bayi fisiologis 7 langkahAskeb bayi fisiologis 7 langkah
Askeb bayi fisiologis 7 langkah
Warnet Raha
 
Kti ruli desta
Kti ruli destaKti ruli desta
Kti ruli desta
Karya Tulis Ilmiah
 
IDENTIFIKASI KARAKTERISTIK IBU BERSALINDENGANPOSTTERMDI RUANG DELIMA RUMAH SA...
IDENTIFIKASI KARAKTERISTIK IBU BERSALINDENGANPOSTTERMDI RUANG DELIMA RUMAH SA...IDENTIFIKASI KARAKTERISTIK IBU BERSALINDENGANPOSTTERMDI RUANG DELIMA RUMAH SA...
IDENTIFIKASI KARAKTERISTIK IBU BERSALINDENGANPOSTTERMDI RUANG DELIMA RUMAH SA...
Warnet Raha
 
eGovernance mechanism with BioMetrics Classification and Authentication for D...
eGovernance mechanism with BioMetrics Classification and Authentication for D...eGovernance mechanism with BioMetrics Classification and Authentication for D...
eGovernance mechanism with BioMetrics Classification and Authentication for D...
Association of Scientists, Developers and Faculties
 
Astute's PeopleSoft Integration Testing Utilities and Tools Implementation
Astute's PeopleSoft Integration Testing Utilities and Tools ImplementationAstute's PeopleSoft Integration Testing Utilities and Tools Implementation
Astute's PeopleSoft Integration Testing Utilities and Tools Implementation
Beastute
 
PTF Presentation
PTF PresentationPTF Presentation
PTF PresentationTelly Ipock
 
Kti nur vita budirman akbid paramata
Kti nur vita budirman akbid paramataKti nur vita budirman akbid paramata
Kti nur vita budirman akbid paramata
Operator Warnet Vast Raha
 
MANAJEMEN DAN PENDOKUMENTASIAN ASUHAN KEBIDANAN IBU HAMIL PADA NY. “S” DENGAN...
MANAJEMEN DAN PENDOKUMENTASIAN ASUHAN KEBIDANAN IBU HAMIL PADA NY. “S” DENGAN...MANAJEMEN DAN PENDOKUMENTASIAN ASUHAN KEBIDANAN IBU HAMIL PADA NY. “S” DENGAN...
MANAJEMEN DAN PENDOKUMENTASIAN ASUHAN KEBIDANAN IBU HAMIL PADA NY. “S” DENGAN...
Warnet Raha
 
PeopleSoft test framework
PeopleSoft test frameworkPeopleSoft test framework
PeopleSoft test framework
Francis Manoharan
 

Viewers also liked (16)

Kti irnawati baco akbid paramata
Kti irnawati baco akbid paramataKti irnawati baco akbid paramata
Kti irnawati baco akbid paramata
 
CROWNFUNDING
CROWNFUNDINGCROWNFUNDING
CROWNFUNDING
 
Turki saud
Turki saudTurki saud
Turki saud
 
Sejarah benzena
Sejarah benzenaSejarah benzena
Sejarah benzena
 
Experience Letter
Experience LetterExperience Letter
Experience Letter
 
Metode penugasan fungsional
Metode penugasan fungsionalMetode penugasan fungsional
Metode penugasan fungsional
 
Managing Conflicts
Managing ConflictsManaging Conflicts
Managing Conflicts
 
Askeb bayi fisiologis 7 langkah
Askeb bayi fisiologis 7 langkahAskeb bayi fisiologis 7 langkah
Askeb bayi fisiologis 7 langkah
 
Kti ruli desta
Kti ruli destaKti ruli desta
Kti ruli desta
 
IDENTIFIKASI KARAKTERISTIK IBU BERSALINDENGANPOSTTERMDI RUANG DELIMA RUMAH SA...
IDENTIFIKASI KARAKTERISTIK IBU BERSALINDENGANPOSTTERMDI RUANG DELIMA RUMAH SA...IDENTIFIKASI KARAKTERISTIK IBU BERSALINDENGANPOSTTERMDI RUANG DELIMA RUMAH SA...
IDENTIFIKASI KARAKTERISTIK IBU BERSALINDENGANPOSTTERMDI RUANG DELIMA RUMAH SA...
 
eGovernance mechanism with BioMetrics Classification and Authentication for D...
eGovernance mechanism with BioMetrics Classification and Authentication for D...eGovernance mechanism with BioMetrics Classification and Authentication for D...
eGovernance mechanism with BioMetrics Classification and Authentication for D...
 
Astute's PeopleSoft Integration Testing Utilities and Tools Implementation
Astute's PeopleSoft Integration Testing Utilities and Tools ImplementationAstute's PeopleSoft Integration Testing Utilities and Tools Implementation
Astute's PeopleSoft Integration Testing Utilities and Tools Implementation
 
PTF Presentation
PTF PresentationPTF Presentation
PTF Presentation
 
Kti nur vita budirman akbid paramata
Kti nur vita budirman akbid paramataKti nur vita budirman akbid paramata
Kti nur vita budirman akbid paramata
 
MANAJEMEN DAN PENDOKUMENTASIAN ASUHAN KEBIDANAN IBU HAMIL PADA NY. “S” DENGAN...
MANAJEMEN DAN PENDOKUMENTASIAN ASUHAN KEBIDANAN IBU HAMIL PADA NY. “S” DENGAN...MANAJEMEN DAN PENDOKUMENTASIAN ASUHAN KEBIDANAN IBU HAMIL PADA NY. “S” DENGAN...
MANAJEMEN DAN PENDOKUMENTASIAN ASUHAN KEBIDANAN IBU HAMIL PADA NY. “S” DENGAN...
 
PeopleSoft test framework
PeopleSoft test frameworkPeopleSoft test framework
PeopleSoft test framework
 

Similar to Implementing string

Microsoft Word Hw#1
Microsoft Word   Hw#1Microsoft Word   Hw#1
Microsoft Word Hw#1
kkkseld
 
oodp elab.pdf
oodp elab.pdfoodp elab.pdf
oodp elab.pdf
SWATIKUMARIRA2111030
 
Bind me if you can
Bind me if you canBind me if you can
Bind me if you can
Ovidiu Farauanu
 
include.docx
include.docxinclude.docx
include.docx
NhiPtaa
 
Nested micro
Nested microNested micro
Nested micro
Satyamevjayte Haxor
 
Asssignment2
Asssignment2 Asssignment2
Asssignment2
AnnamalikAnnamalik
 
CBSE Class XI Programming in C++
CBSE Class XI Programming in C++CBSE Class XI Programming in C++
CBSE Class XI Programming in C++
Pranav Ghildiyal
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
ssuserd6b1fd
 
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
GkhanGirgin3
 
#include Status.hnamespace sdds{StatusStatus(c
#include Status.hnamespace sdds{StatusStatus(c#include Status.hnamespace sdds{StatusStatus(c
#include Status.hnamespace sdds{StatusStatus(c
MoseStaton39
 
#include Status.hnamespace sdds{StatusStatus(c
#include Status.hnamespace sdds{StatusStatus(c#include Status.hnamespace sdds{StatusStatus(c
#include Status.hnamespace sdds{StatusStatus(c
SilvaGraf83
 
#include Status.hnamespace sdds{StatusStatus(c
#include Status.hnamespace sdds{StatusStatus(c#include Status.hnamespace sdds{StatusStatus(c
#include Status.hnamespace sdds{StatusStatus(c
SilvaGraf83
 
array, function, pointer, pattern matching
array, function, pointer, pattern matchingarray, function, pointer, pattern matching
array, function, pointer, pattern matching
Shakila Mahjabin
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
ssuser3cbb4c
 
Write Python for Speed
Write Python for SpeedWrite Python for Speed
Write Python for Speed
Yung-Yu Chen
 
Статичный SQL в С++14. Евгений Захаров ➠ CoreHard Autumn 2019
Статичный SQL в С++14. Евгений Захаров ➠  CoreHard Autumn 2019Статичный SQL в С++14. Евгений Захаров ➠  CoreHard Autumn 2019
Статичный SQL в С++14. Евгений Захаров ➠ CoreHard Autumn 2019
corehard_by
 
Alexey Tsoy Meta Programming in C++ 16.11.17
Alexey Tsoy Meta Programming in C++ 16.11.17Alexey Tsoy Meta Programming in C++ 16.11.17
Alexey Tsoy Meta Programming in C++ 16.11.17
LogeekNightUkraine
 

Similar to Implementing string (20)

Microsoft Word Hw#1
Microsoft Word   Hw#1Microsoft Word   Hw#1
Microsoft Word Hw#1
 
oodp elab.pdf
oodp elab.pdfoodp elab.pdf
oodp elab.pdf
 
Bind me if you can
Bind me if you canBind me if you can
Bind me if you can
 
include.docx
include.docxinclude.docx
include.docx
 
Nested micro
Nested microNested micro
Nested micro
 
Asssignment2
Asssignment2 Asssignment2
Asssignment2
 
Lecture5
Lecture5Lecture5
Lecture5
 
CBSE Class XI Programming in C++
CBSE Class XI Programming in C++CBSE Class XI Programming in C++
CBSE Class XI Programming in C++
 
Overloading
OverloadingOverloading
Overloading
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
 
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
 
#include Status.hnamespace sdds{StatusStatus(c
#include Status.hnamespace sdds{StatusStatus(c#include Status.hnamespace sdds{StatusStatus(c
#include Status.hnamespace sdds{StatusStatus(c
 
#include Status.hnamespace sdds{StatusStatus(c
#include Status.hnamespace sdds{StatusStatus(c#include Status.hnamespace sdds{StatusStatus(c
#include Status.hnamespace sdds{StatusStatus(c
 
#include Status.hnamespace sdds{StatusStatus(c
#include Status.hnamespace sdds{StatusStatus(c#include Status.hnamespace sdds{StatusStatus(c
#include Status.hnamespace sdds{StatusStatus(c
 
array, function, pointer, pattern matching
array, function, pointer, pattern matchingarray, function, pointer, pattern matching
array, function, pointer, pattern matching
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
 
Vcs29
Vcs29Vcs29
Vcs29
 
Write Python for Speed
Write Python for SpeedWrite Python for Speed
Write Python for Speed
 
Статичный SQL в С++14. Евгений Захаров ➠ CoreHard Autumn 2019
Статичный SQL в С++14. Евгений Захаров ➠  CoreHard Autumn 2019Статичный SQL в С++14. Евгений Захаров ➠  CoreHard Autumn 2019
Статичный SQL в С++14. Евгений Захаров ➠ CoreHard Autumn 2019
 
Alexey Tsoy Meta Programming in C++ 16.11.17
Alexey Tsoy Meta Programming in C++ 16.11.17Alexey Tsoy Meta Programming in C++ 16.11.17
Alexey Tsoy Meta Programming in C++ 16.11.17
 

Recently uploaded

Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
Tendenci - The Open Source AMS (Association Management Software)
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
Ortus Solutions, Corp
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
RISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent EnterpriseRISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent Enterprise
Srikant77
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdfEnhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Jay Das
 

Recently uploaded (20)

Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
RISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent EnterpriseRISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent Enterprise
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdfEnhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
 

Implementing string

  • 1. C++ Algorithms Prepared by Mohammed Sikander Team Lead CranesVarsity
  • 2. Mohammed Sikander www.cranessoftware.com int main( ) { char *ptr = "SIKANDER"; string str = "SIKANDER"; cout << sizeof(ptr) << endl; cout << sizeof(str) << endl; }
  • 3. Mohammed Sikander www.cranessoftware.com int main( ) { char *ptr = "SIKANDER"; string str = "SIKANDER"; ptr[0] = ‘A’; str[0] = 'A'; cout << ptr << endl; cout << str << endl; }
  • 4. Mohammed Sikander www.cranessoftware.com int main( ) { char *ptr ; string str ; cin >> ptr; cin >> str; cout << ptr << endl; cout << str << endl; }
  • 5. Mohammed Sikander www.cranessoftware.com class MyString { char *ptr; }; int main( ) { MyString ms1; cout << sizeof(ms1) << endl; } class MyString { char *ptr; }; int main( ) { MyString ms1; cout << sizeof(ms1) << endl; MyString ms2 ("ASDF"); }
  • 6. Mohammed Sikander www.cranessoftware.com class MyString { char *str; public : MyString(const char *ptr) { int len = strlen(ptr); str = new char[len + 1]; strcpy(str , ptr); } }; int main( ) { MyString ms1; cout << sizeof(ms1) << endl; MyString ms2 ("ASDF"); } class MyString { char *str; public : MyString( ) { str = NULL; } MyString(const char *ptr) { int len = strlen(ptr); str = new char[len + 1]; strcpy(str , ptr); } };
  • 7. Mohammed Sikander www.cranessoftware.com class MyString { char *str; public : MyString( ) { str = NULL; } MyString(char *ptr) { int len = strlen(ptr); str = new char[len + 1]; strcpy(str , ptr); } char operator [ ](int index) { return str[index]; } }; int main( ) { MyString ms2 ("ASDF"); for(int i = 0 ; i < 4 ; i++) { ms2[i] = ms2[i] + 1; cout << ms2[i] << endl; } } int main( ) { MyString ms2 ("ASDF"); for(int i = 0 ; i < 4 ; i++) cout << ms2[i] << endl; }
  • 8. Mohammed Sikander www.cranessoftware.com class MyString { char *str; public : MyString( ) { str = NULL; } MyString(const char *ptr) { int len = strlen(ptr); str = new char[len + 1]; strcpy(str , ptr); } char & operator [ ](int index) { return str[index]; } }; int main( ) { MyString ms2 ("ASDF"); for(int i = 0 ; i < 4 ; i++) { ms2[i] = ms2[i] + 1; cout << ms2[i] << endl; } } int main( ) { MyString ms2 ("ASDF"); for(int i = 0 ; i < 4 ; i++) cout << ms2[i] << endl; }
  • 9. Mohammed Sikander www.cranessoftware.com MyString MyString ::operator +(const MyString &rhs) const { MyString temp; int len = strlen(str) + strlen(rhs.str); temp.str = new char[len + 1]; strcpy(temp.str , str); strcat(temp.str , rhs.str); return temp; } int main( ) { MyString firstname = “MOHAMMED”; MyString lastname = “SIKANDER”; MyString fullname = firstname +lastname; firstname.display() ; lastname.display() ; fullname.display(); } class MyString { char *str; public : //relevent constructors are provided void display( ) { cout << str << endl; } MyString operator +(const MyString &rhs) const };
  • 10. Mohammed Sikander www.cranessoftware.com class MyString { char *str; public : //Assume require constructors are present bool operator >(const MyString &rhs) { return strcmp(str , rhs.str) > 0 ? true : false; } bool operator >=(const MyString &rhs) { return strcmp(str , rhs.str) >= 0 ? true : false;} bool operator <(const MyString &rhs) { return strcmp(str , rhs.str) < 0 ? true : false; } bool operator <=(const MyString &rhs) { return strcmp(str , rhs.str) <= 0 ? true : false; } bool operator ==(const MyString &rhs) { return strcmp(str , rhs.str) == 0 ? true : false; } bool operator !=(const MyString &rhs) { return strcmp(str , rhs.str) != 0 ? true : false; } }; int main( ) { MyString s1 = “BANGALORE”; MyString s2= “DELHI”; MyString s3 = “BANGALORE”; cout << (s1 > s2 ? S1 : s2); if(s1 == s3) cout << “EQUAL”; else cout<<“NOT EQUAL” }
  • 11. Mohammed Sikander www.cranessoftware.com class MyString { char *str; public : MyString( ) { str = NULL; } MyString(const char *ptr) { int len = strlen(ptr); str = new char[len + 1]; strcpy(str , ptr); } }; int main( ) { MyString s1 = "SIKANDER"; return 0; } Run valgrind and detect memory leakage. $valgrind ./a.out
  • 12. Mohammed Sikander www.cranessoftware.com class MyString { char *str; public : MyString( ) { str = NULL; } MyString(const char *ptr) { int len = strlen(ptr); str = new char[len + 1]; strcpy(str , ptr); } ~MyString( ) { delete [ ] str; } }; int main( ) { MyString s1 = "SIKANDER"; return 0; } Run valgrind and detect memory leakage. $valgrind ./a.out
  • 13. Mohammed Sikander www.cranessoftware.com class MyString { char *str; public : MyString(char *ptr) { cout <<“Constructor n”; int len = strlen(ptr); str = new char[len + 1]; strcpy(str , ptr); } ~MyString( ) { cout <<“Destructor ” << str; delete [ ] str; } }; int main( ) { MyString s1 = “SIKANDER”; MyString s2 = s1; }
  • 14. Mohammed Sikander www.cranessoftware.com class MyString { char *str; public : MyString(const char *ptr) { cout <<“Constructor n”; int len = strlen(ptr); str = new char[len + 1]; strcpy(str , ptr); } MyString(const MyString &old){ cout <<“Copy Constructor”; str = new char[strlen(old.str) + 1]; strcpy(str , old.str); } ~MyString( ) { cout <<“Destructor n”; delete [ ] str; } }; int main( ) { MyString s1 = “SIKANDER”; MyString s2 = s1; }
  • 15. Mohammed Sikander www.cranessoftware.com class MyString { char *str; public : MyString( ) { cout <<“Default Constructor n”; str = NULL; } MyString(const char *ptr) { cout <<“Constructor n”; int len = strlen(ptr); str = new char[len + 1]; strcpy(str , ptr); } ~MyString( ) { cout <<“Destructor n”; delete [ ] str; } int main( ) { MyString s1 = “SIKANDER”; MyString s2; s2 = s1; } MyString(const MyString &old){ cout <<“Copy Constructor”; str = new char[strlen(old.str) + 1; strcpy(str , old.str); } };
  • 16. Mohammed Sikander www.cranessoftware.com class MyString { char *str; public : MyString( ) { cout <<“Default Constructor n”; str = NULL; } MyString(const char *ptr) { cout <<“Constructor n”; int len = strlen(ptr); str = new char[len + 1]; strcpy(str , ptr); } ~MyString( ) { cout <<“Destructor n”; delete [ ] str; } MyString(const MyString &old){ cout <<“Copy Constructor”; str = new char[strlen(old.str) + 1; strcpy(str , old.str); } int main( ) { MyString s1 = “SIKANDER”; MyString s2; s2 = s1; } MyString &operator = (const MyString &rhs) { cout <<“Assignment n”; int len = strlen(rhs.str); str = new char[len + 1]; strcpy(str , rhs.str); return *this; } };
  • 17. Mohammed Sikander www.cranessoftware.com class MyString { char *str; public : MyString( ) { cout <<“Default Constructor n”; str = NULL; } MyString(const char *ptr) { cout <<“Constructor n”; int len = strlen(ptr); str = new char[len + 1]; strcpy(str , ptr); } ~MyString( ) { cout <<“Destructor n”; delete [ ] str; } MyString(const MyString &old){ cout <<“Copy Constructor”; str = new char[strlen(old.str) + 1; strcpy(str , old.str); } int main( ) { MyString s1 = “SIKANDER”; MyString s2 = “CHETHAN”; s2 = s1; } MyString &operator = (const MyString &rhs) { cout <<“Assignment n”; int len = strlen(rhs.str); str = new char[len + 1]; strcpy(str , rhs.str); return *this; } };
  • 18. Mohammed Sikander www.cranessoftware.com class MyString { char *str; public : MyString( ) { cout <<“Default Constructor n”; str = NULL; } MyString(const char *ptr) { cout <<“Constructor n”; int len = strlen(ptr); str = new char[len + 1]; strcpy(str , ptr); } ~MyString( ) { cout <<“Destructor n”; delete [ ] str; } MyString(const MyString &old){ cout <<“Copy Constructor”; str = new char[strlen(old.str) + 1; strcpy(str , old.str); } int main( ) { MyString s1 = “SIKANDER”; MyString s2 = “CHETHAN”; s2 = s1; } MyString & operator = (const MyString &rhs) { this->~MyString(); int len = strlen(rhs.str); str = new char[len + 1]; strcpy(str , rhs.str); return *this; } };
  • 19. Mohammed Sikander www.cranessoftware.com class MyString { char *str; public : MyString( ) { cout <<“Default Constructor n”; str = NULL; } MyString(const char *ptr) { cout <<“Constructor n”; int len = strlen(ptr); str = new char[len + 1]; strcpy(str , ptr); } ~MyString( ) { cout <<“Destructor n”; delete [ ] str; } MyString(const MyString &old){ cout <<“Copy Constructor”; str = new char[strlen(old.str) + 1; strcpy(str , old.str); } int main( ) { MyString s1 = “SIKANDER”; MyString s2 = “CHETHAN”; s2 = s2; } MyString & operator = (const MyString &rhs) { this->~MyString(); int len = strlen(rhs.str); str = new char[len + 1]; strcpy(str , rhs.str); return *this; } };
  • 20. Mohammed Sikander www.cranessoftware.com class MyString { char *str; public : MyString( ) { cout <<“Default Constructor n”; str = NULL; } MyString(const char *ptr) { cout <<“Constructor n”; int len = strlen(ptr); str = new char[len + 1]; strcpy(str , ptr); } ~MyString( ) { cout <<“Destructor n”; delete [ ] str; } MyString(const MyString &old){ cout <<“Copy Constructor”; str = new char[strlen(old.str) + 1; strcpy(str , old.str); } int main( ) { MyString s1 = “SIKANDER”; MyString s2 = “CHETHAN”; s2 = s2; } MyString & operator = (const MyString &rhs) { if(this != &rhs) { this->~MyString(); int len = strlen(rhs.str); str = new char[len + 1]; strcpy(str , rhs.str); } return *this; } };
  • 21. Mohammed Sikander www.cranessoftware.com class MyString { char *str; public : MyString( ); MyString(const char *ptr); ~MyString( ); MyString(const MyString &old); friend ostream &operator << (ostream & , const MyString &); friend istream &operator >> (istream & , MyString &); }; int main( ) { MyString s1; MyString s2; cout <<“Enter the string : “; cin >> s1; cout << s1<< endl; } ostream &operator << (ostream &out, const MyString &s) { out << s.str ; return out; } istream &operator >> (istream &in, MyString &s) { char buffer[1024]; in >> buffer; str = new char[strlen(buffer) + 1]; strcpy(str , buffer); }
  • 22. Mohammed Sikander www.cranessoftware.com  For feedback and suggestions.  Mail me at mohammed.sikander@cranessoftware.com;