Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- 코딩독학
- 풀스택프레임워크
- 자바정리
- backend
- 빅데이터
- 개인프로젝트
- 코테
- Python
- 코딩테스트
- 모의 주식
- 프로그래머스
- java
- 프로그래밍
- SNS
- 자바독학
- 백준
- Reflex
- 개발
- 프로그램
- 사물인터넷
- 코스닥
- frontend
- Project
- 코딩
- 이웃한 칸
- 개발자
- java정리
- 주가예측
- C++
- 자바
Archives
- Today
- Total
Creative Code
main.cpp(string2) 본문
※main.cpp파일
#include <iostream>
#include "string.h"
int main() {
String s1;
String s2 = "hello, world"; // String s2("hello,world");
//String s2 = 0; // NULL
String s3 = s2; // String s3(s2);
s1 = s2;
String s4 = " IoT";
s1 = s2 + s4;
s1 = "hello, "; //String tmp("hello, ");
if (s1 == s2) std::cout << "s1 and s2 are equal" << std::endl;
else std::cout << "s1 and s2 are not equal" << std::endl;
s4+= s4;
s1 != s2;
std::cout << "s1 : " << s1.c_str() << std::endl;
std::cout << "s2 : " << s2.c_str() << std::endl;
std::cout << "s2 len : " << s2.size() << std::endl;
std::cout << "s3 : " << s3 <<std::endl;
const String s5 = "hello";
//s5 = "world";
std::cout << "s5 len : " << s5.size() << std::endl;
return 0;
}
※string.h파일
#ifndef STRING_H
#define STRING_H
#include <iostream>
class String {
friend std::ostream& operator<<(std::ostream& out, const String& rhs);
private:
char *str;
int len;
void init_str(const char *str); // tool function, helper function(다른 멤버함수들에 의해서만 사용되는 함수)
public:
String(const char *str=0);
String(const String& str);
~String();
String& operator=(const String& rhs);
String& operator=(const char* str);
String& operator+=(const String& rhs);
bool operator!=(const String& rhs) const;
bool operator==(const String& rhs) const; // const member function (객체에 대해서 read only)
const String operator+(const String& rhs) const;
const char* c_str() const;
const int size() const;
};
#endif
※string.cpp파일
#include "string.h"
#include <cassert>
#include <cstring>
std::ostream& operator<<(std::ostream& out, const String& rhs) {
out << rhs.str;
return out;
}
void String::init_str(const char* str) {
if (str) {
this->str = new char[strlen(str)+1];
assert(this->str);
strcpy(this->str,str);
this->len = strlen(str);
} else {
this->str = new char[1];
assert(this->str);
this->str[0] = '\0';
this->len = 0;
}
}
String::String(const char *str) {
this->init_str(str);
}
String::String(const String& rhs) {
this->init_str(rhs.str);
}
String::~String() {
delete[] this->str;
}
const char *String::c_str() const{
return this->str;
}
const int String::size() const {
return this->len;
}
String& String::operator=(const String& rhs) {
if (this != &rhs) { //self-assignment test!
delete[] this->str;
this->init_str(rhs.str);
}
return *this;
}
String& String::operator=(const char* str) {
if (this->str != str) {
delete[] this->str;
this->init_str(str);
}
return *this;
}
bool String::operator==(const String& rhs) const{
return (strcmp(this->str,rhs.str)==0);
}
const String String::operator+(const String& rhs) const {
char *buf = new char[this->len + rhs.len+1];
assert(buf);
strcpy(buf,this->str);
strcat(buf,rhs.str);
String result(buf);
delete[] buf;
return result;
}
String& String::operator+=(const String& rhs) {
*this = *this + rhs;
return *this;
}
bool String::operator!=(const String& rhs) const{
return !this->operator==(rhs);
}
'C++ Programming' 카테고리의 다른 글
main.cpp(employee클래스) (0) | 2023.09.01 |
---|---|
main.cpp(컴파일러가 자동생성하는 함수) (0) | 2023.09.01 |
main.cpp(string) (0) | 2023.08.31 |
main.cpp(rational클래스 여러 연산자) (0) | 2023.08.31 |
main.cpp(complex클래스 여러 연산자) (0) | 2023.08.31 |