Class

I received an email from HackerRank inviting me to solve the Class challenge. I like the reminder feature because I do not have to think about challenges during the workday. They just pop into my in box and I just have to pick a time to solve them.

Following is a screen capture of a console generated when using the sample input data for the challenge:

15
john
carmack
10
15
carmack, john
10

15,john,carmack,10

The code for my solution implemented in C++ using Visual Studio Enterprise Edition 2017 follows:

#include <iostream>
#include <sstream>

using namespace std;

/*
Student class.
*/

class Student {

	// **** members ****

	private: int age;
	private: string first_name;
	private: string last_name;
	private: int standard;

	// **** getters and setters ****

public:
	void set_age(int a) {
	age = a;
	}

	void set_first_name(string fn) {
		first_name = fn;
	}

	void set_last_name(string ln) {
		last_name = ln;
	}

	void set_standard(int s) {
		standard = s;
	}

	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 to_string() {
		stringstream ss;
		ss << age << "," << first_name << "," << last_name << "," << standard; return ss.str(); } }; /* Test code. */ 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;
}

Not much more to add. Is a simple implementation of a class with getters and setters.

If you have comments or questions regarding this post please fill in the form at the end of this post. I will reply as soon as possible.

Enjoy;

John

Follow me on Twitter:  @john_canessa

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.