We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I used class, methods stringstream for the first time.
Learned typically we set properties in private then access to them using setters and getters for each properties.
#include<iostream>#include<sstream>usingnamespacestd;/*Enter code for class Student here.Read statement for specification.*/classStudent{private:intstudent_age,student_standard;stringstudent_first_name,student_last_name;stringstreamcombined_attrs;public:voidset_age(intage){student_age=age;}voidset_standard(intstandard){student_standard=standard;}voidset_first_name(stringfirst_name){student_first_name=first_name;}voidset_last_name(stringlast_name){student_last_name=last_name;}intget_age(){returnstudent_age;}intget_standard(){returnstudent_standard;}stringget_first_name(){returnstudent_first_name;}stringget_last_name(){returnstudent_last_name;}stringto_string(){combined_attrs<<student_age<<","<<student_first_name<<","<<student_last_name<<","<<student_standard;returncombined_attrs.str();}};intmain(){intage,standard;stringfirst_name,last_name;cin>>age>>first_name>>last_name>>standard;Studentst;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();return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Class
You are viewing a single comment's thread. Return to all comments →
I used class, methods stringstream for the first time. Learned typically we set properties in private then access to them using setters and getters for each properties.