Class Hackerrank Solution in C++ | Classes C++ Solutions

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:

public: Public members (variables, methods) can be accessed from anywhere the code is visible.
private: Private members can be accessed only by other member functions, and they can not be accessed outside of class.

Class Hackerrank Solution in C++

The class can be represented in the form of

class ClassName {
        type1 val1;
        type2 val2;
        ret_type1 method1(type_arg1 arg1, type_arg2 arg2,...)
        type3 val3;
        type4 val4;
        ret_type2 method2(type_arg3 arg3, type_arg4 arg4,...)

It's a common practice to make all variables private, and set/get them using public methods. For example:

class SampleClass {
        int val;
        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 have 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; that is, the class should at least have the following functions:

  • get_age, set_age
  • get_first_name, set_first_name
  • get_last_name, set_last_name
  • get_standard, set_standard

Also, you have 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.

Input Format

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.

Output Format

The code provided by HackerRank will use your class members to set and then get the elements of the Student class.

Sample Input


Sample Output

carmack, john


Submit your solution here: Click here

Class Hackerrank Solution in C++

#include <iostream>
#include <sstream>
using namespace std;

Enter code for class Student here.
Read statement for specification.

class Student
		int age;
	string firstName;
	string lastName;
	int standard;
		void set_age(int a)
			age = a;

	int get_age()
		return age;

	void set_first_name(string fName)
		firstName = fName;

	string get_first_name()
		return firstName;

	void set_last_name(string lName)
		lastName = lName;

	string get_last_name()
		return lastName;

	void set_standard(int s)
		standard = s;

	int get_standard()
		return standard;

	string to_string()
		stringstream ss;
		ss << age << "," << firstName << "," << lastName << "," << standard;
		return ss.str();

int main()
	int age, standard;
	string first_name, last_name;

	cin >> age >> first_name >> last_name >> standard;

	Student st;

	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;

The Output of Class Hackerrank Solution

The Output of Class Hackerrank Solution

Similar to Class

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