1
\$\begingroup\$

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 <iostream>
#include <sstream>
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 >> 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";

    ss << st.get_age() <<","<<st.get_first_name() <<","<<st.get_last_name()<<","<< st.get_standard();
    cout<<ss.str()<<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?
\$\endgroup\$
  • \$\begingroup\$ Challenge says "Also, you have to create another method to_string()". Where is it? \$\endgroup\$ – Mathias Ettinger Nov 9 '16 at 23:10
  • \$\begingroup\$ @MathiasEttinger I just assumed I could avoid it by using ss << ... in this way. If this is wrong then I would update my answer asap? \$\endgroup\$ – Siobhan Nov 9 '16 at 23:20
  • \$\begingroup\$ It's just that reading the description, I expected it to be defined. So you can simply do std::cout << st.to_string() << "\n"; in main. But since you get the desired input as well, it might be as valid… probably. Looking at the example output have me wondering that they will be trying to call get_age, get_last_name, get_first_name, get_standard and to_string in that order. \$\endgroup\$ – Mathias Ettinger Nov 9 '16 at 23:34
2
\$\begingroup\$
  1. You are writing a C++ program, so why have you used C Style comments?
  2. 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.
  3. 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.
  4. Defining multiple variables on a line is a maintenance nightmare, also it means you can write comments explaining what they are used for.
  5. 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.
  6. Rather than calling 4 set functions why not define a constructor that takes for optional parameters?
  7. 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.
  8. You should get into the habit of commenting the code better.
  9. 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.
  10. 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.
  11. Always include all header files you need, it makes it easier to reuse code because everything you need is there. (You missed ).
  12. The code should be in 2 files, the class in one and main in another.

`

#include <string>
#include <iostream>
#include <sstream>

// 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& fName, const std::string& lName, const int& 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 >> age >> first_name >> last_name >> standard;

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

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

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

    return 0;
}

`

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.