"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."
i have to complete this exercise in hackerank and i dont know why it doesnt work.
#include <iostream>
#include <sstream>
#include<string>
using namespace std;
class Student{
private:
int val,val1;
string fn,sn;
int get_age(){return val;};
int get_standard(){return val1;};
int get_first_name(){return fn;};
int get_last_name(){return sn;};
};
int main() {
int age, standard;
string first_name, last_name;
cin >> age >> first_name >> last_name >> standard;
Also, you have to create another method to_string() which returns the string consisting of the above elements, separated by a comma(,)
Why is your to_string function void instead of returning a string ?
Why I ran your code on the shell I got tons of errors / warnings
26:1: error: expected ';' after class definition In member function 'void Student::set_age(int)':
11:28: warning: unused variable 'val' [-Wunused-variable] In member function 'void Student::set_standard(int)':
12:37: warning: unused variable 'val1' [-Wunused-variable] In member function 'void Student::to_string()':
25:7: error: 'age' was not declared in this scope
25:17: error: 'first_name' was not declared in this scope
25:34: error: 'last_name' was not declared in this scope
25:50: error: 'standard' was not declared in this scope In function 'int get_age()':
28:22: error: 'val' was not declared in this scope At global scope:
28:27: warning: extra ';' [-Wpedantic] In function 'int get_standard()':
29:27: error: 'val1' was not declared in this scope At global scope:
29:33: warning: extra ';' [-Wpedantic] In function 'int get_first_name()':
30:29: error: 'fn' was not declared in this scope At global scope:
30:33: warning: extra ';' [-Wpedantic] In function 'int get_last_name()':
31:28: error: 'sn' was not declared in this scope At global scope:
31:32: warning: extra ';' [-Wpedantic]
32:1: error: expected declaration before '}' token In function 'int get_age()':
28:26: warning: control reaches end of non-void function [-Wreturn-type] In function 'int get_standard()':
29:32: warning: control reaches end of non-void function [-Wreturn-type] In function 'int get_first_name()':
30:32: warning: control reaches end of non-void function [-Wreturn-type] In function 'int get_last_name()':
31:31: warning: control reaches end of non-void function [-Wreturn-type]