Master C++ with Real-time Projects and Kickstart Your Career Start Now!!
Program 1
// Operator overloading (+ for string concat)
#include<iostream>
#include<string.h>
#define clrscr() system("cls")
using namespace std;
class TestString
{
char str[500];
public:
TestString(char str[])
{
strcpy(this->str,str);
}
TestString()
{}
TestString operator+(TestString &k);
void display();
};
void TestString::display()
{
cout<<str;
}
TestString TestString ::operator+(TestString &k)
{
TestString temp;
strcpy(temp.str,str); // temp.str="Hello"
strcat(temp.str,k.str);// temp.str="Hello DataFlair"
return temp;
}
int main()
{
clrscr();
TestString T1("This is Data Flair");
TestString T2(" C++ Free Course");
TestString T3;
T3=T1+T2; // T3=T1.operator+(T2);
cout<<"\n";
T1.display();
cout<<"\n";
T2.display();
cout<<"\n";
T3.display();
return 0;
}