Saturday, March 11, 2023

BCA and B.Sc Programs - Programming in C++ Lab - Bharathiar University - Practical Program 5 - Write a C++ Program to create a class STRING. Write a member function to initialize, get and display strings. Overload the operators ++ and == to concatenate two Strings and to compare two strings respectively.

 


 Bharathiar University

Programming in C++ Lab
C++ Program 5
Write a C++ Program to create a class STRING. Write a member function to initialize, get and display strings. Overload the operators ++ and == to concatenate two Strings and to compare two strings respectively.

In this page

  • C++ Source Code for Visual Studio Code
  • C++ Source Code for MS Dos Turbo C++

FOR VISUAL STUDIO CODE

Source code
#include <iostream>    
#include<string.h>
#include<stdio.h>
using namespace std;    
class STRING    
{    
        public:    
      string s;    
    
       void initialize()
       {
        s="";
    
       }    
       void get()
       {
          cin >> s;  
                   
       }
       void operator+(STRING ob)
       {     
              s.append(" "+ob.s);
              display(); 
             
       }  
       void operator==(STRING ob)
       {     
         if (s.compare(ob.s)==0)
               cout<<"Given Strings are equal"<<endl;
         else
           
                 cout<<"Strings are not equal"<<endl;
                    
       }    
       
      void display() {     
           cout<<"Concatenated String: "<<s<<endl;     
       }    
};    
int main()    
{    
    STRING ob1,ob2;
    ob1.initialize();
    ob2.initialize();
    cout << " Enter the first string: "; 
    ob1.get();
    cout << "\n Enter the second string: ";  
    ob2.get();
    ob1==ob2;
    ob1+ob2;
    
   
           
    return 0;    
}    

OUTPUT 1
Enter the first string: hello

 Enter the second string: world
Strings are not equal
Concatenated String: hello world

OUTPUT 2
 Enter the first string: hello

 Enter the second string: hello
Given Strings are equal
Concatenated String: hello hello

SOURCE CODE FOR TUBO C++(MS DOS)

#include <iostream.h>

#include <string.h>

#include<stdlib.h>

#include<conio.h>

class STRING

{

                public:

                 char s[100];

 

       void initialize()

       {

        }

       void get()

       {              

    cin >> s;

        }

       void operator+(STRING ob)

       {

 

                      strcat(s,ob.s);

                      display();

        }

       void operator==(STRING ob)

       {

                 if (strcmp(s,ob.s)==0)

                       cout<<"Given Strings are equal"<<endl;

                 else

                      cout<<"Strings are not equal"<<endl;

        }

 

      void display() {

                   cout<<"Concatenated String: "<<s<<endl;

       }

};

int main()

{

clrscr();

    STRING ob1,ob2;

     ob1.initialize();

    ob2.initialize();

    cout << " Enter the first string: ";

    ob1.get();

    cout << "\n Enter the second string: ";

    ob2.get();

    ob1==ob2;

    ob1+ob2;

    getch();

    return 0;

}   

Sample output

 


No comments:

Post a Comment

Program 12 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 12 Implement a telephone directory using files

  BCA Object Oriented Programming using C++ Practical Madras University  Program 12  Implement a telephone directory using files SOURCE CODE...