28/03/2023

Overloading Ostream Operator Hackerrank Solution in C++

Overloading Ostream Operator Hackerrank Solution in C++. The task is to overload the << operator for Person class in such a way that for p being an instance of class Person the result of:

std::cout << p << " " << <some_string_value> << std::endl;

produces the following output:

first_name=<first_name>,last_name=<last_name> <some_string_value>

where:

<first_name> is the value of p's first_name_
<last_name> is the value of p's last_name_
<some_string_value> is an arbitrary std::string value

Overloading Ostream Operator Hackerrank Solution in C++

Input Format

The input is read by the provided locked code template. In the only line of the input, there are 3 space-separated strings first_name, last_name, and event. The values of first_name and last_name will be used to create an object p of type Person. The value of the event will be used by the provided code to produce the output.

Constraints

  • Each word in the input contains only English letters and is no longer than 15 characters

Output Format

The output should be produced by the provided locked code template. This code will use the implementation of Person public methods and the overloaded << operator to produce the output. Specifically, the output will be produced by the following code:

cout << p << " " << event << endl;

Sample Input 0

john doe registered

Sample Output 0

first_name=john,last_name=doe registered

Submit your solution here: Click here

Overloading Ostream Operator Hackerrank Solution in C++


#include <iostream>
using namespace std;

class Person
{
	public:
		Person(const string &first_name, const string &last_name): first_name_(first_name), last_name_(last_name) {}

	const string &get_first_name() const
	{
		return first_name_;
	}

	const string &get_last_name() const
	{
		return last_name_;
	}

	private:
		string first_name_;
	string last_name_;
};

// Enter your code here.
ostream &operator<<(ostream &out, const Person &pers)
{
	out << "first_name=" << pers.get_first_name() << "," << "last_name=" << pers.get_last_name();
	return out;
}

int main()
{
	string first_name, last_name, event;
	cin >> first_name >> last_name >> event;
	auto p = Person(first_name, last_name);
	cout << p << " " << event << endl;
	return 0;
}

The Output of Overloading Ostream Operator Hackerrank Solution


The Output of Overloading Ostream Operator Hackerrank Solution

Similar to Overloading Ostream Operator


Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: