10 include iostream list 10 1 class Rectangle

  • Slides: 47
Download presentation

10일째 고급 함수들 #include <iostream> list 10. 1 class Rectangle { public: Rectangle(int width,

10일째 고급 함수들 #include <iostream> list 10. 1 class Rectangle { public: Rectangle(int width, int height); ~Rectangle(){} // overloading void Draw. Shape() const; void Draw. Shape(int a. Width, int a. Height) const; private: int its. Width; int its. Height; }; Rectangle: : Rectangle(int width, int height) { its. Width = width; its. Height = height; } 3

10일째 고급 함수들 void Rectangle: : Draw. Shape() const { Draw. Shape( its. Width,

10일째 고급 함수들 void Rectangle: : Draw. Shape() const { Draw. Shape( its. Width, its. Height); } void Rectangle: : Draw. Shape(int width, int height) const { for (int i = 0; i<height; i++) { for (int j = 0; j< width; j++) { std: : cout << "*"; } std: : cout << "n"; } } int main() { Rectangle the. Rect(30, 5); std: : cout << "Draw. Shape(): n"; the. Rect. Draw. Shape(); std: : cout << "n. Draw. Shape(40, 2): n"; the. Rect. Draw. Shape(40, 2); return 0; } 4

10일째 고급 함수들 #include <iostream> list 10. 2 using namespace std; class Rectangle {

10일째 고급 함수들 #include <iostream> list 10. 2 using namespace std; class Rectangle { public: // constructors Rectangle(int width, int height); ~Rectangle(){} void Draw. Shape(int a. Width, int a. Height, bool Use. Current. Vals = false) const; private: int its. Width; int its. Height; }; Rectangle: : Rectangle(int width, int height) { its. Width = width; its. Height = height; } 6

10일째 고급 함수들 void Rectangle: : Draw. Shape( int width, int height, bool Use.

10일째 고급 함수들 void Rectangle: : Draw. Shape( int width, int height, bool Use. Current. Value) const { int print. Width; int print. Height; if (Use. Current. Value == true) { print. Width = its. Width; print. Height = its. Height; } else { print. Width = width; print. Height = height; } for (int i = 0; i<print. Height; i++) { for (int j = 0; j< print. Width; j++) { cout << "*"; } cout << "n"; } } 7

10일째 고급 함수들 int main() { // initialize a rectangle to 30, 5 Rectangle

10일째 고급 함수들 int main() { // initialize a rectangle to 30, 5 Rectangle the. Rect(30, 5); cout << "Draw. Shape(0, 0, true). . . n"; the. Rect. Draw. Shape(0, 0, true); cout <<"Draw. Shape(40, 2). . . n"; the. Rect. Draw. Shape(40, 2); return 0; } 8

10일째 고급 함수들 #include <iostream> using namespace std; list 10. 3 class Rectangle {

10일째 고급 함수들 #include <iostream> using namespace std; list 10. 3 class Rectangle { public: Rectangle(); Rectangle(int width, int length); ~Rectangle() {} int Get. Width() const { return its. Width; } int Get. Length() const { return its. Length; } private: int its. Width; int its. Length; }; Rectangle: : Rectangle() { its. Width = 5; its. Length = 10; } Rectangle: : Rectangle (int width, int length) { its. Width = width; its. Length = length; } 10

10일째 고급 함수들 int main() { Rectangle Rect 1; cout << "Rect 1 width:

10일째 고급 함수들 int main() { Rectangle Rect 1; cout << "Rect 1 width: " << Rect 1. Get. Width() << endl; cout << "Rect 1 length: " << Rect 1. Get. Length() << endl; int a. Width, a. Length; cout << "Enter a width: "; cin >> a. Width; cout << "n. Enter a length: "; cin >> a. Length; Rectangle Rect 2(a. Width, a. Length); cout << "n. Rect 2 width: " << Rect 2. Get. Width() << endl; cout << "Rect 2 length: " << Rect 2. Get. Length() << endl; return 0; } 11

10일째 고급 함수들 객체 초기화 l 초기화 단계에서 멤버변수를 초기화 할 수 있음 :

10일째 고급 함수들 객체 초기화 l 초기화 단계에서 멤버변수를 초기화 할 수 있음 : 멤버변수(초기값), … , 멤버변수(초기값) 예) Rectangle: : Rectangle(): its. Width(5), its. Length(10) { } Rectangle: : Rectangle (int width, int length): its. Width(width), its. Length(length) { } 12

10일째 고급 함수들 list 10. 5 #include <iostream> using namespace std; class CAT {

10일째 고급 함수들 list 10. 5 #include <iostream> using namespace std; class CAT { public: CAT(); //default constructor CAT (const CAT &); // copy constructor ~CAT(); // destructor int Get. Age() const { return *its. Age; } int Get. Weight() const { return *its. Weight; } void Set. Age(int age) { *its. Age = age; } private: int *its. Age; int *its. Weight; }; 15

10일째 고급 함수들 CAT: : CAT() { its. Age = new int; its. Weight

10일째 고급 함수들 CAT: : CAT() { its. Age = new int; its. Weight = new int; *its. Age = 5; *its. Weight = 9; } CAT: : CAT(const CAT & rhs) { its. Age = new int; its. Weight = new int; *its. Age = rhs. Get. Age(); *its. Weight = *(rhs. its. Weight); } CAT: : ~CAT() { delete its. Age; its. Age = 0; delete its. Weight; its. Weight = 0; } 16

10일째 고급 함수들 int main() { CAT frisky; cout << "frisky's age: " <<

10일째 고급 함수들 int main() { CAT frisky; cout << "frisky's age: " << frisky. Get. Age() << endl; cout << "Setting frisky to 6. . . n"; frisky. Set. Age(6); cout << "Creating boots from friskyn"; CAT boots(frisky); cout << "frisky's age: " << frisky. Get. Age() << endl; cout << "boots' age: " << boots. Get. Age() << endl; cout << "setting frisky to 7. . . n"; frisky. Set. Age(7); cout << "frisky's age: " << frisky. Get. Age() << endl; cout << "boot's age: " << boots. Get. Age() << endl; return 0; } 17

10일째 고급 함수들 #include <iostream> using namespace std; list 10. 6 class Counter {

10일째 고급 함수들 #include <iostream> using namespace std; list 10. 6 class Counter { public: Counter(); ~Counter(){} int Get. Its. Val()const { return its. Val; } void Set. Its. Val(int x) {its. Val = x; } private: int its. Val; }; Counter: : Counter(): its. Val(0) {} int main() { Counter i; cout << "The value of i is " << i. Get. Its. Val() << endl; return 0; } 19

10일째 고급 함수들 #include <iostream> list 10. 8 using namespace std; class Counter {

10일째 고급 함수들 #include <iostream> list 10. 8 using namespace std; class Counter { public: Counter(); ~Counter(){} int Get. Its. Val()const { return its. Val; } void Set. Its. Val(int x) {its. Val = x; } void Increment() { ++its. Val; } void operator++ () { ++its. Val; } private: int its. Val; }; Counter: : Counter(): its. Val(0) {} int main() { Counter i; cout << "The value of i is " << i. Get. Its. Val() << endl; i. Increment(); cout << "The value of i is " << i. Get. Its. Val() << endl; ++i; cout << "The value of i is " << i. Get. Its. Val() << endl; return 0; } 20

10일째 고급 함수들 #include <iostream> using namespace std; list 10. 9 class Counter {

10일째 고급 함수들 #include <iostream> using namespace std; list 10. 9 class Counter { public: Counter(); ~Counter(){} int Get. Its. Val()const { return its. Val; } void Set. Its. Val(int x) {its. Val = x; } void Increment() { ++its. Val; } Counter operator++ (); private: int its. Val; }; Counter: : Counter(): its. Val(0) {} Counter: : operator++() { ++its. Val; Counter temp; temp. Set. Its. Val(its. Val); return temp; } 22

10일째 고급 함수들 int main() { Counter i; cout << "The value of i

10일째 고급 함수들 int main() { Counter i; cout << "The value of i is " << i. Get. Its. Val() << endl; i. Increment(); cout << "The value of i is " << i. Get. Its. Val() << endl; ++i; cout << "The value of i is " << i. Get. Its. Val() << endl; Counter a = ++i; cout << "The value of a: " << a. Get. Its. Val(); cout << " and i: " << i. Get. Its. Val() << endl; return 0 } 23

10일째 고급 함수들 #include <iostream> using namespace std; class Counter { public: Counter(); Counter(int

10일째 고급 함수들 #include <iostream> using namespace std; class Counter { public: Counter(); Counter(int val); ~Counter(){} int Get. Its. Val()const { return its. Val; } void Set. Its. Val(int x) {its. Val = x; } void Increment() { ++its. Val; } Counter operator++ (); private: int its. Val; }; Counter: : Counter(): its. Val(0) {} Counter: : Counter(int val): its. Val(val) {} Counter: : operator++() { ++its. Val; return Counter (its. Val); } 24 list 10. 10

10일째 고급 함수들 int main() { Counter i; cout << "The value of i

10일째 고급 함수들 int main() { Counter i; cout << "The value of i is " << i. Get. Its. Val() << endl; i. Increment(); cout << "The value of i is " << i. Get. Its. Val() << endl; ++i; cout << "The value of i is " << i. Get. Its. Val() << endl; Counter a = ++i; cout << "The value of a: " << a. Get. Its. Val(); cout << " and i: " << i. Get. Its. Val() << endl; return 0; } 25

10일째 고급 함수들 #include <iostream> using namespace std; list 10. 11 class Counter {

10일째 고급 함수들 #include <iostream> using namespace std; list 10. 11 class Counter { public: Counter(); ~Counter(){} int Get. Its. Val()const { return its. Val; } void Set. Its. Val(int x) {its. Val = x; } void Increment() { ++its. Val; } const Counter& operator++ (); private: int its. Val; }; Counter: : Counter(): its. Val(0) {}; const Counter& Counter: : operator++() { ++its. Val; return *this; } 26

10일째 고급 함수들 int main() { Counter i; cout << "The value of i

10일째 고급 함수들 int main() { Counter i; cout << "The value of i is " << i. Get. Its. Val() << endl; i. Increment(); cout << "The value of i is " << i. Get. Its. Val() << endl; ++i; cout << "The value of i is " << i. Get. Its. Val() << endl; Counter a = ++i; cout << "The value of a: " << a. Get. Its. Val(); cout << " and i: " << i. Get. Its. Val() << endl; return 0; } 27

10일째 고급 함수들 #include <iostream> using namespace std; list 10. 12 class Counter {

10일째 고급 함수들 #include <iostream> using namespace std; list 10. 12 class Counter { public: Counter(); Counter(int init. Val); Counter(const Counter& rhs); ~Counter(){} int Get. Its. Val()const { return its. Val; } void Set. Its. Val(int x) {its. Val = x; } const Counter& operator++ (); // 전치 const Counter operator++ (int); // 후치 private: int its. Val; }; 29

10일째 고급 함수들 Counter: : Counter(): its. Val(0) {} Counter: : Counter(int init. Val):

10일째 고급 함수들 Counter: : Counter(): its. Val(0) {} Counter: : Counter(int init. Val): its. Val(init. Val) {} Counter: : Counter(const Counter& rhs): its. Val(rhs. its. Val) {} const Counter& Counter: : operator++() { ++its. Val; return *this; } const Counter: : operator++(int the. Flag) { Counter temp(*this); ++its. Val; return temp; } 30

10일째 고급 함수들 int main() { Counter i(3); cout << "The value of i

10일째 고급 함수들 int main() { Counter i(3); cout << "The value of i is " << i. Get. Its. Val() << endl; i++; cout << "The value of i is " << i. Get. Its. Val() << endl; ++i; cout << "The value of i is " << i. Get. Its. Val() << endl; Counter a = ++i; cout << "The value of a: " << a. Get. Its. Val(); cout << " and i: " << i. Get. Its. Val() << endl; a = i++; cout << "The value of a: " << a. Get. Its. Val(); cout << " and i: " << i. Get. Its. Val() << endl; return 0; } 31

10일째 고급 함수들 #include <iostream. h> using namespace std; list 10. 13 class Counter

10일째 고급 함수들 #include <iostream. h> using namespace std; list 10. 13 class Counter { public: Counter(); Counter(int initial. Value); ~Counter(){} int Get. Its. Val()const { return its. Val; } void Set. Its. Val(int x) {its. Val = x; } Counter Add(const Counter &); private: int its. Val; }; Counter: : Counter(int initial. Value): its. Val(initial. Value) {} Counter: : Counter(): its. Val(0) {} Counter: : Add(const Counter & rhs) { return Counter(its. Val+ rhs. Get. Its. Val()); } 33

10일째 고급 함수들 int main() { Counter var. One(2), var. Two(4), var. Three; var.

10일째 고급 함수들 int main() { Counter var. One(2), var. Two(4), var. Three; var. Three = var. One. Add(var. Two); cout << "var. One: " << var. One. Get. Its. Val()<< endl; cout << "var. Two: " << var. Two. Get. Its. Val() << endl; cout << "var. Three: " << var. Three. Get. Its. Val() << endl; return 0; } 34

10일째 고급 함수들 #include <iostream> using namespace std; list 10. 14 class Counter {

10일째 고급 함수들 #include <iostream> using namespace std; list 10. 14 class Counter { public: Counter(); Counter(int initial. Value); ~Counter(){} int Get. Its. Val()const { return its. Val; } void Set. Its. Val(int x) {its. Val = x; } Counter operator+ (const Counter &); private: int its. Val; }; Counter: : Counter(int initial. Value): its. Val(initial. Value) {} Counter: : Counter(): its. Val(0) {} Counter: : operator+ (const Counter & rhs) { return Counter(its. Val + rhs. Get. Its. Val()); } 35

10일째 고급 함수들 int main() { Counter var. One(2), var. Two(4), var. Three; var.

10일째 고급 함수들 int main() { Counter var. One(2), var. Two(4), var. Three; var. Three = var. One + var. Two; cout << "var. One: " << var. One. Get. Its. Val()<< endl; cout << "var. Two: " << var. Two. Get. Its. Val() << endl; cout << "var. Three: " << var. Three. Get. Its. Val() << endl; return 0; } 36

10일째 고급 함수들 #include <iostream> using namespace std; list 10. 15 class CAT {

10일째 고급 함수들 #include <iostream> using namespace std; list 10. 15 class CAT { public: CAT(); int Get. Age() const { return *its. Age; } int Get. Weight() const { return *its. Weight; } void Set. Age(int age) { *its. Age = age; } CAT & operator=(const CAT &); private: int *its. Age; int *its. Weight; }; CAT: : CAT() { its. Age = new int; its. Weight = new int; *its. Age = 5; *its. Weight = 9; } 38

10일째 고급 함수들 CAT & CAT: : operator=(const CAT & rhs) { if (this

10일째 고급 함수들 CAT & CAT: : operator=(const CAT & rhs) { if (this == &rhs) return *this; *its. Age = rhs. Get. Age(); *its. Weight = rhs. Get. Weight(); return *this; } int main() { CAT frisky; cout << "frisky's age: " << frisky. Get. Age() << endl; cout << "Setting frisky to 6. . . n"; frisky. Set. Age(6); CAT whiskers; cout << "whiskers' age: " << whiskers. Get. Age() << endl; cout << "copying frisky to whiskers. . . n"; whiskers = frisky; cout << "whiskers' age: " << whiskers. Get. Age() << endl; return 0; } 39

10일째 고급 함수들 #include <iostream> using namespace std; list 10. 15 심화 class Eng

10일째 고급 함수들 #include <iostream> using namespace std; list 10. 15 심화 class Eng { public: Eng( int num ); Eng & operator=(const Eng &); private: int its. Num; int *data; }; Eng: : Eng( int num ) { its. Num = num; data = new int[its. Num]; for( int i=0 ; i<its. Num ; i++ ) data[i] = 0; } 40

10일째 고급 함수들 Eng & Eng: : operator=(const Eng & rhs) { if (this

10일째 고급 함수들 Eng & Eng: : operator=(const Eng & rhs) { if (this == &rhs) return *this; delete data; its. Num = rhs. its. Num data = new int [its. Num] ; for( int i=0 ; i<its. Num ; i++ ) data[i] = rhs. data[i] ; return *this; } int main() { Eng class. A( 40 ); Eng class. Temp( 10 ); … class. Temp = class. A; class. A = class. A return 0; } 41

10일째 고급 함수들 #include <iostream> using namespace std; list 10. 17 class Counter {

10일째 고급 함수들 #include <iostream> using namespace std; list 10. 17 class Counter { public: Counter(); Counter(int val); ~Counter(){} int Get. Its. Val()const { return its. Val; } void Set. Its. Val(int x) {its. Val = x; } private: int its. Val; }; Counter: : Counter(): its. Val(0) {} Counter: : Counter(int val): its. Val(val) {} int main() { int the. Short = 5; Counter the. Ctr = the. Short; cout << "the. Ctr: " << the. Ctr. Get. Its. Val() << endl; return 0; } 43

10일째 고급 함수들 #include <iostream> list 10. 18 class Counter { public: Counter(); Counter(int

10일째 고급 함수들 #include <iostream> list 10. 18 class Counter { public: Counter(); Counter(int val); ~Counter(){} int Get. Its. Val()const { return its. Val; } void Set. Its. Val(int x) {its. Val = x; } operator int (); private: int its. Val; }; Counter: : Counter(): its. Val(0) {} Counter: : Counter(int val): its. Val(val) {} Counter: : operator unsigned short () { return ( int (its. Val) ); } 45

10일째 고급 함수들 int main() { Counter ctr(5); int the. Short = ctr; std:

10일째 고급 함수들 int main() { Counter ctr(5); int the. Short = ctr; std: : cout << "the. Short: " << the. Short << std: : endl; return 0; } 46