Training/HackerRank

[C++][Classes] Class

FATKITTY 2021. 8. 7. 01:08
반응형

https://www.hackerrank.com/challenges/c-tutorial-class/problem

 

#include <iostream>
#include <sstream>
using namespace std;

class Student {
    private:
        int age, standard;
        string first_name, last_name;
    public:
    int get_age() { return age; }
    string get_first_name() { return first_name; }
    string get_last_name() { return last_name; }
    int get_standard() { return standard; }
    void set_age(int a) { age = a; }
    void set_standard(int s) { standard = s; }
    void set_first_name(string f) { first_name = f; }
    void set_last_name(string l) { last_name = l; }
    string to_string() { 
        string str;
        stringstream ss;
        ss << age << "," << first_name << "," << last_name << "," << standard;
        ss >> str;
        return str;
    }
};

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;
}

 

반응형

'Training > HackerRank' 카테고리의 다른 글

[C++][Introduction] Variable Sized Arrays  (0) 2021.08.07
[C++][STL] Vector-Erase  (0) 2021.08.07
[C++][Strings] StringStream  (0) 2021.08.04
[C++][Introduction] Pointer  (0) 2021.08.03
[C++][Implementation] Breaking the Records  (0) 2021.08.01