Write a String class which has:
1) default constructor
2) copy constructor
3) destructor
4) equality operator similar to strcmp
5) constructor which takes a character array parameter
6) stream << operator

Answer Posted / stevewu

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


using namespace std;

class ownStrcmp
{
public:
ownStrcmp(){}
ownStrcmp(ownStrcmp& rhs);
ownStrcmp(char* instring){ _string = instring;}
void setString(char* instring){ _string = instring;}
char* getString(){return _string ;}
~ownStrcmp(){}
int operator == ( ownStrcmp &rhs);

private:
char* _string;
bool _ret;

};

ownStrcmp::ownStrcmp(ownStrcmp& rhs)
{
_string = rhs._string;
}
int ownStrcmp::operator == ( ownStrcmp &rhs)
{
_ret = true;
if(this == &rhs)
{
return _ret;
}
int i = 0;
while( _string[i] != NULL){ ++i;}
int stringLength = i;
for (int j=0;j<stringLength;j++)
{
if(_string[j]!=rhs._string[j]) _ret=false;
}
return _ret;
}


int main()
{
ownStrcmp string1("hello world");
ownStrcmp string2("hello world");
if (string1 == string2)
cout<<"result is true"<<endl;
else
cout<<"result is false"<<endl;
return 0;
}

Is This Answer Correct ?    5 Yes 5 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What do you mean by overhead in c++?

787


Using a smart pointer can we iterate through a container?

788


What is the benefit of c++?

779


What is the arrow operator in c++?

736


What are friend classes? What are advantages of using friend classes?

811






what Is DCS ? what i will get benefit when i did?

2019


Can we use this pointer inside static member function?

805


Does a derived class inherit or doesn't inherit?

806


write a c++ program to create class student having datamember name,Roll_no,age,and branch intilcization all the member using constructor print the all the details on the screen.

2439


What are smart pointers?

1188


Please explain the reference variable in c++?

778


When should we use container classes instead of arrays?

762


What are the various situations where a copy constructor is invoked?

806


What are the 2 main types of data structures?

782


How do you initialize a string in c++?

723