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
What is the use of seekg in c++?
What is public, protected, private in c++?
Define a constructor - what it is and how it might be called (2 methods)?
int age=35; if(age>80) {Console.WriteLine("Boy you are old");} else {Console.WrieLine("That is a good age");}
Define stacks. Provide an example where they are useful.
How do I download c++?
Is it possible for the objects to read and write themselves?
What is the full form of dos?
How would you obtain segment and offset addresses from a far address of a memory location?
What is the use of map in c++?
Do class declarations end with a semicolon? Do class method definitions?
an integer constant must have atleast one a) character b) digit c) decimal point
Explain the virtual inheritance in c++.
What is the header file for setw?
In which situation the program terminates before reaching the breakpoint set by the user at the beginning of the mainq method?