|
|
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #ifndef _MYLIB_H_ #define _MYLIB_H_ #include<iostream> #include<string> #include<ctime> #include<cstdlib> using namespace std; namespace wjddydrb {//1 int add(int*, int, int*); int add(int*, int); } #endif // !_MYLIB_H_ #include"mylib.h" using namespace std; namespace wjddydrb {//1 int add(int* a, int b) { int s = 0; for (int i = 0; i < 5; i++) { s += a[i]; } return s; } int add(int* a, int b, int* c) { return add(a, b) + add(c, b); } } #include"mylib.h" using namespace std; int main() {//1 int a[] = { 1,2,3,4,5 }; int b[] = { 6,7,8,9,10 }; int c = add(a, 5); int d = add(a, 5, b); cout << c << endl; cout << d << endl; } | cs |
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | #ifndef _MYLIB_H_ #define _MYLIB_H_ #include<iostream> #include<string> #include<ctime> #include<cstdlib> using namespace std; namespace wjddydrb {//2 class person { int id; double weight; string name; public: void show(); person(int id = 1, string name = "grace", double weight = 20.5); }; } #endif // !_MYLIB_H_ #include"mylib.h" using namespace std; namespace wjddydrb {//2 person::person(int id , string name, double weight) { this->id = id; this->name = name; this->weight = weight; } void person::show() { cout << id << ' ' << weight << ' ' << name << endl; } } #include"mylib.h" using namespace std; int main() {//2 person grace, ashley(2, "ashley"), helen(3, "helen", 32.5); grace.show(); ashley.show(); helen.show(); } | cs |
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 32 33 34 | #ifndef _MYLIB_H_ #define _MYLIB_H_ #include<iostream> #include<string> #include<ctime> #include<cstdlib> using namespace std; namespace wjddydrb {//3 int big(int a, int b, int c= 0 ); } #endif // !_MYLIB_H_ #include"mylib.h" using namespace std; namespace wjddydrb {//3 int big(int a, int b, int c) { b = b > c ? b : c; int big = a > b ? a : b; return 100>big ? big : 100; } } #include"mylib.h" using namespace std; int main() {//3 int x = big(3, 5); int y = big(300, 60); int z = big(30, 60, 50); cout << x << ' ' << y << ' ' << z << endl; } | cs |
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 32 33 34 35 36 37 38 39 40 41 42 43 44 | #ifndef _MYLIB_H_ #define _MYLIB_H_ #include<iostream> #include<string> #include<ctime> #include<cstdlib> using namespace std; namespace wjddydrb { class myvector { int* mem; int size; public: myvector(int n=100 , int val=0); ~myvector(); }; } #endif // !_MYLIB_H_ #include"mylib.h" using namespace std; namespace wjddydrb {//4 myvector::myvector(int n, int val) { mem = new int[n]; size = n; for (int i = 0; i < size; i++)mem[i] = val; } myvector::~myvector() { delete[]mem; } } #include"mylib.h" using namespace std; int main() {//4 myvector a, b(200, 10); } | cs |
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | #ifndef _MYLIB_H_ #define _MYLIB_H_ #include<iostream> #include<string> #include<ctime> #include<cstdlib> using namespace std; namespace wjddydrb {//5 class AU { public: static void inttodouble(int* a, double* b, int size); static void doubletoint(double* a, int* b, int size); }; } #endif // !_MYLIB_H_ #include"mylib.h" using namespace std; namespace wjddydrb {//5 void AU:: inttodouble(int* a, double* b, int size) { for (int i = 0; i < size; i++) { b[i] = a[i]; } } void AU:: doubletoint(double* a, int* b, int size) { for (int i = 0; i < size; i++) { b[i] = a[i]; } } } #include"mylib.h" using namespace std; int main() {//5 int x[] = { 1,2,3,4,5 }; double y[5]; double z[5] = { 9.9,8.8,7.7,6.6,5.5 }; AU::inttodouble(x, y, 5); for (int i = 0; i < 5; i++) cout << y[i] << ' '; cout << endl; AU::doubletoint(z, x, 5); for (int i = 0; i < 5; i++) cout << x[i] << ' '; cout << endl; } | cs |
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | #ifndef _MYLIB_H_ #define _MYLIB_H_ #include<iostream> #include<string> #include<ctime> #include<cstdlib> using namespace std; namespace wjddydrb {//6 class arryutility2 { public: static int* concat(int*, int*, int); static int* remove(int*, int*, int, int& ); }; } #endif // !_MYLIB_H_ #include"mylib.h" using namespace std; namespace wjddydrb {//6 int* arryutility2::concat(int* a, int* b, int c) { int* p = new int[c*2]; for (int i = 0; i < c*2; i++) { if (i >= 5) p[i] = b[i - 5]; else p[i] = a[i]; } return p; } int* arryutility2::remove(int* a, int* b, int c, int& d) { int count = 0; for (int i = 0; i < c; i++) { for (int j = 0; j < c; j++) { if (a[i] == b[j]) { count++; } } } int n = 0; d = c - count; count = 0; int* p = new int[d]; for (int i = 0; i < c; i++) { for (int j = 0; j < c; j++) { if (a[j] == b[i]) { count++; } } if (count == 0) p[n++] = a[i]; count = 0; } return p; } } #include"mylib.h" using namespace std; int main() {//6 int x[5], y[5],size; cout << "정수 5개를 입력하라 배열 x에 삽입한다" << endl; for (int i = 0; i < 5; i++) cin >> x[i]; cout << "정수 5개를 입력하라 배열 y에 삽입한다" << endl; for (int i = 0; i < 5; i++) cin >> y[i]; int* a = arryutility2::concat(x, y, 5); cout << "합친 배열을 출력한다" << endl; for (int i = 0; i < 10; i++)cout << a[i] << ' '; cout << endl; a = arryutility2::remove(x, y, 5, size); cout << "배열 x[]에서 y[]를 뺀 결과를 출력한다. 개수는 " << size << endl; for (int i = 0; i < size; i++) cout << a[i] << ' '; } | cs |
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | #ifndef _MYLIB_H_ #define _MYLIB_H_ #include<iostream> #include<string> #include<ctime> #include<cstdlib> using namespace std; namespace wjddydrb {//7 class random { public: static void seed(); static int nextint(int min = 0, int max = 32767); static char nextalphabet(); static double nextdouble(); }; } #endif // !_MYLIB_H_ #include"mylib.h" using namespace std; namespace wjddydrb {//7 void random::seed() { srand(time(0)); } int random::nextint(int min, int max) { return rand() % max > min? rand() % max : rand() % max +min; } char random::nextalphabet() { return (char)(rand() % 26 + 'a'); } double random::nextdouble() { double d = rand(); while (d > 1) { d *= 0.1; } return d; } } #include"mylib.h" using namespace std; int main() {//7번 random::seed(); cout << "1부터 100까지 랜덤한 정수를 10개 출력합니다" << endl; for (int i = 0; i < 10; i++) { cout<<random::nextint(1, 100)<<' '; } cout << endl; cout << "알파벳을 랜덤하게 10개 출력합니다" << endl; for (int i = 0; i < 10; i++) { cout << random::nextalphabet()<< ' '; } cout << endl; cout << "랜덤한 실수를 10개 출력합니다" << endl; for (int i = 0; i < 10; i++) { cout << random::nextdouble() << ' '; } cout << endl; } | cs |
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | #ifndef _MYLIB_H_ #define _MYLIB_H_ #include<iostream> #include<string> #include<ctime> #include<cstdlib> using namespace std; namespace wjddydrb {//8 class Trace { static string s[10][2]; static int n; public: static void put(string, string); static void print(string f = "모든"); }; void f(); } #endif // !_MYLIB_H_ #include"mylib.h" using namespace std; namespace wjddydrb {//8 int Trace::n = 0; string Trace::s[10][2]; void Trace::put(string s1, string s2) { s[n][0] = s1; s[n++][1] = s2; } void Trace::print(string f) { cout << "-----" << f << " 정보를 출력합니다----" << endl; for (int i = 0; i < n; i++) { if (s[i][0] == f) { cout << s[i][0] << ":" << s[i][1]; cout << endl; } else if (f == "모든") { cout << s[i][0] << ":" << s[i][1]; cout << endl; } } cout << endl; } void f() { int a, b, c; cout << "두 개의 정수를 입력하세요>>"; cin >> a >> b; Trace::put("f()", "정수를 입력 받았음"); c = a + b; Trace::put("f()", "합 계산"); cout << "합은 " << c << endl; } } #include"mylib.h" using namespace std; int main() {//8 Trace::put("main()", "프로그램 시작합니다"); f(); Trace::put("main()", "종료"); Trace::print("f()"); Trace::print(); return 0; } | cs |
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | #ifndef _MYLIB_H_ #define _MYLIB_H_ #include<iostream> #include<string> #include<ctime> #include<cstdlib> using namespace std; namespace wjddydrb {//9 class board { private: static string s; static int n; public: static void add(string); static void print(); }; } #endif // !_MYLIB_H_ #include"mylib.h" using namespace std; namespace wjddydrb {//9 int board::n = 0; string board::s = "*******게시판입니다********\n"; void board::add(string s2) { s += to_string(n++) + " : " + s2 + "\n"; } void board::print() { cout << s; } } #include"mylib.h" using namespace std; int main() {//9 board::add("중간고사는 갑독 없는 자율시험입니다"); board::add("코딩 라운지 많이 이용해주세요"); board::print(); board::add("진소린 학생이 경신대회 입상하였습니다 축하해주세요"); board::print(); } | cs |
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 32 33 34 35 36 37 38 39 40 41 42 43 44 | #ifndef _MYLIB_H_ #define _MYLIB_H_ #include<iostream> #include<string> #include<ctime> #include<cstdlib> using namespace std; namespace wjddydrb { class book { string title; int price, page; public: book(string , int ,int); friend bool operator!(book); }; bool operator!(book); } #endif // !_MYLIB_H_ #include"mylib.h" using namespace std; namespace wjddydrb {//3 book::book(string s, int a, int b) { title = s; price = a, page = b; } bool operator!(book a) { return a.price == 0 ? true : false; } } #include"mylib.h" using namespace std; int main() {//3 book book("벼룩시장", 0, 50); if (!book)cout << "공짜다" << endl; } | cs |
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | #ifndef _MYLIB_H_ #define _MYLIB_H_ #include<iostream> #include<string> #include<ctime> #include<cstdlib> using namespace std; namespace wjddydrb {//4 class book { string title; int price, page; public: string gettitle(); book(string, int, int); friend bool operator< ( string, book); }; } #endif // !_MYLIB_H_ #include"mylib.h" using namespace std; namespace wjddydrb {//4 string book::gettitle() { return title; } book::book(string s, int a, int b) { title = s; price = a, page = b; } bool operator< (string a, book b) { return a < b.title ? true : false; } } #include"mylib.h" using namespace std; int main() {//4 book a("청춘", 20000, 3); string b; cout << "책이름을 입력하시오>>"; getline(cin, b); if (b < a)cout << a.gettitle() << "이 " << b << "보다 뒤에 있구나" << endl; } | cs |
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | #ifndef _MYLIB_H_ #define _MYLIB_H_ #include<iostream> #include<string> #include<ctime> #include<cstdlib> using namespace std; namespace wjddydrb {//5 class color { int r, g, b; public: color(int r = 0, int g = 0, int b = 0); void show(); friend color operator+(color, color); friend bool operator==(color, color); }; } #endif // !_MYLIB_H_ #include"mylib.h" using namespace std; namespace wjddydrb {//5 color::color(int r, int g, int b) { this->r = r; this->g = g; this->b = b; } color operator+(color a, color b) { color c; c.r = a.r + b.r; c.b = a.b + b.b; c.g = a.g + b.g; return c; } bool operator==(color a, color b) { if (a.r == b.r && a.g == b.g && a.b == b.b) return true; else return false; } void color::show() { cout << r << ' ' << g << ' ' << b << endl; } } #include"mylib.h" using namespace std; int main() {//5 color red(255, 0, 0), blue(0, 0, 255), c; c = red + blue; c.show(); color fuchia(255, 0, 255); if (c == fuchia) cout << "보라색임" << endl; else cout << "보라색 아님"; } | cs |
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | #ifndef _MYLIB_H_ #define _MYLIB_H_ #include<iostream> #include<string> #include<ctime> #include<cstdlib> using namespace std; namespace wjddydrb {//6 class matrix { int m[4]; public: matrix(int a= 0, int b = 0, int c = 0, int d = 0); void show(); friend matrix operator+(matrix, matrix); friend void operator+=(matrix&, matrix); friend bool operator==(matrix, matrix); }; } #endif // !_MYLIB_H_ #include"mylib.h" using namespace std; namespace wjddydrb {//6 void matrix::show() { cout << "matrix = { "; for (int i = 0; i < 4; i++) cout << m[i] << ' '; cout<<"}" << endl; } matrix::matrix(int a, int b, int c, int d) { m[0] = a; m[1] = b; m[2] = c; m[3] = d; } matrix operator+(matrix a, matrix b) { matrix c; for (int i = 0; i < 4; i++) { c.m[i] = a.m[i] + b.m[i]; } return c; } void operator+=(matrix& a, matrix b) { for (int i = 0; i < 4; i++) { a.m[i] += b.m[i]; } } bool operator==(matrix a, matrix b) { int n = 0; for (int i = 0; i < 4; i++) { if (a.m[i] == b.m[i]) n++; } return n == 4 ? true : false; } } #include"mylib.h" using namespace std; int main() {//6 matrix a(1, 2, 3, 4), b(2, 3, 4, 5), c; c = a + b; a += b; a.show(); b.show(); c.show(); if (a == c) cout << "a and c are same" << endl; } | cs |
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | #ifndef _MYLIB_H_ #define _MYLIB_H_ #include<iostream> #include<string> #include<ctime> #include<cstdlib> using namespace std; namespace wjddydrb {//7 class matrix { int m[4]; public: matrix(int a = 0, int b = 0, int c = 0, int d = 0); void show(); friend void operator>>(matrix, int*); friend void operator<<(matrix&, int*); }; } #endif // !_MYLIB_H_ #include"mylib.h" using namespace std; namespace wjddydrb {//7 void matrix::show() { cout << "matrix = {" << m[0] << m[1] << m[2] << m[3] << "}" << endl; } void operator>>(matrix a, int* b) { for (int i = 0; i < 4; i++) { b[i] = a.m[i]; } } void operator<<(matrix& a, int* b) { for (int i = 0; i < 4; i++) { a.m[i] = b[i]; } } matrix::matrix(int a, int b, int c, int d) { m[0] = a; m[1] = b; m[2] = c; m[3] = d; } } #include"mylib.h" using namespace std; int main() {//7 matrix a(4, 3, 2, 1), b; int x[4], y[4] = { 1,2,3,4 }; a >> x; b << y; for (int i = 0; i < 4; i++) cout << x[i] << ' '; cout << endl; b.show(); } | cs |
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | #ifndef _MYLIB_H_ #define _MYLIB_H_ #include<iostream> #include<string> #include<ctime> #include<cstdlib> using namespace std; namespace wjddydrb {//8 class circle { int r; public: circle(int r = 0); void show(); friend void operator++(circle&); friend circle& operator++(circle&, int n); }; } #endif // !_MYLIB_H_ #include"mylib.h" using namespace std; namespace wjddydrb {//8 circle::circle(int r) { this->r = r; } void circle::show() { cout << "radius = " << r << "인 원" << endl; } void operator++(circle& a) { a.r++; } circle& operator++(circle& a, int n) { circle t = a; a.r++; return t; } } #include"mylib.h" using namespace std; int main() {//8 circle a(5), b(4); ++a; b = a++; a.show(); b.show(); } | cs |
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 32 33 34 35 36 37 38 39 40 41 42 43 | #ifndef _MYLIB_H_ #define _MYLIB_H_ #include<iostream> #include<string> #include<ctime> #include<cstdlib> using namespace std; namespace wjddydrb {//9 class circle { int r; public: circle(int r = 0); void show(); friend circle operator+(int, circle); }; } #endif // !_MYLIB_H_ #include"mylib.h" using namespace std; namespace wjddydrb {//9 circle::circle(int r) { this->r = r; } void circle::show() { cout << "radius = " << r << "인 원" << endl; } circle operator+(int n, circle a) { return a.r += n; } } #include"mylib.h" using namespace std; int main() {//9 circle a(5), b(4); b = 1 + a; a.show(); b.show(); } | cs |
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | #ifndef _MYLIB_H_ #define _MYLIB_H_ #include<iostream> #include<string> #include<ctime> #include<cstdlib> using namespace std; namespace wjddydrb {//10 class statistics { int size =0; int* p; public: friend bool operator!(statistics); friend void operator~(statistics); friend statistics& operator<<(statistics&, int); friend void operator>>(statistics, int&); }; } #endif // !_MYLIB_H_ #include"mylib.h" using namespace std; namespace wjddydrb {//10 bool operator!(statistics a) { return a.size == 0 ? true : false; } statistics& operator<<(statistics& a, int n) { int* ary = a.p; a.p = new int[++a.size]; for (int i = 0; i < a.size - 1; i++) { a.p[i] = ary[i]; } a.p[a.size - 1] = n; delete[]ary; return a; } void operator~(statistics a) { for (int i = 0; i < a.size; i++) { cout << a.p[i] << ' '; } cout << endl; } void operator>>(statistics a, int& b) { b= 0; for (int i = 0; i < a.size; i++) { b += a.p[i]; } b /= a.size; } } #include"mylib.h" using namespace std; using namespace wjddydrb; int main() {//10 statistics stat; if (!stat) cout << "현재 데이타가 없습니다" << endl; int x[5]; cout << "정수 5개를 입력하라>>"; for (int i = 0; i < 5; i++){ cin >> x[i]; } for (int i = 0; i < 5; i++) { stat << x[i]; } stat << 100<<200; ~stat; int avg; stat >> avg; cout << "avg = " << avg << endl; } | cs |
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | #ifndef _MYLIB_H_ #define _MYLIB_H_ #include<iostream> #include<string> #include<ctime> #include<cstdlib> using namespace std; namespace wjddydrb {//11 class stack { int x[100]; int top = 0; public: bool operator!(); stack& operator<<(int); int operator>>(int&); }; } #endif // !_MYLIB_H_ #include"mylib.h" using namespace std; namespace wjddydrb {//11 bool stack::operator!() { return top == 0 ? true: false; } stack& stack::operator<<(int a) { x[top++] = a; return *this; } int stack::operator>>(int& a) { a = x[--top]; return a; } } #include"mylib.h" using namespace std; using namespace wjddydrb; int main() {//11 stack stack; stack << 3 << 5 << 10; while (true) { if (!stack) break; int x; stack >> x; cout << x << ' '; } cout << endl; } | cs |
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | #ifndef _MYLIB_H_ #define _MYLIB_H_ #include<iostream> #include<string> #include<ctime> #include<cstdlib> using namespace std; namespace wjddydrb {//12 class sortedarray { int size; int* p; void sort(); public: sortedarray() { p = NULL; size = 0; } sortedarray(int* p, int size); sortedarray(sortedarray&); ~sortedarray(); sortedarray operator+(sortedarray&); sortedarray& operator= (const sortedarray&); void show(); }; } #endif // !_MYLIB_H_ #include"mylib.h" using namespace std; namespace wjddydrb {//12 void sortedarray::show() { sort(); cout << "배열 출력 :"; for (int i = 0; i < size; i++) { cout << p[i]<<' '; } cout << endl; } void sortedarray::sort() { for (int i = 0; i < (this->size) - 1; i++){ for (int j = 0; j < (this->size) - 1 - i; j++){ if (this->p[j] > this->p[j + 1]) { int tmp = this->p[j]; this->p[j] = this->p[j + 1]; this->p[j + 1] = tmp; } } } } sortedarray::sortedarray(int* p, int size) { this->p = p; this->size = size; this->p = new int[size]; for (int i = 0; i < size; i++) { this->p[i] = p[i]; } } sortedarray::sortedarray(sortedarray& a) { this->size = a.size; this->p = new int[size]; for (int i = 0; i < size; i++) { this->p[i] = a.p[i]; } } sortedarray::~sortedarray() { delete[]p; } sortedarray sortedarray::operator+(sortedarray& a) { sortedarray ar; ar.p= this->p; ar.size = this->size + a.size; ar.p = new int[ar.size]; for (int i = 0; i < size; i++) { ar.p[i] = this->p[i]; } for (int i = size ; i < ar.size; i++) { ar.p[i] = a.p[i-this->size]; } return ar; } sortedarray& sortedarray::operator=(const sortedarray& a) { this->size = a.size; this->p = new int[a.size]; for (int i = 0; i < a.size; i++) { this->p[i] = a.p[i]; } return *this; } } #include"mylib.h" using namespace std; using namespace wjddydrb; int main() {//12 int n[] = { 2,20,6 }; int m[] = { 10,7,8,30 }; sortedarray a(n, 3), b(m, 4), c; c = a + b; a.show(); b.show(); c.show(); } | cs |
6장
1. 디폴트 매개변수를 선언하는 위치는 어디인가?
함수 선언부에 매개변수
2. 함수의 중복이 왜 필요한가?
함수의 이름과 기능은 비슷하나 매개변수의 자료형이나 개수가 다른 경우가 있어서
3. static 키워드를 붙이는 위치는 어디인가?
자료형 앞
4. static 멤버변수는 왜 외부에서 다시 선언해야 하는가?
클래스 내에서 선언하게 되면 클래스 안에서만 접근이 가능하기 때문에 외부에서 사용할 때는 다시 선언해줘야 한다
5. 그리고 이때는 왜 static키워드를 붙이지 않는가?
붙이게 되면 선언된 소스파일 안에서만 접근이 가능하다
7장
1. 연산자 함수의 장점은 무엇인가?
프로그래머가 원하는 연산을 구현 가능하다
2. 연산자 함수를 정의하는 2가지 방법을 설명하라.
내부에 멤버함수로 구현하는 방법과 외부에서 구현하고 프렌드 함수로 클래스 안에 넣는 방법이 있다
3. 연산자함수를 프렌드 함수로만 선언해야 하는 경우를 설명하라.
피연산자가 같은 객체가 아닌 경우
4. 연산자함수 중에서 반환형을 참조형으로 선언해야 하는 경우는 어떤 경우인가?
전위연산자 같은 경우 자기 자신을 연산 후 자신을 리턴해하기 떄문에 반환형이 참조형이다
5. 전위++, += 연산자 등은 왜 참조를 리턴해야 하는가?
정의가 변수로 정의되어 있기에 반환형을 참조형으로 하여 계속적인 연산이 가능하게 해야한다.
6. a++++, a---- 는 문법오류입니다. 이유를 설명하시오.
반환값이 상수임으로 불가능하다.
7. ++++a, ----a 는 정상입니다. 이유를 설명하시오
반환값이 변수임으로 가능하다

첫댓글 1. 문제 번호는 적을것
2. 불필요한 코드는 제거할것
3. 들여쓰기 주의할것
4. 동적할당 했으면 해제도 반드시 코드에서 해줄것