본문 바로가기

언어/C++

책 제목 사전 순으로 비교하기 #include #include using namespace std; class Book { string name; int price; int page; public: Book(string name, int price ,int page){ this->name=name; this->price=price; this->page=page; } string getTitle(){ return name; } bool operator b.getTitle().substr(0,1) ){ return true; }else { return false; } } }; int main(){ Book a ("청춘",20000,300); string b; cout 더보기
프렌드 함수 프랜드함수: 클래스 내에 friend 키워드로 선언된 외부 함수 두 Rect 객체를 비교하는 bool equals (Rect r ,Rect s ) 를 Rect 클래스에 프랜드 함수로 작성하기 #include using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ class Rect{ int width; int height; public: Rect(int width, int height){ this->width=width; this->height=height; } friend bool equals(Rect r, Rect s); }; bool eq.. 더보기
4장 예제- 면적 조건에 따른 원의 개수 구하기 #include using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ class Circle{ private : int radius; public: Circle(); ~Circle(); void setRadius(int r){ radius=r; } double getArea() { return 3.14*radius*radius; } }; Circle:: Circle(){ radius=1; } int main(int argc, char** argv) { int num; int radius; int count=0; cout > num; Circl.. 더보기
6장 6번 - 정수 배열 합치기, 빼기 #include using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ class ArrayUtility2{ public: static int*concat(int s1[], int s2[], int size){ int *s3= new int [size]; int j=0; for(int i=0; i 더보기
5장 예제- 복사 생성자 오빠 동생 복사 -깊은 복사 #include #include using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ class Person{ private : char *name; int id; public: Person(int id, char*name); Person(Person& person); // 복사 생성자 ~Person(); void changeName(char*name); void show() {cout id=person.id; int len=strlen(person.name); this->name=new char[len+1]; strcpy(this->name.. 더보기
5장 예제- 복사 생성자 오빠 동생 복사 - 얕은 복사 #include #include using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ class Person{ private : char *name; int id; public: Person(int id, char*name); ~Person(); void changeName(char*name); void show() {cout strlen(this->name)) { return; } strcpy(this->name, name); } int main(){ Person brother(1,"돌딸기"); Person sister(brother); c.. 더보기
5장 7번 - 덧셈 계산기 만들기 #include using namespace std; class Accumulator { int value; public : Accumulator(int value){ this->value = value; } Accumulator& add(int n){ value += n; return *this; } int get(){ return value; } }; int main() { Accumulator acc(10); acc.add(5).add(6).add(7); cout 더보기
5장 5번 - 스택에 데이터 삽입,삭제 #include using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ class MyIntStack{ int *p; int max=10; int top=0; int size; public: MyIntStack(){ p=new int[max]; } ~MyIntStack(){ delete[] p; } bool push(int n); bool pop(int &n); }; bool MyIntStack:: push(int n){ if(top==max){ return false; } else{ p[top]=n; top++; return true; } }.. 더보기