HackerRank C++ Class




ou 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.



  1. #include <iostream>
  2. using namespace std;
  3. class Student
  4. {
  5. int age, standard;
  6. string first_name, last_name;
  7. public:
  8. void set_age(int age)
  9. {
  10. this->age=age;
  11. }
  12. void set_standard(int standard)
  13. {
  14. this->standard=standard;
  15. }
  16. void set_first_name(string fname)
  17. {
  18. first_name=fname;
  19. }
  20. void set_last_name(string lname)
  21. {
  22. last_name=lname;
  23. }
  24. int get_age()
  25. {
  26. return age;
  27. }
  28. int get_standard()
  29. {
  30. return standard;
  31. }
  32. string get_first_name()
  33. {
  34. return(first_name);
  35. }
  36. string get_last_name()
  37. {
  38. return(last_name);
  39. }
  40. void to_string(){
  41. cout<<age<<","<<first_name<<","<<last_name<<","<<standard;
  42. }
  43. };
  44. int main() {
  45. int age, standard;
  46. string first_name, last_name;
  47. cin >> age >> first_name >> last_name >> standard;
  48. Student st;
  49. st.set_age(age);
  50. st.set_standard(standard);
  51. st.set_first_name(first_name);
  52. st.set_last_name(last_name);
  53. cout << st.get_age() << "\n";
  54. cout << st.get_last_name() << ", " << st.get_first_name() << "\n";
  55. cout << st.get_standard() << "\n";
  56. cout << "\n";
  57. st.to_string();
  58. return 0;
  59. }




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube