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


Please Help Members By Posting Answers For Below Questions

What is the use of this pointer in c++?

570


Which compiler does turbo c++ use?

604


When can I use a forward declaration?

624


How do I run c++?

576


What is #include iostream?

737






What is the role of static keyword for a class member variable?

629


what is COPY CONSTRUCTOR and what is it used for?

621


What kind of problems can be solved by a namespace?

591


What does the ios::ate argument do?

663


When does the c++ compiler create temporary variables?

570


What ANSI C++ function clears the screen a) clrscr() b) clear() c) Its not defined by the ANSI C++ standard

588


What are the differences between malloc() and calloc()?

613


What is a responder chain?

572


Explain how to initialize a const data member.

596


Which software is best for c++ programming?

576