Last active
June 19, 2020 16:57
-
-
Save DevT0ny/3b2ea4837c813004a7676ce460c61801 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <span style="background-color:black !important;"> | |
| Helllow | |
| </span> | |
| #include<iostream> | |
| #include<string.h> | |
| using namespace std; | |
| /** AIM : Write a class for concatenation and printing strings | |
| * variable to store string data | |
| * create Print() to display stored String | |
| * create OP + overloaded function to concatenation of 2 strings | |
| * | |
| */ | |
| class String{ | |
| char str[30]; | |
| public: | |
| // Default constructor | |
| String(){ | |
| strcpy(str, "\0"); | |
| } | |
| // Parameterized constructor for storing string / array of char | |
| String(char data[30]){ | |
| strcpy(str, data); | |
| } | |
| // Destructor *optional | |
| ~String(){ | |
| cout<< "Obj is destroyed"<<endl; | |
| } | |
| // Declaration of concatenation ( + OP overloaded) friend func | |
| friend String operator+(String, String); | |
| // Display func | |
| void Print(){ | |
| cout << "The string is : " << str <<endl; | |
| } | |
| // Copy constructor for taking String type data | |
| // String (String ©){ | |
| // strcpy(str, copy.str); | |
| // } | |
| }; | |
| // Definition of concatenation ( + OP overloaded) friend func | |
| String operator+(String str1 ,String str2){ | |
| String temp; | |
| strcpy(temp.str, str1.str); | |
| strcat(temp.str , str2.str) ; // temp.str = str1.str + str2.str | |
| return temp; | |
| } | |
| int main(){ | |
| String myStr1("Hello "); | |
| cout << "myString1 obj ==> "; | |
| myStr1.Print(); | |
| String myStr2("world"); | |
| cout << "myString2 obj ==> "; | |
| myStr2.Print(); | |
| String result = myStr1+myStr2; | |
| cout << "Concatenated String ==> "; | |
| result.Print(); | |
| //------copy constructor is commented above class String------- | |
| // String Copy(myStr1); | |
| // Copy.Print(); | |
| } | |
| // output: | |
| // The string is :Hello | |
| // The string is :world | |
| // The string is :Hello world |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment