function call into main

I am getting error in my code please solve this...!

#include<iostream>
using namespace std;
class Myclass
{
public:
Myclass()
{
char First_Name[20],Last_Name[20];
}

Myclass(char First_Name[20], char Last_Name[20])
{
First_Name="Mirza";
Last_Name="Salman";
}
void concatinate_Fun(char First_Name[20], char Last_Name[20])
{
cout<<First_Name<<" "<<Last_Name;
}
};
int main()
{
Myclass A1;
A1.concatinate_Fun(char First_Name[20], char Last_Name[20])

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
#include <string>

using namespace std;
class Myclass
{
	string First_Name, Last_Name;
public:
	Myclass()
	{
		First_Name = "Mirza";
		Last_Name = "Salman";
	}

	
	void concatinate_Fun()
	{
		cout << First_Name << " " << Last_Name;
	}
};
int main()
{
	Myclass A1;
	A1.concatinate_Fun();

	cin.get();
return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
using namespace std;
class Myclass
{
public:
Myclass()
{
    char First_Name[20],Last_Name[20];
}

Myclass(char First_Name[20], char Last_Name[20])
{
    First_Name="Mirza";
    Last_Name="Salman";
}
void concatinate_Fun(char First_Name[20], char Last_Name[20])
{
     cout<<First_Name<<" "<<Last_Name;
}
};
int main()
{
    Myclass A1;
    char firstname[] = "Donatus";
    char lastname[] = "Liber";
    A1.concatinate_Fun(firstname, lastname);
    
    return 0;
}


For more information, see http://www.cplusplus.com/doc/tutorial/functions/
@blongho and @mirzasalman

First_Name="Mirza" should create a very nice compiler warning for you. See how Manga used the std::string class instead of char arrays? Use the C++ facilities you have given you.

If you are forbidden from using std::string (as some professors will do, IDK why), then make sure to #include <cstring> and std::strcpy( First_Name, "Mirza" );.

Finally, concatenation is combining two string values by appending them, creating a new string value. Printing them to standard out is not concatenation. It is output.

It would help if you were more specific about what you are trying to accomplish. Names like "MyClass" are meaningless. The only clue is the member variables "First_Name" and "Last_Name", which suggests that we are dealing with a person, which leads to an incongruity: you cannot concatenate people. Perhaps you mean, "full name"?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>

struct Person
{
  std::string first_name;
  std::string last_name;

  Person( const std::string& first, const std::string& last )
  {
    first_name = first;
    last_name  = last;
  }

  std::string full_name() const
  {
    return first_name + " " + last_name;
  }
};

int main()
{
  Person p( "Mirza", "Salman" );
  std::cout << p.full_name() << "\n";
}

Hope this helps.
Topic archived. No new replies allowed.