Bharathiar University
Programming in C++ LabC++ Program 5Write 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 1Enter the first string: hello
Enter the second string: worldStrings are not equalConcatenated String: hello world
OUTPUT 2 Enter the first string: hello
Enter the second string: helloGiven Strings are equalConcatenated 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
#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
void display() {
cout<<"Concatenated String:
"<<s<<endl;
}
};
int main()
{
clrscr();
STRING ob1,ob2;
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