logo

Come concatenare due stringhe in C++?

Questa sezione discuterà la concatenazione di due o più stringhe nel linguaggio di programmazione C++. Per concatenazione della stringa si intende il gruppo di caratteri che combina altre due stringhe per restituire un'unica stringa concatenata. Durante la concatenazione delle stringhe, la seconda stringa viene aggiunta alla fine della prima stringa per creare un'unica stringa.

ottenere la connessione

Ad esempio, abbiamo due stringhe, ' Giava ' E ' Punto T ', e vogliamo concatenare per creare una singola stringa come Java + Tpoint = JavaTpoint.

Come concatenare due stringhe in C++

Discutiamo i diversi modi per concatenare la stringa data nel linguaggio di programmazione C++.

  1. Concatena due stringhe usando il ciclo for
  2. Concatena due stringhe utilizzando il ciclo while
  3. Concatena due stringhe utilizzando l'operatore +
  4. Concatena due stringhe utilizzando la funzione strcat()
  5. Concatena due stringhe utilizzando la funzione append()
  6. Concatena due stringhe usando l'ereditarietà
  7. Concatena due stringhe utilizzando la funzione friend e la funzione strcat()

Programma per concatenare due stringhe utilizzando il ciclo for

Consideriamo un esempio per combinare due stringhe utilizzando il ciclo for nella programmazione C++.

