직전에 계속 작성하던 MyString 클래스는 C스타일을 C++로 작성한 것이다.
퍼포먼스가 중요한 환경에서는 쓰기에는 많이 부족하다.
그래서 뛰어난 개발자들이 C++ 표준으로 편입시킨 클래스가 string이다.
#include<iostream>
#include<string>
int main()
{
std::string s = "abc";
std::cout << s << std::endl;
return 0;
}
실행결과
abc
#include<iostream>
#include<string>
int main()
{
std::string s = "abc";
std::string t = "def";
std::string s2 = s;
std::cout << s << " 의 길이 : " << s.length() << std::endl;
std::cout << s << " 뒤에 " << t << " 를 붙이면 : " << s + t << std::endl;
if(s == s2)
{
std::cout << s << " 와 " << s2 << " 는 같다" << std::endl;
}
if(s != t)
{
std::cout << s << " 와 " << t << " 는 다르다 " << std::endl;
}
return 0;
}
실행결과
abc 의 길이 3
abc 뒤에 def 를 붙이면 abcdef
abc 와 abc는 같다.
abc 와 def는 다르다
class Employee
{
std::string name;
int age;
std::string position; // 직책
int rank; // 순위 (높을수록 우선순위)
int year_of_service; // 근속년수
public:
Employee(std::string name, int age, std::string position, int rank) :
name(name), age(age), position(position), rank(rank) {}
Employee(const Employee& employee)
{
name = employee.name;
age = employee.age;
position = employee.position;
rank = employee.rank;
}
Employee() {}
void print_info()
{
std::cout << name << " (" << position << " , " << age << ") ==> " << calculate_pay() << "만원" << std::endl;
}
int calculate_pay() { return 200 + rank * 50 }
};
class EmployeeList
{
int alloc_employee;
int current_employee;
Employee **employee_list;
public:
EmployeeList(int alloc_employee) : alloc_employee(alloc_employee)
{
employee_list = new Employee*[alloc_employee];
current_employee = 0;
}
void add_employee(Employee* employee)
{
employee_list[current_employee] = employee;
current_employee++;
}
int current_employee_num() { return current_employee; }
void print_employee_info()
{
int total_pay = 0;
for (int i = 0; i < current_employee; i++)
{
employee_list[i] -> print_info();
total_pay += employee_list[i]->calculate_pay();
}
std::cout << "총 비용 : " << total_pay << "만원" << std::endl;
}
~EmployeeList()
{
for(int i = 0; i < current_employee; i++)
{
delete employee_list[i];
}
delete[] employee_list;
}
};
int main()
{
EmployeeList emp_list(10);
emp_list.add_employee(new Employee("노홍철", 34, "평사원", 1));
emp_list.add_employee(new Employee("하하", 34, "평사원", 1));
emp_list.add_employee(new Employee("유재석", 41, "부장", 7));
emp_list.add_employee(new Employee("정준하", 43, "과장", 4));
emp_list.add_employee(new Employee("=박명수", 43, "차장", 5));
emp_list.add_employee(new Employee("정형돈", 36, "대리", 2));
emp_list.add_employee(new Employee("노홍철", 36, "인턴", -2));
emp_list.print_employee_info();
return 0;
}
노홍철 (평사원 , 34) ==> 250만원
하하 (평사원 , 34) ==> 250만원
유재석 (부장 , 41) ==> 550만원
정준하 (과장 , 43) ==> 400만원
박명수 (차장 , 43) ==> 450만원
정형돈 (대리 , 36) ==> 300만원
노홍철 (인턴 , 36) ==> 100만원
총 비용 : 2300만원