Saturday, 23 June 2012

write a c++ program to consider the following class

/*write a c++ program to consider  the following class
class string
{
char *ptr;
int length;
public://member function declaration
}
overload the unary operator ! for the string class as a member
function so that it returns true if the string pointed by ptr is empty.
false otherwise write a main() to test this function. write other
necessary function including constructor(s) and destructor(s).
*/
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
    char *ptr;
    int len;
    public:
        string(char *s)
        {
            len=strlen(s);
            ptr=new char[len];
            strcpy(ptr,s);
        }
        ~string()
        {
            delete ptr;
        }
        void operator !()
        {

        if(len==0)
                   cout<<"\n\nString is empty";
                   else
                   cout<<"\n\nString is;"<<ptr;
        }

};
int main()
{
    clrscr();
    char str[20];
    cout<<"\n\nEnter a string : ";
    cin>>str;
    string s1(str),s2("");
    !s1;
    !s2;
    getch();
    return(0);
}
/*


Enter a string : peer


String is;peer

String is empty

*/

No comments: