Class Hackerrank Problem Solution

 Class Hackerrank Problem Solution

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 it can not be accessed outside of 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_arg3 arg3,...)
        ...
};

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

class SampleClass {
    private:
        int val;
    public:
        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 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 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

Solution

Q.

what's the difference between setting the variables like
void setVar(int var){
    this->var=var;
}
and
void setVar(int var){
    var=var;
}

When using the second approach, consider the following class with the instance variable called var:
class ClassName {
private:
int var;
public:
void setVar(int var) {
var = var;
}
}
In this case, there is a naming conflict between your class instance variable and the parameter name of the setter method. Not the instance variable ClassName::var is set, but instead you assign the var parameter to itself. As you can imagine, this does not have any effect on the class instance variable.
This naming problem is solved by using your first approach with

void setVar(int var) {
    this->var = var;
}

or by explicitly stating the instance of the class like for our example

void setVar(int var) {
    ClassName::var = var;
}

As far as I know, there is no difference between the last two options.

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

class Student{
    private:
    int ag, std;
    string fn, ln;
    public:
    void set_age(int age){
        ag=age;
    }
    int get_age() {
        return ag;
    }
    
    string get_first_name(){
    return fn;
    }
     string get_last_name(){
    return ln;
    }
    void set_first_name(string first_name){
        fn=first_name;
    }
    void set_last_name(string last_name){
        ln=last_name;
    }
    int get_standard(){
        return std;
    }
    void set_standard(int standard){
        std=standard;
    } 
    string to_string(){
         cout<<ag<<","<<fn<<","<<ln<<","<<std;
    return " ";
    }
};
/*
Enter code for class Student here.
Read statement for specification.
*/

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;
}

Q.What is the reason for creating the constructor Student(). My code passed without the use of the constructor. Is it best practice to use constructor while defining class in C++.?

Yes contructors and destructors are guidelines for good practises. Also when you are calling a your class members in a separate cpp file to define your functions, constructors are needed to set the default values.
Q. what do you choose string or stringstream
When a person returns a string, one of two things happen: 1. They return the string as-is. 2. They return the string after having been appended to by other data, AKA Concatenation.
Stringstream allows one to bypass the concatenation process, bringing the code to fewer lines.

Class Hackerrank Problem Solution
All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin @coderinme

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.