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

Explain some examples of operator overloading?

645


Differentiate between a pointer and a reference with respect to c++.

695


Can I learn c++ as my first language?

609


Explain the advantages of using friend classes.

585


What is the difference between delegation and implemented-in-terms-of?

521






Is c++ vector a linked list?

551


What is struct c++?

572


How can you quickly find the number of elements stored in a dynamic array? Why is it difficult to store linked list in an array?

560


Which software is best for coding?

573


How will you call C functions from C ++ and vice-versa?

661


How would you implement a substr() function that extracts a sub string from a given string?

565


Explain rethrowing exceptions with an example?

608


How can you differentiate between inheritance and implementation in c++?

640


Are php strings immutable?

560


Explain about Virtual Function in C++?

613