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 data structure is fastest, on average, for retrieving data: a) Binary Tree b) Hash Table c) Stack

601


Write some differences between an external iterator and an internal iterator?

600


Explain the differences between list x; & list x();.

616


write a program that withdrawals,deposits,balance check,shows mini statement. (using functions,pointers and arrays)

1817


In which header file does one find isalpha() a) conio.h b) stdio.h c) ctype.h

693






Which format specifier is used for printing a pointer value?

590


What is c++ coding?

669


What is c++ flowchart?

692


If there are two catch statements, one for base and one for derived, which should come first?

590


Does a derived class inherit or doesn't inherit?

630


What is a constructor and how is it called?

608


How does class accomplish data hiding in c++?

670


How do you differentiate between overloading the prefix and postfix increments?

603


What is the use of bit fields in structure declaration?

553


How to implement is-a and has-a class relationships?

597