이어서 좀더 진행해보겠습니다.
Main.cpp를 다음과 같이 바꿔보겠습니다.
- Write a test that fails
- Make the code work
- Eliminate redundancy
[main.cpp]
#include "student.h"
int main(void)
{
// 1. Test 1번
Student* s = new Student();
assert(s);
delete s;
// 2. Test 2번
// 3. Test 3번
s = new Student("이원재",19,100979571,"서울시 관악구 XXXX");
assert(!strcmp(s->getName(), "이원재"));
assert(s->getAge() == 19);
assert(s->getStudentID() == 100979571);
assert(!strcmp(s->getAddress(),"서울시 관악구 XXXX"));
delete s;
return 0;
}
|
그러면 아래와 같이 class의 멤버함수가 없으므로 error를 발생합니다.
그럼 test code가 실패하지 않도록 코드를 수정하겠습니다.
- Write a test that fails
- Make the code work
- Eliminate redundancy
[student.h]
#undef UNICODE
#include <stdio.h>
#include <string.h>
#include <assert.h>
class Student
{
private:
char Name[1024];
int age;
int studentID;
char address[1024];
public:
Student();
Student(char* _Name, int _age, int _studentID, char* _address);
char* getName();
int getAge();
int getStudentID();
char* getAddress();
};
|
[student.cpp]
#include "student.h"
Student::Student()
{
::memset(Name, 0,sizeof(char)*1024);
::memset(address, 0,sizeof(char)*1024);
age = 0;
studentID = 0;
}
Student::Student(char* _Name, int _age, int _studentID, char* _address)
{
::memset(Name, 0,sizeof(char)*1024);
::memset(address, 0,sizeof(char)*1024);
memcpy(Name,_Name,strlen(_Name)*sizeof(char));
age = _age;
studentID = _studentID;
memcpy(address,_address,strlen(_address)*sizeof(char));
}
char* Student::getName()
{
return this->Name;
}
int Student::getAge()
{
return this->age;
}
int Student::getStudentID()
{
return this->studentID;
}
char* Student::getAddress()
{
return this->address;
}
|
F5를 누르니 잘된다. ㅎㅎ 이제 Refectoring이다. 고고
- Write a test that fails
- Make the code work
- Eliminate redundancy
우선 호출하는 부위에서 해당 포인터를 가지고 변경하는 실수를 막기위해 const를 추가해봤다.
[student.h]
#undef UNICODE
#include <stdio.h>
#include <string.h>
#include <assert.h>
class Student
{
private:
char Name[1024];
int age;
int studentID;
char address[1024];
public:
Student();
Student(char* _Name, int _age, int _studentID, char* _address);
const char* getName();
int getAge();
int getStudentID();
const char* getAddress();
};
|
[student.cpp]
#include "student.h"
Student::Student()
{
::memset(Name, 0,sizeof(char)*1024);
::memset(address, 0,sizeof(char)*1024);
age = 0;
studentID = 0;
}
Student::Student(char* _Name, int _age, int _studentID, char* _address)
{
::memset(Name, 0,sizeof(char)*1024);
::memset(address, 0,sizeof(char)*1024);
memcpy(Name,_Name,strlen(_Name)*sizeof(char));
age = _age;
studentID = _studentID;
memcpy(address,_address,strlen(_address)*sizeof(char));
}
const char* Student::getName()
{
return this->Name;
}
int Student::getAge()
{
return this->age;
}
int Student::getStudentID()
{
return this->studentID;
}
const char* Student::getAddress()
{
return this->address;
}
|
F5를 눌러보면 success 여기까지도 잘 되었다.
이제 get를 했는니 set되 해볼까? ㅋㅋ
- Write a test that fails
- Make the code work
- Eliminate redundancy
아래와 같이 class 변수 접근 가능한 set 변수를 만들었다. ㅋㅋ 그럼 초기화 될까. Run Run Run
[main.cpp]
#include "student.h"
int main(void)
{
// 1. Test 1번
Student* s = new Student();
assert(s);
delete s;
// 2. Test 2번
// 3. Test 3번
s = new Student("이원재",19,100979571,"서울시 관악구 XXXX");
assert(!strcmp(s->getName(), "이원재"));
assert(s->getAge() == 19);
assert(s->getStudentID() == 100979571);
assert(!strcmp(s->getAddress(),"서울시 관악구 XXXX"));
delete s;
// 4. Test 4번
s = new Student();
s->setName("이원재");
s->setAge(19);
s->setStudentID("100979571");
s->setAddress("서울시 관악구 XXX");
assert(!strcmp(s->getName(), "이원재"));
assert(s->getAge() == 19);
assert(s->getStudentID() == 100979571);
assert(!strcmp(s->getAddress(),"서울시 관악구 XXXX"));
delete s;
return 0;
}
|
당연히 Fail이다. 계속 이런식이다. ㅎㅎ 우리는 수정하면서 고치면된다. 고고
- Write a test that fails
- Make the code work
- Eliminate redundancy
[student.h]
#undef UNICODE
#include <stdio.h>
#include <string.h>
#include <assert.h>
class Student
{
private:
char Name[1024];
int age;
int studentID;
char address[1024];
public:
Student();
Student(char* _Name, int _age, int _studentID, char* _address);
const char* getName();
int getAge();
int getStudentID();
const char* getAddress();
void setName( char* );
void setAge( int _age );
void setStudentID(int _studentID );
void setAddress( char* _address );
};
|
[student.cpp]
#include "student.h"
Student::Student()
{
::memset(Name, 0,sizeof(char)*1024);
::memset(address, 0,sizeof(char)*1024);
age = 0;
studentID = 0;
}
Student::Student(char* _Name, int _age, int _studentID, char* _address)
{
::memset(Name, 0,sizeof(char)*1024);
::memset(address, 0,sizeof(char)*1024);
memcpy(Name,_Name,strlen(_Name)*sizeof(char));
age = _age;
studentID = _studentID;
memcpy(address,_address,strlen(_address)*sizeof(char));
}
const char* Student::getName()
{
return this->Name;
}
int Student::getAge()
{
return this->age;
}
int Student::getStudentID()
{
return this->studentID;
}
const char* Student::getAddress()
{
return this->address;
}
void Student::setName( char* _Name)
{
memcpy(this->Name,_Name,strlen(_Name)*sizeof(char));
}
void Student::setAge( int _age )
{
age = _age;
}
void Student::setStudentID(int _studentID )
{
studentID = _studentID;
}
void Student::setAddress( char* _address )
{
memcpy(address,_address,strlen(_address)*sizeof(char));
}
|
F5를 눌러보자 아마 잘 돌꺼다. ㅋㅋㅋ
다음을 refactoring이다. 고고
- Write a test that fails
- Make the code work
- Eliminate redundancy
[student.h]
#undef UNICODE
#include <stdio.h>
#include <string.h>
#include <assert.h>
class Student
{
private:
char Name[1024];
int age;
int studentID;
char address[1024];
public:
Student();
Student(char* _Name, int _age, int _studentID, char* _address);
const char* getName();
int getAge();
int getStudentID();
const char* getAddress();
void setName(const char* );
void setAge( int _age );
void setStudentID(int _studentID );
void setAddress(const char* _address );
};
|
[student.cpp]
#include "student.h"
Student::Student()
{
::memset(Name, 0,sizeof(char)*1024);
::memset(address, 0,sizeof(char)*1024);
age = 0;
studentID = 0;
}
Student::Student(char* _Name, int _age, int _studentID, char* _address)
{
::memset(Name, 0,sizeof(char)*1024);
::memset(address, 0,sizeof(char)*1024);
memcpy(Name,_Name,strlen(_Name)*sizeof(char));
age = _age;
studentID = _studentID;
memcpy(address,_address,strlen(_address)*sizeof(char));
}
const char* Student::getName()
{
return this->Name;
}
int Student::getAge()
{
return this->age;
}
int Student::getStudentID()
{
return this->studentID;
}
const char* Student::getAddress()
{
return this->address;
}
void Student::setName(const char* _Name)
{
memcpy(this->Name,_Name,strlen(_Name)*sizeof(char));
}
void Student::setAge( int _age )
{
age = _age;
}
void Student::setStudentID(int _studentID )
{
studentID = _studentID;
}
void Student::setAddress( const char* _address )
{
memcpy(address,_address,strlen(_address)*sizeof(char));
}
|
고고 이렇게 하면 refactoring 완료완료