C++ 하루에 조금씩이라도 공부하기
#include <cstring>
#include <iostream>
class MyString
{
char* string_content;
int string_length;
int memory_capacity;
public:
MyString(char c);
MyString(const char* str);
MyString(const MyString& str);
~MyString();
int length() const;
int capacity() const;
void reserve(int size);
void print() const;
void println() const;
char at(int i) const;
int compare(MyString& str);
bool operator==(MyString& str);
};
MyString::MyString(char c)
{
string_content = new char[1];
string_content[0] = c;
memory_capacity = 1;
string_length = 1;
}
MyString::MyString(const char* str)
{
string_length = strlen(str);
memory_capacity = string_length;
string_content = new char[string_length];
for (int i = 0; i != string_length; i++)
{
string_content[i] = string_content[i] = str[i];
}
}
MyString::MyString(const MyString& str)
{
string_length = str.string_length;
string_content = new char[string_length];
for (int i = 0; i != string_length; i++)
{
string_content[i] = str.string_content[i];
}
}
MyString::~MyString()
{
delete[] string_content;
}
int MyString::length() const
{
return string_length;
}
void MyString::print() const
{
for (int i = 0; i != string_length; i++)
{
std::cout << string_content[i];
}
}
void MyString::println() const
{
for (int i = 0; i != string_length; i++)
{
std::cout << string_content[i];
}
std:: cout << std::endl;
}
int MyString::capacity() const
{
return memory_capacity;
}
void MyString::reserve(int size)
{
if (size > memory_capacity)
{
char* prev_string_content = string_content;
string_content = new char[size];
memory_capacity = size;
for (int i = 0; i != string_length; i++)
{
string_content[i] = prev_string_content[i];
}
delete[] prev_string_content;
}
}
char MyString::at(int i) const
{
if (i >= string_length || i < 0)
{
return 0;
}
else
{
return string_content[i];
}
}
int MyString::compare(MyString& str)
{
for (int i = 0; i < std::min(string_length, str.string_length); i++)
{
if (string_content[i] > str.string_content[i])
{
return 1;
}
else if (string_content[i] < str.string_content[i])
{
return -1;
}
}
if (string_length == str.string_length) return 0;
else if (string_length > str.string_length)
return 1;
return -1;
}
bool MyString::operator==(MyString& str)
{
return !compare(str);
}
int main(int argc, char* argv[])
{
MyString str1("a word");
MyString str2("sentence");
MyString str3("sentence");
if (str1 == str2)
std::cout << "str1 와 str2 같다." << std::endl;
else
std::cout << "st1 와 str2 는 다르다." << std::endl;
if (str2 == str3)
std::cout << "str2 와 str3 는 같다." << std::endl;
else
std::cout << "st2 와 str3 는 다르다" << std::endl;
}
실행결과
st1 와 str2 는 다르다.
str2 와 str3 는 같다.
MyString 클래스가 너무 커져서 복소수 클래스 만들어서 진행
C++ 표준에 std::complex 가 이미 있어서 굳이 만들필요는 없는데 교육용으로 만듬
어떤 실수 a에 대해서

를 만족하는 x를 a의 제곱근이라고 한다.
실수의 제곱은 언제나 양수라서 음수의 제곱근은 실수로 표현할 수 없다.
그래서

-1의 제곱근을 i로 표기하는데 이걸 허수라고한다.
class Complex
{
private:
double real, img;
public:
Complex(double real, double img) : real(real), img(img) {} // 초기화 리스트
}
복소수는 항상 실수부와 허수부로 나눠진다.
Complex 클래스도 실수부(real) 허수부(img)를 나타내는 변수가 있다.
class Complex
{
private:
double real, img;
public:
Complex(double real, double img) : real(real), img(img) {}
Complex plus(const Complex& c);
Complex minus(const Complex& c);
Complex times(const Complex& c);
Complex divide(const Complex& c);
}
이렇게 선언한다면 만약 int형 변수라면
a + b / c + d;
간단하게 쓸 수 있었던 명령을
a.plus(b.divide(c)).plus(d);
이렇게 써야하는데 너무 복잡해 보이지 않는가?
이걸 연산자 오버로딩을 이용해서
로 바꿔주면 개발자가 a + b/ c +d 라고 써도
a.operator+(b.operator/(c)).operator+(d);로 알아서 변환해서 처리하기 때문에
속도나 다른 부분에서의 차이없이 뛰어난 가독성으로 편리함을 얻을 수 있다.
class Complex
{
private:
double real, img;
public:
Complex(double real, double img) : real(real), img(img) {}
Complex(const Complex& c)
{
real = c.real;
img = c.img;
}
Complex plus(const Complex& c);
Complex minus(const Complex& c);
Complex times(const Complex& c);
Complex divide(const Complex& c);
void println()
{
std::cout << "(" << real << " , " << img << " ) " << std::endl;
}
}
// +
Complex Complex::operator+(const Complex& c) const
{
Complex temp(real + c.real, img + c.img);
return temp;
}
// -
Complex Complex::operator-(const Complex& c) const
{
Complex temp(real - c.real, img - c.img);
return temp;
}
// *
Complex Complex::operator*(const Complex& c) const
{
Complex temp(real * c.real - img * c.img, real * c.img + img * c.real);
return temp;
}
// /
Complex Complex::operator/(const Complex& c) const
{
Complex temp(
(real * c.real + img * c.img) / (c.real * c.real + c.img * c.img),
(img * c.real + real * c.img) / (c.real * c.real + c.img * c.img)
);
return temp;
}
int main()
{
Complex a(1.0, 2.0);
Complex b(3.0, -2.0);
Complex c = a * b;
c.println();
}
실행결과
( 7 , 4 )