Monday, August 15, 2011

Create a program with constructor of different type and use some overloaded operator

We have to create a program with constructor of different type and use some overloaded operator for comparing & showing subsets of strings.
Class Diagram
Class str
Private:
char *input
int len
Public :
str()
str(str &)
str operator +(str)
int operator =(str)
int operator <(str)
int operator &\^(str)
void getdata()
void display()
Solution:
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>

class str{
private:
       char *input;
       int len;
public:
str(){
       input = "";
       }
str(str &){
       len=strlen(input);
       input=new char[len+1];
       strcpy(input,s.input);
       delete input;
       }
str operator +(str){
       str temp;
       strcpy(temp.input,input);
       strcat(temp.input,s.input);
       return temp;
       }
int operator =(str){
       if (!strcmp(input,s.input))
              return 1;
       else
              return 0;
       }
int operator <(str){
       int len1,len2;
       len1=strlen(input);
       len2=strlen(s.input);
       if (len1<len2)
              return 1;
       else
              return 0;
       }
int operator ^(str){
       char *ptr;
       ptr=strstr(input,s.input);
       if (ptr=="")
              return 0;
       else if (!strcmp(s.input,ptr))
              return 1;
       else if (!strcmp(input,ptr))
              return 2;
       }
void getdata(){
       cout<<"Enter your string: ";
       gets(input);
       }
void display(){
       cout<<input<<endl;
       }
};

void main(){
       str a,b,c;
       a.getdata();
       b.getdata();
       c=a+b;
       c.display();

       if (a=b)
              cout<<"Both strings are equal.\n";
       else if (a<b)
              cout<<"String 1 is less than string 2.\n";
       if (!(a^b))
              cout<<"String 2 is not a substring of string 1.\n";
       else if ((a^b)==1)
              cout<<"String 2 is a substring of string 1.\n";
       else if ((a^b)==2)
              cout<<"String 2 is a real subset of string 1.\n";
       getch();
       }

I made a program that compares, shows substrings & I think I solved the program successfully.

No comments:

Post a Comment

Vision