Class C++
Problem Statement :
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_arg4 arg4,...) ... }; 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.
Solution :
Solution in C :
#include <iostream>
#include <sstream>
using namespace std;
/*
Enter code for class Student here.
Read statement for specification.
*/
class Student{
public:
int age, standard;
string first_name, last_name;
void set_age(int a){
age = a;
}
void set_first_name(string a){
first_name = a;
}
void set_last_name(string a){
last_name = a;
}
void set_standard(int a){
standard = a;
}
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 t_string(){
string s = "";
s+=to_string(age);
s+=",";
s+=first_name;
s+=",";
s+=last_name;
s+=",";
s+=to_string(standard);
return s;
}
};
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.t_string();
return 0;
}
View More Similar Problems
Array and simple queries
Given two numbers N and M. N indicates the number of elements in the array A[](1-indexed) and M indicates number of queries. You need to perform two types of queries on the array A[] . You are given queries. Queries can be of two types, type 1 and type 2. Type 1 queries are represented as 1 i j : Modify the given array by removing elements from i to j and adding them to the front. Ty
View Solution →Median Updates
The median M of numbers is defined as the middle number after sorting them in order if M is odd. Or it is the average of the middle two numbers if M is even. You start with an empty number list. Then, you can add numbers to the list, or remove existing numbers from it. After each add or remove operation, output the median. Input: The first line is an integer, N , that indicates the number o
View Solution →Maximum Element
You have an empty sequence, and you will be given N queries. Each query is one of these three types: 1 x -Push the element x into the stack. 2 -Delete the element present at the top of the stack. 3 -Print the maximum element in the stack. Input Format The first line of input contains an integer, N . The next N lines each contain an above mentioned query. (It is guaranteed that each
View Solution →Balanced Brackets
A bracket is considered to be any one of the following characters: (, ), {, }, [, or ]. Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and (). A matching pair of brackets is not balanced if the set of bra
View Solution →Equal Stacks
ou have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You can change the height of a stack by removing and discarding its topmost cylinder any number of times. Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you must remove zero or more cylinders from the top of zero or more of
View Solution →Game of Two Stacks
Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f
View Solution →