Programma.cpp

 #include using namespace std; int main () { string str1, str2, result; // declare string variables int i; cout &lt;&gt; str1; // take string cout &lt;&gt; str2; // take second string // use for loop to enter the characters of the str1 into result string for ( i = 0; i <str1.size(); i++) { result="result" + str1[i]; add character of the str1 into } use for loop to enter characters str2 string ( i="0;" < str2.size(); str2[i]; cout << ' concatenation and is <<result; return 0; pre> <p> <strong>Output</strong> </p> <pre> Enter the first string: Java Enter the second string: Tpoint The Concatenation of the string Java and Tpoint is JavaTpoint </pre> <h3>Program to concatenate two strings using while loop</h3> <p>Let&apos;s consider an example to combine two strings using a while loop in C++ programming.</p> <p> <strong>Program2.cpp</strong> </p> <pre> #include using namespace std; int main () { // declare and initialize the string char str1[100] = &apos; We Love&apos;; char str2[100] = &apos; C++ Programming Language&apos;; int i, j; // declare variable cout &lt;&lt; &apos; The first string is: &apos; &lt;&lt; str1 &lt;&lt; endl; cout &lt;&lt; &apos; The second string is: &apos;&lt;&lt; str2 &lt;<endl; for (i="0;" str1[i] !="" ; i++); j="0;" initialize with 0; use while loop that insert the str2 characters in str1 (str2[j] ) check is not equal to null { assign character of i++; j++; } cout << ' concatenated string is: str1; return < pre> <p> <strong>Output</strong> </p> <pre> The first string is: We Love The second string is: C++ Programming Language The concatenated string is: We Love C++ Programming Language </pre> <h3>Program to concatenate two strings using the + operator in C++</h3> <p> <strong>+ Operator:</strong> It is an arithmetic &apos;+&apos;operator that simply adds two strings to return a new concatenated string.</p> <p>Let&apos;s consider an example to combine two strings using the + operator in C++ programming.</p> <p> <strong>Program3.cpp</strong> </p> <pre> #include using namespace std; int main () { string str1, str2; // declare string variables cout &lt;&gt; str1; cout &lt;&gt; str2; // use &apos;+&apos; operator to concatenate the str1 and str2 string result = str1 + str2; cout &lt;&lt; &apos; The concatenated string &apos; &lt;&lt; str1 &lt;&lt; &apos; and &apos; &lt;&lt; str2 &lt;<' is: ' << result; return 0; } < pre> <p> <strong>Output</strong> </p> <pre> Enter the first string: Java Enter the second string: Tpoint The concatenated string Java and Tpoint is: JavaTpoint </pre> <h3>Program to concatenate two strings using the strcat() method</h3> <p> <strong>strcat() function:</strong> The strcat is an inbuilt function of the string class, which adds two character strings to return a concatenated string.</p> <p> <strong>Syntax</strong> </p> <pre> strcat ( char *arr1, char *arr2) </pre> <p>There are two character arrays in the above syntax, arr1 and arr2, which passed inside the strcat() function to return a concatenated string.</p> <p>Let&apos;s consider an example to combine two strings using strcat() function in the C++ programming.</p> <p> <strong>Program4.cpp</strong> </p> <pre> #include #include using namespace std; int main() { // declare and initialize the string char str1[] = &apos; We love&apos;; char str2[] = &apos; C++ Programming&apos;; cout &lt;&lt; &apos; String 1: &apos; &lt;<str1 <<endl; cout << ' string 2: <<str2 use the strcat() function to concatenate strcat(str1, str2); concatenated is: <<str1; return 0; } < pre> <p> <strong>Output</strong> </p> <pre> String 1: We love String 2: C++ Programming The concatenated string is: We love C++ Programming </pre> <h3>Program to concatenate two strings using the append function</h3> <p> <strong>append() function:</strong> An <strong>append()</strong> function is a predefined library function used to insert or add a second string at the end of the first string to return a single string.</p> <p> <strong>Syntax</strong> </p> <pre> str1.append(str2); </pre> <p>In the above syntax, the str2 is a second string to pass in the append() function that inserts the str2 string at the end of the str1 string.</p> <p>Let&apos;s consider an example to combine two strings using append() function in the C++ programming.</p> <p> <strong>Program5.cpp</strong> </p> <pre> #include using namespace std; int main () { string str1, str2, result; // declare string variables cout &lt;&gt; str1; // take string cout &lt;&gt; str2; // take second string // use append() function to insert element at the end of the str1 str1.append(str2); cout &lt;&lt; &apos; 
 The concatenation of the string is: &apos;&lt;&lt; str1; return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Enter the first string: Hello Enter the second string: Friends! The concatenation of the string is: HelloFriends! </pre> <h3>Program to concatenate two string using the inheritance of the class</h3> <p>Let&apos;s consider an example to combine two strings using inheritance in the C++ programming.</p> <p> <strong>Program6.cpp</strong> </p> <pre> #include #include using namespace std; // create base class class base { protected: virtual string concatenate(string &amp;str1, string &amp;str2) = 0; }; // create derive class to acquire features of base class class derive: protected base { public: string concatenate (string &amp;str1, string &amp;str2) { string temp; temp = str1 + str2; return temp; } }; int main() { // declare variable string str1, str2; cout &lt;&gt;str1; cout &lt;&gt;str2; // create string object derive obj; // print string cout &lt;<' 
 the concatenated string is: ' << obj.concatenate (str1, str2); return 0; } < pre> <p> <strong>Output</strong> </p> <pre> Enter first string: C++ Enter second string: Programming The concatenated string is: C++Programming </pre> <h3>Program to concatenate two strings using the friend function and strcat() function</h3> <p>Let&apos;s consider an example to combine two strings using friend function and strcat() function in the C++ programming.</p> <p> <strong>Program7.cpp</strong> </p> <pre> #include #include using namespace std; class Base { private: char st[100], st2[100]; public: void inp() { cout &lt;<' 100 enter the first string: '; cin.getline (st, 100); take a line of string with limit cout <<' second (st2, friend void myfun(base b); }; myfun (base b) { strcat (b.st, b.st2); pass parameter to concatenate concatenated ' < <b.st; } int main() base b; create b as an object b.inp(); call inp() function myfun(b); myfun() print return 0; pre> <p> <strong>Output</strong> </p> <pre> Enter the first string: javatpoint Enter the second string: .com The concatenated string: javatpoint.com </pre> <hr></'></pre></'></pre></str1></pre></'></pre></endl;></pre></str1.size();>

Programma per concatenare due stringhe utilizzando il ciclo while

Consideriamo un esempio per combinare due stringhe utilizzando un ciclo while nella programmazione C++.

Programma2.cpp

 #include using namespace std; int main () { // declare and initialize the string char str1[100] = &apos; We Love&apos;; char str2[100] = &apos; C++ Programming Language&apos;; int i, j; // declare variable cout &lt;&lt; &apos; The first string is: &apos; &lt;&lt; str1 &lt;&lt; endl; cout &lt;&lt; &apos; The second string is: &apos;&lt;&lt; str2 &lt;<endl; for (i="0;" str1[i] !="" ; i++); j="0;" initialize with 0; use while loop that insert the str2 characters in str1 (str2[j] ) check is not equal to null { assign character of i++; j++; } cout << \' concatenated string is: str1; return < pre> <p> <strong>Output</strong> </p> <pre> The first string is: We Love The second string is: C++ Programming Language The concatenated string is: We Love C++ Programming Language </pre> <h3>Program to concatenate two strings using the + operator in C++</h3> <p> <strong>+ Operator:</strong> It is an arithmetic &apos;+&apos;operator that simply adds two strings to return a new concatenated string.</p> <p>Let&apos;s consider an example to combine two strings using the + operator in C++ programming.</p> <p> <strong>Program3.cpp</strong> </p> <pre> #include using namespace std; int main () { string str1, str2; // declare string variables cout &lt;&gt; str1; cout &lt;&gt; str2; // use &apos;+&apos; operator to concatenate the str1 and str2 string result = str1 + str2; cout &lt;&lt; &apos; The concatenated string &apos; &lt;&lt; str1 &lt;&lt; &apos; and &apos; &lt;&lt; str2 &lt;<\' is: \' << result; return 0; } < pre> <p> <strong>Output</strong> </p> <pre> Enter the first string: Java Enter the second string: Tpoint The concatenated string Java and Tpoint is: JavaTpoint </pre> <h3>Program to concatenate two strings using the strcat() method</h3> <p> <strong>strcat() function:</strong> The strcat is an inbuilt function of the string class, which adds two character strings to return a concatenated string.</p> <p> <strong>Syntax</strong> </p> <pre> strcat ( char *arr1, char *arr2) </pre> <p>There are two character arrays in the above syntax, arr1 and arr2, which passed inside the strcat() function to return a concatenated string.</p> <p>Let&apos;s consider an example to combine two strings using strcat() function in the C++ programming.</p> <p> <strong>Program4.cpp</strong> </p> <pre> #include #include using namespace std; int main() { // declare and initialize the string char str1[] = &apos; We love&apos;; char str2[] = &apos; C++ Programming&apos;; cout &lt;&lt; &apos; String 1: &apos; &lt;<str1 <<endl; cout << \' string 2: <<str2 use the strcat() function to concatenate strcat(str1, str2); concatenated is: <<str1; return 0; } < pre> <p> <strong>Output</strong> </p> <pre> String 1: We love String 2: C++ Programming The concatenated string is: We love C++ Programming </pre> <h3>Program to concatenate two strings using the append function</h3> <p> <strong>append() function:</strong> An <strong>append()</strong> function is a predefined library function used to insert or add a second string at the end of the first string to return a single string.</p> <p> <strong>Syntax</strong> </p> <pre> str1.append(str2); </pre> <p>In the above syntax, the str2 is a second string to pass in the append() function that inserts the str2 string at the end of the str1 string.</p> <p>Let&apos;s consider an example to combine two strings using append() function in the C++ programming.</p> <p> <strong>Program5.cpp</strong> </p> <pre> #include using namespace std; int main () { string str1, str2, result; // declare string variables cout &lt;&gt; str1; // take string cout &lt;&gt; str2; // take second string // use append() function to insert element at the end of the str1 str1.append(str2); cout &lt;&lt; &apos; 
 The concatenation of the string is: &apos;&lt;&lt; str1; return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Enter the first string: Hello Enter the second string: Friends! The concatenation of the string is: HelloFriends! </pre> <h3>Program to concatenate two string using the inheritance of the class</h3> <p>Let&apos;s consider an example to combine two strings using inheritance in the C++ programming.</p> <p> <strong>Program6.cpp</strong> </p> <pre> #include #include using namespace std; // create base class class base { protected: virtual string concatenate(string &amp;str1, string &amp;str2) = 0; }; // create derive class to acquire features of base class class derive: protected base { public: string concatenate (string &amp;str1, string &amp;str2) { string temp; temp = str1 + str2; return temp; } }; int main() { // declare variable string str1, str2; cout &lt;&gt;str1; cout &lt;&gt;str2; // create string object derive obj; // print string cout &lt;<\' 
 the concatenated string is: \' << obj.concatenate (str1, str2); return 0; } < pre> <p> <strong>Output</strong> </p> <pre> Enter first string: C++ Enter second string: Programming The concatenated string is: C++Programming </pre> <h3>Program to concatenate two strings using the friend function and strcat() function</h3> <p>Let&apos;s consider an example to combine two strings using friend function and strcat() function in the C++ programming.</p> <p> <strong>Program7.cpp</strong> </p> <pre> #include #include using namespace std; class Base { private: char st[100], st2[100]; public: void inp() { cout &lt;<\' 100 enter the first string: \'; cin.getline (st, 100); take a line of string with limit cout <<\' second (st2, friend void myfun(base b); }; myfun (base b) { strcat (b.st, b.st2); pass parameter to concatenate concatenated \' < <b.st; } int main() base b; create b as an object b.inp(); call inp() function myfun(b); myfun() print return 0; pre> <p> <strong>Output</strong> </p> <pre> Enter the first string: javatpoint Enter the second string: .com The concatenated string: javatpoint.com </pre> <hr></\'></pre></\'></pre></str1></pre></\'></pre></endl;>

Programma per concatenare due stringhe utilizzando l'operatore + in C++

+ Operatore: È un operatore aritmetico '+' che aggiunge semplicemente due stringhe per restituire una nuova stringa concatenata.

Consideriamo un esempio per combinare due stringhe utilizzando l'operatore + nella programmazione C++.

Programma3.cpp

stringa formattata c
 #include using namespace std; int main () { string str1, str2; // declare string variables cout &lt;&gt; str1; cout &lt;&gt; str2; // use &apos;+&apos; operator to concatenate the str1 and str2 string result = str1 + str2; cout &lt;&lt; &apos; The concatenated string &apos; &lt;&lt; str1 &lt;&lt; &apos; and &apos; &lt;&lt; str2 &lt;<\' is: \' << result; return 0; } < pre> <p> <strong>Output</strong> </p> <pre> Enter the first string: Java Enter the second string: Tpoint The concatenated string Java and Tpoint is: JavaTpoint </pre> <h3>Program to concatenate two strings using the strcat() method</h3> <p> <strong>strcat() function:</strong> The strcat is an inbuilt function of the string class, which adds two character strings to return a concatenated string.</p> <p> <strong>Syntax</strong> </p> <pre> strcat ( char *arr1, char *arr2) </pre> <p>There are two character arrays in the above syntax, arr1 and arr2, which passed inside the strcat() function to return a concatenated string.</p> <p>Let&apos;s consider an example to combine two strings using strcat() function in the C++ programming.</p> <p> <strong>Program4.cpp</strong> </p> <pre> #include #include using namespace std; int main() { // declare and initialize the string char str1[] = &apos; We love&apos;; char str2[] = &apos; C++ Programming&apos;; cout &lt;&lt; &apos; String 1: &apos; &lt;<str1 <<endl; cout << \' string 2: <<str2 use the strcat() function to concatenate strcat(str1, str2); concatenated is: <<str1; return 0; } < pre> <p> <strong>Output</strong> </p> <pre> String 1: We love String 2: C++ Programming The concatenated string is: We love C++ Programming </pre> <h3>Program to concatenate two strings using the append function</h3> <p> <strong>append() function:</strong> An <strong>append()</strong> function is a predefined library function used to insert or add a second string at the end of the first string to return a single string.</p> <p> <strong>Syntax</strong> </p> <pre> str1.append(str2); </pre> <p>In the above syntax, the str2 is a second string to pass in the append() function that inserts the str2 string at the end of the str1 string.</p> <p>Let&apos;s consider an example to combine two strings using append() function in the C++ programming.</p> <p> <strong>Program5.cpp</strong> </p> <pre> #include using namespace std; int main () { string str1, str2, result; // declare string variables cout &lt;&gt; str1; // take string cout &lt;&gt; str2; // take second string // use append() function to insert element at the end of the str1 str1.append(str2); cout &lt;&lt; &apos; 
 The concatenation of the string is: &apos;&lt;&lt; str1; return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Enter the first string: Hello Enter the second string: Friends! The concatenation of the string is: HelloFriends! </pre> <h3>Program to concatenate two string using the inheritance of the class</h3> <p>Let&apos;s consider an example to combine two strings using inheritance in the C++ programming.</p> <p> <strong>Program6.cpp</strong> </p> <pre> #include #include using namespace std; // create base class class base { protected: virtual string concatenate(string &amp;str1, string &amp;str2) = 0; }; // create derive class to acquire features of base class class derive: protected base { public: string concatenate (string &amp;str1, string &amp;str2) { string temp; temp = str1 + str2; return temp; } }; int main() { // declare variable string str1, str2; cout &lt;&gt;str1; cout &lt;&gt;str2; // create string object derive obj; // print string cout &lt;<\' 
 the concatenated string is: \' << obj.concatenate (str1, str2); return 0; } < pre> <p> <strong>Output</strong> </p> <pre> Enter first string: C++ Enter second string: Programming The concatenated string is: C++Programming </pre> <h3>Program to concatenate two strings using the friend function and strcat() function</h3> <p>Let&apos;s consider an example to combine two strings using friend function and strcat() function in the C++ programming.</p> <p> <strong>Program7.cpp</strong> </p> <pre> #include #include using namespace std; class Base { private: char st[100], st2[100]; public: void inp() { cout &lt;<\' 100 enter the first string: \'; cin.getline (st, 100); take a line of string with limit cout <<\' second (st2, friend void myfun(base b); }; myfun (base b) { strcat (b.st, b.st2); pass parameter to concatenate concatenated \' < <b.st; } int main() base b; create b as an object b.inp(); call inp() function myfun(b); myfun() print return 0; pre> <p> <strong>Output</strong> </p> <pre> Enter the first string: javatpoint Enter the second string: .com The concatenated string: javatpoint.com </pre> <hr></\'></pre></\'></pre></str1></pre></\'>

Programma per concatenare due stringhe utilizzando il metodo strcat()

funzione strcat(): Lo strcat è una funzione incorporata della classe string, che aggiunge due stringhe di caratteri per restituire una stringa concatenata.

Sintassi

 strcat ( char *arr1, char *arr2) 

Nella sintassi precedente sono presenti due array di caratteri, arr1 e arr2, passati all'interno della funzione strcat() per restituire una stringa concatenata.

Consideriamo un esempio per combinare due stringhe utilizzando la funzione strcat() nella programmazione C++.

Programma4.cpp

 #include #include using namespace std; int main() { // declare and initialize the string char str1[] = &apos; We love&apos;; char str2[] = &apos; C++ Programming&apos;; cout &lt;&lt; &apos; String 1: &apos; &lt;<str1 <<endl; cout << \' string 2: <<str2 use the strcat() function to concatenate strcat(str1, str2); concatenated is: <<str1; return 0; } < pre> <p> <strong>Output</strong> </p> <pre> String 1: We love String 2: C++ Programming The concatenated string is: We love C++ Programming </pre> <h3>Program to concatenate two strings using the append function</h3> <p> <strong>append() function:</strong> An <strong>append()</strong> function is a predefined library function used to insert or add a second string at the end of the first string to return a single string.</p> <p> <strong>Syntax</strong> </p> <pre> str1.append(str2); </pre> <p>In the above syntax, the str2 is a second string to pass in the append() function that inserts the str2 string at the end of the str1 string.</p> <p>Let&apos;s consider an example to combine two strings using append() function in the C++ programming.</p> <p> <strong>Program5.cpp</strong> </p> <pre> #include using namespace std; int main () { string str1, str2, result; // declare string variables cout &lt;&gt; str1; // take string cout &lt;&gt; str2; // take second string // use append() function to insert element at the end of the str1 str1.append(str2); cout &lt;&lt; &apos; 
 The concatenation of the string is: &apos;&lt;&lt; str1; return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Enter the first string: Hello Enter the second string: Friends! The concatenation of the string is: HelloFriends! </pre> <h3>Program to concatenate two string using the inheritance of the class</h3> <p>Let&apos;s consider an example to combine two strings using inheritance in the C++ programming.</p> <p> <strong>Program6.cpp</strong> </p> <pre> #include #include using namespace std; // create base class class base { protected: virtual string concatenate(string &amp;str1, string &amp;str2) = 0; }; // create derive class to acquire features of base class class derive: protected base { public: string concatenate (string &amp;str1, string &amp;str2) { string temp; temp = str1 + str2; return temp; } }; int main() { // declare variable string str1, str2; cout &lt;&gt;str1; cout &lt;&gt;str2; // create string object derive obj; // print string cout &lt;<\' 
 the concatenated string is: \' << obj.concatenate (str1, str2); return 0; } < pre> <p> <strong>Output</strong> </p> <pre> Enter first string: C++ Enter second string: Programming The concatenated string is: C++Programming </pre> <h3>Program to concatenate two strings using the friend function and strcat() function</h3> <p>Let&apos;s consider an example to combine two strings using friend function and strcat() function in the C++ programming.</p> <p> <strong>Program7.cpp</strong> </p> <pre> #include #include using namespace std; class Base { private: char st[100], st2[100]; public: void inp() { cout &lt;<\' 100 enter the first string: \'; cin.getline (st, 100); take a line of string with limit cout <<\' second (st2, friend void myfun(base b); }; myfun (base b) { strcat (b.st, b.st2); pass parameter to concatenate concatenated \' < <b.st; } int main() base b; create b as an object b.inp(); call inp() function myfun(b); myfun() print return 0; pre> <p> <strong>Output</strong> </p> <pre> Enter the first string: javatpoint Enter the second string: .com The concatenated string: javatpoint.com </pre> <hr></\'></pre></\'></pre></str1>

Programma per concatenare due stringhe utilizzando la funzione append

funzione append(): UN aggiungere() function è una funzione di libreria predefinita utilizzata per inserire o aggiungere una seconda stringa alla fine della prima stringa per restituire una singola stringa.

Sintassi

 str1.append(str2); 

Nella sintassi precedente, str2 è una seconda stringa da passare nella funzione append() che inserisce la stringa str2 alla fine della stringa str1.

apprendimento automatico e tipi

Consideriamo un esempio per combinare due stringhe utilizzando la funzione append() nella programmazione C++.

Programma5.cpp

 #include using namespace std; int main () { string str1, str2, result; // declare string variables cout &lt;&gt; str1; // take string cout &lt;&gt; str2; // take second string // use append() function to insert element at the end of the str1 str1.append(str2); cout &lt;&lt; &apos; 
 The concatenation of the string is: &apos;&lt;&lt; str1; return 0; } 

