Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

HackerRank Class solution in C++ programming

YASH PAL, 31 July 202416 January 2026

HackerRank Class C++ solution – In this HackerRank class problem solution in C++ programming language, Classes in C++ are user-defined types declared with the keyword class that have 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 primary 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 it can not be accessed outside of the class.

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,…)

        …

};

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 following functions:

  1. get_age, set_age
  2. get_first_name, set_first_name
  3. get_last_name, set_last_name
  4. 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(,). 

HackerRank Class solution in c++ programming

HackerRank Class problem solution in c++ programming.

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

class Student{
    int age;
    int standard;
    string first_name;
    string last_name;
public:
    Student()
    {
        age = 0;
        standard = 0;
        first_name.clear();
        last_name.clear();
    }
    void set_age(int newAge)
    {
        age = newAge;
    }
    void set_standard(int newStandard)
    {
        standard = newStandard;
    }
    void set_first_name(string newFirst_name)
    {
        first_name = newFirst_name;
    }
    void set_last_name(string newLast_name)
    {
        last_name = newLast_name;
    }
    int get_age() {return age;}
    int get_standard() {return standard;}
    string get_first_name() {return first_name;}
    string get_last_name() {return last_name;}
    
    string to_string()
    {
        stringstream ss;
        char c = ',';
        ss << age << c << first_name << c << last_name << c << 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;
}

Second solution

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

/*
Enter code for class Student here.
Read statement for specification.
*/
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;
}
C++ Solutions coding problems solutions Hackerrank Problems Solutions cppHackerRank

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

HackerRank C++ Problems Solutions
Say “Hello, World!” with C++ solution
Input and Output solution
Basic Data Types solution
Conditional Statements solution
For loop solution
Functions solution
Pointer solution
Arrays Introduction solution
Variable-Sized Arrays solution
Attribute Parser solution
StringStream solution
Strings solution
Structs solution
Class solution
classes and objects solution
Box It! solution
Inherited code solution
Exceptional server solution
Virtual Functions solution
Abstract Classes — Polymorphism solution
Vector-Sort solution
Vector-Erase solution
Lower Bound-STL solution
Sets-STL solution
Maps-STL solution
Print Pretty solution
Deque-STL solution
Inheritance Introduction solution
Hotel Prices solution
Cpp exception handling solution
Rectangle Area solution
Multi-Level Inheritance solution
Overloading Ostream Operator solution
Messages Order solution
Accessing Inherited Functions solution
Magic Spells solution
C++ Class Templates problem solution
Preprocessor Solution
Operator Overloading solution
Overload Operators solution
Attending workshops solution
C++ Class Template Specialization solution
C++ Variadics problem solution
Bit Array solution
CLOSE ADS
CLOSE ADS

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes