Student Class in C++ using sstream for format Based on my previous review, I'm posting another Hackerrank Class solution.


  Problem Statement
  
  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:
  
  
  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.
   NB: I didn't implement the to_string() as I felt I could use sstream in   the main().
  
  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 student.
  
  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

15
john
carmack
10

  
  Sample Output

15
carmack, john
10

15,john,carmack,10



Here is my code:

#include &lt;iostream&gt;
#include &lt;sstream&gt;
using namespace std;

/*
Enter code for class Student here.
Read statement for specification.
*/
class Student{

    int age;
    string first_name;
    string last_name;
    int standard;

    public:
          int get_age(){
              return age;
          }
          void set_age(int age1){
              age = age1;
          }
           string get_first_name(){
              return first_name;
          }
          void set_first_name(string first_name1){
              first_name = first_name1;
          }
          string get_last_name(){
              return last_name;
          }
          void set_last_name(string last_name1){
              last_name = last_name1;
          }
           int get_standard(){
              return standard;
          }
          void set_standard(int standard1){
              standard = standard1;
          }

};

int main() {
    int age, standard;
    string first_name, last_name;
    stringstream ss;

    cin &gt;&gt; age &gt;&gt; first_name &gt;&gt; last_name &gt;&gt; standard;

    Student st;
    st.set_age(age);
    st.set_standard(standard);
    st.set_first_name(first_name);
    st.set_last_name(last_name);

    cout &lt;&lt; st.get_age() &lt;&lt; "\n";
    cout &lt;&lt; st.get_last_name() &lt;&lt; ", " &lt;&lt; st.get_first_name() &lt;&lt; "\n";
    cout &lt;&lt; st.get_standard() &lt;&lt; "\n";
    cout &lt;&lt; "\n";

    ss &lt;&lt; st.get_age() &lt;&lt;","&lt;&lt;st.get_first_name() &lt;&lt;","&lt;&lt;st.get_last_name()&lt;&lt;","&lt;&lt; st.get_standard();
    cout&lt;&lt;ss.str()&lt;&lt;endl;

    return 0;
}


Final thoughts:


Have I used sstream correctly? Have I used it to its full capacity?
What could have been done better, such as style-wise?
You are writing a C++ program, so why have you used C Style comments?
If parameters aren't changing it makes it clearer to readers/maintainers if you declare them as const.  It they are const they may as well be const references.
The point of namespaces is to provide a degree of separation of the code in libraries, so they don't clash.  using namespace std; breaks this don't use it.
Defining multiple variables on a line is a maintenance nightmare, also it means you can write comments explaining what they are used for.
There are very few cases when not assigning an initial value to a variable is a good idea.  The first time you production code crashes on the customer's machine you will appreciate why initialising variables is a good idea.
Rather than calling 4 set functions why not define a constructor that takes for optional parameters?
Rather than letting main() format the output why not write a function that displays the student record in the format that the class dictates.  To make it easier, just right the data to a multi line string.
You should get into the habit of commenting the code better.
Although you don't need to explicitly declare the class members as private it is a good idea to do so, because then someone can add a public function at the top of the class without exposing all your variables.
Its a personal preference but I think the Java style of curly braces {} shouldn't be used in C++.  It makes the code clearer if every { and } is on a separate line.  IMO.
Always include all header files you need, it makes it easier to reuse code because everything you need is there.  (You missed ).
The code should be in 2 files, the class in one and main in another.


`  

#include &lt;string&gt;
#include &lt;iostream&gt;
#include &lt;sstream&gt;

// Enter code for class Student here.
// Read statement for specification.
class Student
{
private:
    int m_age;
    string m_first_name;
    string m_last_name;
    int m_standard;

public:
    Student(const int age, const std::string&amp; fName, const std::string&amp; lName, const int&amp; standard)
        : m_age(age)
        , m_first_name(fName)
        , m_last_name(lName)
        , m_standard(standard)
    {
    }

    int get_age() const
    {
        return m_age;
    }
    std::string get_first_name() const
    {
        return m_first_name;
    }
    std::string get_last_name() const 
    {
        return m_last_name;
    }
    int get_standard() const
    {
        return m_standard;
    }
};

int main() 
{
    int age(-1)
    int standard(-1);
    std::string first_name();
    std::string last_name();
    stringstream ss();

    cin &gt;&gt; age &gt;&gt; first_name &gt;&gt; last_name &gt;&gt; standard;

    Student st(age, first_name, last_name, standard);

    cout &lt;&lt; st.get_age() &lt;&lt; "\n";
    cout &lt;&lt; st.get_last_name() &lt;&lt; ", " &lt;&lt; st.get_first_name() &lt;&lt; "\n";
    cout &lt;&lt; st.get_standard() &lt;&lt; "\n";
    cout &lt;&lt; "\n";

    ss &lt;&lt; st.get_age() &lt;&lt; "," &lt;&lt; st.get_first_name() &lt;&lt; "," &lt;&lt; st.get_last_name() &lt;&lt; "," &lt;&lt; st.get_standard();
    cout &lt;&lt; ss.str() &lt;&lt; endl;

    return 0;
}


`