In this tutorial, we’ll solve the class Hackerrank solution in c++ with the explanation. If you’re wondering how to solve the class Hackerrank solution in c++, then you’re at the right place. Here you’ll get all Hackerrank solutions with an explanation. But, before getting started we’ll first let you know about Hackerrank.
Table of Contents
Hackerrank is a tech company that mainly focuses on competitive programming challenges for students, companies, and consumers. Hackerrank was founded by two founders Vivek Ravishankar and Harishankar Karunanidhi in the year 2012. Its headquarters are situated in California, United States.
With the help of this platform students as well as professionals prepare themselves for the placements by practicing the coding problems. The questions that hacker rank contains are asked in the interview rounds of many companies.
So let us code the Class Hackerrank question in c++ with an Explanation.
Classes in C++ are user-defined types declared with keyword class that has data and functions. Although classes and structures have the same type of functionality, there are some basic differences. The data members of a class are private by default and the members of a structure are public by default. Along with storing multiple data in a common block, it also assigns some functions (known as methods) to manipulate/access them. It serves as the building block of Object-Oriented Programming.
It also has access specifiers, which restrict the access of member elements. The primarily used ones are the following:
The class can be represented in the form of
class ClassName { access_specifier1: type1 val1; type2 val2; ret_type1 method1(type_arg1 arg1, type_arg2 arg2,...) ... access_specifier2: type3 val3; type4 val4; ret_type2 method2(type_arg3 arg3, type_arg4 arg4,...) ... };
It’s common practice to make all variables private, and set or get them using public methods. For example:
class SampleClass { private: int val; public: void set(int a) { val = a; } int get() { return val; } };
We can store details related to a student in a class consisting of his age(int), first_name(string), last_name(string) and standard(int).
You’ve to create a class named Student, representing the student’s details, as mentioned above, and store the data of a student. Create setter and getter functions for each element i.e the class should at least have the following functions:
Also, you’ve to create another method to_string() which returns the string consisting of the above elements, separated by a comma (,). You can refer to stringstream for this.
The input will consist of four lines. The first line will contain an integer, representing the age. The second line will contain a string, consisting of lower-case Latin characters (‘a’-‘z’), representing the first_name of a student. The third line will contain another string, consisting of lower-case Latin characters (‘a’-‘z’), representing the last_name of a student. The fourth line will contain an integer, representing the standard of students.
Note: The number of characters in first_name and last_name will not exceed 50.
The code provided by HackerRank will use your class members to set and then get the elements of the Student class.
15
john
carmack
10
15
carmack, john
10
15,john,carmack,10
#include <iostream> #include <sstream> using namespace std; class Student { private: int age; string first_name; string last_name; int standard; public: void set_age(int a) { age = a; } int get_age() { return age; } void set_first_name(string fn) { first_name = fn; } string get_first_name() { return first_name; } void set_last_name(string ln) { last_name = ln; } string get_last_name() { return last_name; } void set_standard(int s) { standard = s; } int get_standard() { return standard; } string to_string() { stringstream ss; ss << age << "," << first_name << "," << last_name << "," << standard; return ss.str(); } }; int main() { int age, standard; string first_name, last_name; cin >> age >> first_name >> last_name >> standard; Student st; st.set_age(age); st.set_standard(standard); st.set_first_name(first_name); st.set_last_name(last_name); cout << st.get_age() << "\n"; cout << st.get_last_name() << ", " << st.get_first_name() << "\n"; cout << st.get_standard() << "\n"; cout << "\n"; cout << st.to_string(); return 0; }
Output:
Explanation:
In the above code, we’ve created the getters and setters method for the variables age, first_name, last_name, and the standard as described in the question. We’ve also created the tostring() method to return the string.
After compiling the above code, the compiler asks the user to enter the input i.e 15, john, Carmack, 10 and these inputs are set to the object created using setters.
And at last, the input is fetched using the getter functions and then displayed using the tostring() method.
#include <iostream> #include <sstream> using namespace std; class Student{ public: int age, standard; string first_name, last_name; void set_age(int a){ age = a; } void set_first_name(string a){ first_name = a; } void set_last_name(string a){ last_name = a; } void set_standard(int a){ standard = a; } int get_age(){ return age; } string get_first_name(){ return first_name; } string get_last_name(){ return last_name; } int get_standard(){ return standard; } string t_string(){ string s = ""; s+=to_string(age); s+=","; s+=first_name; s+=","; s+=last_name; s+=","; s+=to_string(standard); return s; } }; int main() { int age, standard; string first_name, last_name; cin >> age >> first_name >> last_name >> standard; Student st; st.set_age(age); st.set_standard(standard); st.set_first_name(first_name); st.set_last_name(last_name); cout << st.get_age() << "\n"; cout << st.get_last_name() << ", " << st.get_first_name() << "\n"; cout << st.get_standard() << "\n"; cout << "\n"; cout << st.t_string(); return 0; }
Output:
So, in this way we’ve learned how to code the class Hackerrank solution in c++ with an explanation. If you’ve any doubts then please feel free to contact us. We’ll reach you as soon as possible.
Thanks and Happy Coding :)
Also Read:
This website uses cookies.