Produzione

 Enter the first string: Hello Enter the second string: Friends! The concatenation of the string is: HelloFriends! 

Programma per concatenare due stringhe utilizzando l'ereditarietà della classe

Consideriamo un esempio per combinare due stringhe utilizzando l'ereditarietà nella programmazione C++.

Programma6.cpp

 #include #include using namespace std; // create base class class base { protected: virtual string concatenate(string &amp;str1, string &amp;str2) = 0; }; // create derive class to acquire features of base class class derive: protected base { public: string concatenate (string &amp;str1, string &amp;str2) { string temp; temp = str1 + str2; return temp; } }; int main() { // declare variable string str1, str2; cout &lt;&gt;str1; cout &lt;&gt;str2; // create string object derive obj; // print string cout &lt;<\' 
 the concatenated string is: \' << obj.concatenate (str1, str2); return 0; } < pre> <p> <strong>Output</strong> </p> <pre> Enter first string: C++ Enter second string: Programming The concatenated string is: C++Programming </pre> <h3>Program to concatenate two strings using the friend function and strcat() function</h3> <p>Let&apos;s consider an example to combine two strings using friend function and strcat() function in the C++ programming.</p> <p> <strong>Program7.cpp</strong> </p> <pre> #include #include using namespace std; class Base { private: char st[100], st2[100]; public: void inp() { cout &lt;<\' 100 enter the first string: \'; cin.getline (st, 100); take a line of string with limit cout <<\' second (st2, friend void myfun(base b); }; myfun (base b) { strcat (b.st, b.st2); pass parameter to concatenate concatenated \' < <b.st; } int main() base b; create b as an object b.inp(); call inp() function myfun(b); myfun() print return 0; pre> <p> <strong>Output</strong> </p> <pre> Enter the first string: javatpoint Enter the second string: .com The concatenated string: javatpoint.com </pre> <hr></\'></pre></\'>

Programma per concatenare due stringhe utilizzando la funzione friend e la funzione strcat()

Consideriamo un esempio per combinare due stringhe utilizzando la funzione friend e la funzione strcat() nella programmazione C++.

Programma7.cpp

 #include #include using namespace std; class Base { private: char st[100], st2[100]; public: void inp() { cout &lt;<\\' 100 enter the first string: \\'; cin.getline (st, 100); take a line of string with limit cout <<\\' second (st2, friend void myfun(base b); }; myfun (base b) { strcat (b.st, b.st2); pass parameter to concatenate concatenated \\' < <b.st; } int main() base b; create b as an object b.inp(); call inp() function myfun(b); myfun() print return 0; pre> <p> <strong>Output</strong> </p> <pre> Enter the first string: javatpoint Enter the second string: .com The concatenated string: javatpoint.com </pre> <hr></\\'>