#include <iostream>
#include <string>
#include <vector>
class Student
{
private:
int id;
int age;
int grade;
std::string name;
std::string college;
std::string profession;
std::vector<std::string> awards;
Student(int id, int age, int grade, const std::string &name, const std::string &college,
const std::string &profession, const std::vector<std::string> &awards)
: id(id), age(age), grade(grade), name(name), college(college), profession(profession), awards(awards)
{
}
public:
class StudentBuilder
{
private:
int id;
int age;
int grade;
std::string name;
std::string college;
std::string profession;
std::vector<std::string> awards;
public:
StudentBuilder &setId(int id)
{
this->id = id;
return *this;
}
StudentBuilder &setAge(int age)
{
this->age = age;
return *this;
}
StudentBuilder &setGrade(int grade)
{
this->grade = grade;
return *this;
}
StudentBuilder &setName(const std::string &name)
{
this->name = name;
return *this;
}
StudentBuilder &setCollege(const std::string &college)
{
this->college = college;
return *this;
}
StudentBuilder &setProfession(const std::string &profession)
{
this->profession = profession;
return *this;
}
StudentBuilder &setAwards(const std::vector<std::string> &awards)
{
this->awards = awards;
return *this;
}
Student build()
{
return Student(id, age, grade, name, college, profession, awards);
}
};
static StudentBuilder builder()
{
return StudentBuilder();
}
void print()
{
std::cout << "id: " << id << std::endl;
std::cout << "age: " << age << std::endl;
std::cout << "grade: " << grade << std::endl;
std::cout << "name: " << name << std::endl;
std::cout << "college: " << college << std::endl;
std::cout << "profession: " << profession << std::endl;
std::cout << "awards: ";
for (auto &award : awards)
{
std::cout << award << " ";
}
std::cout << std::endl;
}
};
int main(void)
{
Student stu = Student::builder()
.setId(1)
.setAge(18)
.setGrade(1)
.setName("zhangsan")
.setCollege("computer")
.setProfession("computer science")
.setAwards({"first prize", "second prize"})
.build();
stu.print();
return 0;
}