C++ Program to concatenate two strings using friend function
In this program, You will learn how to concatenate two strings using friend function in C++.
strcat(t.str1, t.str2);
Example: How to concatenate two strings using friend function in C++.
#include<iostream>
#include<cstring>
using namespace std;
class Test {
private:
char str1[100], str2[100];
public:
void input() {
cout << "Enter first string:";
cin.getline(str1, 100);
cout << "Enter second string:";
cin.getline(str2, 100);
}
friend void add(Test t);
};
void add(Test t) {
strcat(t.str1, t.str2);
cout << "After Concatenated string:" << t.str1;
}
int main() {
Test t;
t.input();
add(t);
return 0;
}
Output:
Enter first string:Xiith
Enter second string:.com
After Concatenated string:Xiith.com