C++ - Class

该博客介绍了一个C++编程任务,要求创建一个名为`Student`的类,用于存储学生的年龄、名字、姓氏和年级等信息。类中包含了设置和获取这些属性的方法,以及一个将所有信息以逗号分隔的字符串形式返回的`to_string`方法。输入包括四行,分别代表年龄、名字、姓氏和年级,输出则是各个属性的值以及用逗号分隔的字符串表示。
摘要由CSDN通过智能技术生成

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.

Sample Input

15
john
carmack
10

Sample Output

15
carmack, john
10

15,john,carmack,10

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

/*
Enter code for class Student here.
Read statement for specification.
*/
class Student{
    private:
        int age;
        string first_name;
        string last_name;
        int standard;
    public:
    	// void
        void set_age (int a){
            age = a;
        }
        void set_first_name (string b){
            first_name = b;
        }
        void set_last_name (string c){
            last_name = c;
        }
        void set_standard (int d){
            standard = d;
        }
        
        //getter
        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 to_string ()
    {
        stringstream xx;
        char c = ',';
        xx << age << c << first_name << c << last_name << c << standard;
        return xx.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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值