Overloading Ostream Operator in C++ – Hacker Rank Solution | HackerRank Programming Solutions | HackerRank C++ Solutions

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank of Programming Language C++ . At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight you profile to the recruiters.

In this post, you will find the solution for Overloading Ostream Operator in C++-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

Introduction To C++

C++ is a general-purpose programming language that was developed as an enhancement of the C language to include object-oriented paradigm. It is an imperative and a compiled language. 

C++ is a middle-level language rendering it the advantage of programming low-level (drivers, kernels) and even higher-level applications (games, GUI, desktop apps etc.). The basic syntax and code structure of both C and C++ are the same. 

C++ programming language was developed in 1980 by Bjarne Stroustrup at bell laboratories of AT&T (American Telephone & Telegraph), located in U.S.A. Bjarne Stroustrup is known as the founder of C++ language.

Overloading Ostream Operator in C++  - Hackerrank Solution

Problem

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

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, event. The values of first_name and last_name will be used to create an object p of type Person. The value of 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 wiil be produced by the following code:

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

Sample Input :

john doe registered

Sample Output :

first_name=john,last_name=doe registered

Problem

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

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, event. The values of first_name and last_name will be used to create an object p of type Person. The value of 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 wiil be produced by the following code:

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

Sample Input :

john doe registered

Sample Output :

first_name=john,last_name=doe registered

Overloading Ostream Operator in C++ – Hacker Rank Solution
#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.
/* Overloading Ostream Operator in C++ - Hacker Rank Solution START */
ostream& operator<<(ostream& Output, Person& p) 
{
  Output << "first_name=" << p.get_first_name() << ","
         << "last_name=" << p.get_last_name();
  return Output;
}
/* Overloading Ostream Operator in C++ - Hacker Rank Solution END */

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;
}

Leave a Comment

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Refresh