Recent Post

Class.cpp-HackerRank

Problem-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:
  • get_ageset_age
  • get_first_nameset_first_name
  • get_last_nameset_last_name
  • get_standardset_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.


Solution-
#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();
}

Link to the problem:-

Result!

Comments

Popular posts from this blog

Caesar Cipher.c-HackerRank

Bon Appétit.c-HackerRank

Electronics Shop.c-HackerRank