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
Are php strings immutable?
What kind of problems can be solved by a namespace?
How does atoi function work?
Is it possible for a member function to use delete this?
What is the header file for setw?
What is null and void pointer?
Does improper inheritance have a potential to wreck a project?
what you know about c++?
Explain Memory Allocation in C/C++ ?
Differentiate between the manipulator and setf( ) function?
Write a function to find the nth item from the end of a linked list in a single pass.
What is the default access level?
What are libraries in c++?
Is there any function that can skip certain number of characters present in the input stream?
Is c++ a float?