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

Answers were Sorted based on User's Feedback



Write a String class which has: 1) default constructor 2) copy constructor 3) destructor 4) equa..

Answer / jp

#include "stdafx.h"
#include <iostream>
using namespace std;

/********************************************************************/
//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
/********************************************************************/

class strclass
{
public:
strclass()
{
_string = new char();
}
strclass(const strclass& strcls)
{
_string = strcls._string;
}
bool operator==(const strclass& cls)
{
if(this->_string == cls._string)
return true;
else
return false;
}
strclass(char* arr)
{
_string = arr;
}
~strclass()
{
//delete _string;
_string = NULL;
}
char* operator<<(const strclass& cls)
{
return this->_string = cls._string;
}
private:
char* _string;

};
int main()
{
cout << "Hi";
strclass str1("string1");
strclass str2("string1");
strclass str("string2");

if (str1 == str2)
cout << "Strings are Same";
else
cout << "Strings are Different";

cout << (str1 << str);
return 0;
}

Is This Answer Correct ?    1 Yes 0 No

Write a String class which has: 1) default constructor 2) copy constructor 3) destructor 4) equa..

Answer / 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

More C++ General Interview Questions

What's the order in which the local objects are destructed?

0 Answers  


What is the difference between an enumeration and a set of pre-processor # defines?

0 Answers  


How do you compile the source code with your compiler?

0 Answers  


What does extern mean in a function declaration in c++?

0 Answers  


Where are setjmp and longjmp used in c++?

0 Answers  






Write a program to reverse a linked list?

8 Answers   Catalytic Software, Satyam,


class Foo { const int x; protected: Foo(int f); ~Foo(); }; Foo f; Referring to the sample code above, why will the class declaration not compile? a) The variable x is const. b) The destructor is protected. c) The destructor is not public. d) The constructor is protected. e) There is no default constructor.

5 Answers   Quark,


Does defining a function inline mean that it wont push and pop things on/off the stack ...like parameters and the return the address??

2 Answers  


can output 5 students using one dimensional array

1 Answers   Intel,


What is the difference between C and CPP?

0 Answers   iNautix,


What is Memory Alignment?

2 Answers   TCS,


What is the difference between #import and #include?

0 Answers  


Categories