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
If horse and bird inherit virtual public from animal, do their constructors initialize the animal constructor? If pegasus inherits from both horse and bird, how does it initialize animal’s constructor?
Explain container class.
How do you find out if a linked-list has an end?
Define namespace in c++?
What are the uses of c++ in the real world?
Explain virtual class?
What is nested class in c++?
Can I learn c++ as my first language?
What is the difference between a declaration and a definition?
How to give an alternate name to a namespace?
Can c++ be faster than c?
What is the real purpose of class – to export data?
What is an iterator?
Explain the properties and principles of oop.
Is c++ a software?