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 / 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 |
Post New Answer View All Answers
Why is main function important?
Why do we use using namespace std in c++?
Can a Structure contain a Pointer to itself?
What is c++ namespace?
What is the best c++ ide?
How do you find out if a linked-list has an end? (I.e. The list is not a cycle)
List the types of polymorphism in c++?
What is the difference between containment and delegation?
What is prototype for that c string function?
What is microsoft c++ redistributable 2013?
What is c++ & why it is used?
What are dynamic type checking?
Explain explicit container.
Explain the difference between realloc() and free() in c++?
Should the member functions which are made public in the base class be hidden?