Title: Copy constructor
1????? ??????
- Copy constructor
- Assignment operator
- ???? ??????? ?????????
- ????? ???? Templates
2Copy constructorAssignment operator
3Copy constructor Assignment operator
- ?? ???? ???? ?????? s1 s2?
- ?? ???? ???? ?????? Stack s1 s2?
- ?? ?????? ?? ??????
- ?? ???? ???? ?????? ????????
- f(Stack s)
- f(Stack s)
- ????? copy ctor - ??????, assign. Op. - ?????
4Copy constructor Assignment operator
- ???? ?? ????? ????, ??????? ????? ??????? ???
???????? - copy constructor
- ???? ?????? ?? ??? ??? ?? ?????? ?"? ??? ??? ??
???? ????? - ???? ????? ??????? ?????? ?????????, ????? ????
??????? ????? ???? ????? ?????? by value, ????
????? ?? ???????? ???????? ?"? ????? ???????? ???
(??"? ???? ????? ?????) -
-
- X(const X)
????? ??? ???? ?? ?????? ??????
?? ???? ?????? copy constructor ??????? copy
constructor
5Copy constructor Assignment operator
- ??????? ????? ()
- ???? ????? ?? ????? ??????? ?? ??? ??? ?????? ??
??? ??? ?? ?????? ?"? ??? ??? ?? ???? ?????
(????? ?? ????? ????? ?-copy ctor, ????? ??
??????? ????? ?????? ??????) - ???? ??????, ???????? ????? ????? ?????? ??? ????
????. ?? ????? ???? ????? ???? ???? ???? ????
(????, ????? ?? ?? ??????? ????? ??????). ??????
????, ?? ?????? ?????? ???????? ??? ????
6Copy constructor Assignment operator
- ??? ???? ??? ????? ?? ?? ???? ??? ???????, ??
?????? ???? - Copy Constructor
- Assignment Operator
- Destructor
7The Final Stack
class Stack int array int topIndex,
size public Stack (int initSize 100)
Stack (const Stack) Stack
operator(const Stack) Stack() Result
push(int element) Result pop() Result
top(int element) const void print() const
8Copy constructor
StackStack(const Stack stack) array(new
intstack.size) , topIndex(stack.topIndex),
size(stack.size) for (int i 0 i lt
stack.topIndex i) arrayi
stack.arrayi
9Assignment operator
- Stack Stackoperator(const Stack stack)
if (this stack) return this - if (stack.size ! size)
- delete array
- array new intsize stack.size
-
- for (int i 0 i lt stack.topIndex i)
- arrayi stack.arrayi
-
- topIndex stack.topIndex
- return this
???? ??????????? ???? ?????????? ???????????
?????? ????? ????? ????? ????? this
10???? ??????? ?????????
11User Defined Type Conversions
- ? C ?????? ??? ???? ????? ??????? ?????????
- Conversion by constructor
- Conversion by conversion operator
12Conversion by constructor
- ????? ctors ?? ????? T ???? ????? ?? ????? ????
?? ??????? ??????? ?? ???? ?? ?????? ????? ???? T - ????? ?????? ?? ????? Constructor ????? ???????
????, ??????? ???. - T(class C) //converts from class C to T
- ????? ????? ???????? ?? ??? ?????????
- ????? ?????? ????? ????????? ?????? ???? ???????
?????? T ?? ??????? ?????? C ????? - ?????
- ?????? Complex ???? ?-double (??? ???)
- ???? ?- const char ?????? String (????? ????)
13????? - ?????? Complex
- ???? ?????? ????? ?? ?????? ???????. ???? ??
????? ?? ?????? ?????? ??????? ??? ?????? ???????
??????? ??????. ??? ??? ????? ??? ??? ?????? ??
?? ????????? ???? ?? ???????, ????? ?????? ???? - class Complex
- double re,im
- public
- Complex (double r, double i) re(r), im(i)
- friend Complex operator(Complex, Complex)
- friend Complex operator(Complex, double)
- friend Complex operator(double, Complex)
- // Do the same for the rest of the operators
-
- ???? ?? ?????? ????? ?????. ??? ????? ????? ??
???? ???? ??? ?????? constructor ?-Complex ???
???? ????? ???? ?????? double. ??? ????? ?????
???????? ??????? constructor ??
14????? - ?????? Complex
- class Complex
- //...
- Complex(double r) re(r), im(0)
- friend Complex operator(Complex,Complex)
-
- ????? ???? ?? ????? ????? ???????. ??????
- int main()
-
- Complex a
- Complex b
- ab23
?? ?????? ?? 1. 23 ???? ?- Complex ?"? ?-
constructor. 2. ????? ????? ??? ??? Complex. 3. a
???? ?? ?????? ?"? ??????? ????? ?????? ??????
???? ?"? ????? (????? ???? ????).
15Conversion by conversion operator
- ????? ????????? ??????? ???????? ?????? T ????
????? ?? T ???????? ??????? ?? ???? ?? ???????
????? ???? ?????? - ???? ?????? ?????? ???? ???????? ??????? ?? ????
(????) - ???? ?? ?????? ?? ????? conversion operator
(????? ?? ?????? T) - operator C() // converts from T to C.
- ?????
- class Price
- int shekels, agorot
- public .
- operator double() return shekels agorot /
100.0 -
- ???? ?? ????????? ??? ??? ????? ??? ???????
?-return ????? ?? ??? ?????
16????? ?? ????? ?????????
- class x // ...
- x(int) x(char)
- class y //...
- y(int) .
- class z //...
- z(x) .
- x f(x)
- y f(y)
- z g(z)
- void k1()
- f(1) // Illegal ambiguous f(x(1)) or f(y(1))
- f(x(1))
- f(y(1))
- g("asdf") // Illegal g(z(x("asdf"))) not tried
- g(z("asdf")) // O.k. x(char) is carried out
- ??? ??? ?????? ??? ????? ?? ?????
- ?????? ?? ???? ??? ?? ??????
- ????? ??????? ?? ?????? (ambiguity)
17????? ??????????
- ????? operators ?? ???? ????????. ??? ?? ?????
????? ?????? ????. ??????, ??????? ?????? ??????
Complex ???? ?????? - c2 c1 5
- ???? ?? ??????? ????? ? method ?? ????? ????
?? ???????? ?????? ???. ???? ???? ??, ???? ??????
????????? ???????? ?"? ??????? ???????. - class Complex ...
- public
- Complex(double r) re(r), im(0)
- Complex operator-(Complex)
-
- Complex operator(Complex, Complex)
- main()
- Complex c2,c1
- c12.0-c2 // Error 2.0 will not be
converted - c12.0c2 // O.k. 2.0 will be converted
-
-
18????? ???? Templates
19????? ???? - Templates
- ??????? ?-template ????? ????? ???? ?????
Containers - ?????? ??? ?????? ????? ?????, ????
??? ?????? ????????? ????????? ?? ???? ??? - ???? ?????? ??????? ?? ? Container ??? ??? ????
???. ?????? ???? ?????? ???? ?????? - push
- pop
- top
- ??? ?????? ?? ?? ?????? ?? int , char ?? String
20????? ???????
- int_stack.h
- class intStack
- int topIndex,size
- int array
- public
- int_stack(int s)
- void pop()
- void push(int e)
- int top()
- int size()
- char_stack.h
- class charStack
- int topIndex,size
- char array
- public
- char_stack(int s)
- void pop()
- void push(char e)
- char top()
- int size()
-
- String_stack.h
- class stringStack
- int topIndex,size
- String array
- public
- string_stack(int s)
- void pop()
- void push(String e)
- String top()
- int size()
-
21????? ???????
- int_stack.cpp (????)
- intStackintStack (int s)
-
- topIndex 0 size s
- array new ints
-
- int intStacktop()
- return arraytopIndex-1
-
- string_stack.cpp (????)
- stringStackstringStack (int s)
-
- top_index 0 size s
- array new Strings
-
- String stringStacktop()
- return arraytopIndex-1
-
22?????? ??????
- ???? ?????? ?????? ???? ???? ? Template
- ?-Template ???? ?????(??) ??????, ????? ??????
?????? ?? ????? ??? ????? ???? - ? Template ??? ???? ????? ?????? ?????? (????
???? ??????). ???? ??????? ?-Template ??
?????? ??? ??? ???? (?? ????? lt ????? gt ???
?-Template ???? ????? ????? ??? ???
23?? ???? ??????
- ?????? ????? ???? Template ??? ?????? ??????
????? ?????, ??? ?????? ????? template ltclass Tgt
???? ????? ?????? - ??? ???? ????? ?? ??? ?????? ??? ?????? ????????
?-T ??? ????? ?????, ?????? ?????? ?? ????? ???? - ???? ???????, ???? ???? ?????? ?-Template ????
??? ????? ?? T, ????? ?? ??? ?? ?? ?? ????????
??????? (??? ??????? ?????) - ???? ?????????? ????? ????? ?? ???? ?????? ????
?-Template ???? ???? ??? ????? T ????? ????
?????? ?-Template ??????
24?????? ?????
- stack.h
- template ltclass Tgt
- class stack
- int topIndex,size
- T array
- public
- stack(int s100)
- void pop()
- void push(T e)
- T top()
- int size()
- int_stack.h
- class intStack
- int topIndex,size
- int array
- public
- intStack(int s)
- void pop()
- void push(int e)
- int top()
- int size()
25?????? ?????
- stack.h (????)
- template ltclass Tgt
- stackltTgtstack (int s) topIndex (0), size(s),
array(new Ts) -
-
- template ltclass Tgt
- T
- stackltTgt top()
- return arraytopIndex-1
-
- int_stack.cpp (????)
- intStackintStack (int s)
- topIndex (0), size(s),
- array(new ints)
-
-
- int
- intStacktop()
- return arraytopIndex-1
-
26????? ??????? ??????
- int main()
- stackltintgt si(100)
- stackltchargt sc(3)
- si.push(5)
- sc.push(xxx)
- si.push(xxx) // error !
- sc.push(6) // error !
- si.size()
- sc.size()
-
- int main()
- int_stack si(100)
-
- si.push(5)
-
- si.push(xxx) // error !
-
- si.size()
-
27Templates
- Templates ???? ??? ???? ????? ????? ?????
Containers - ?????? ?? Templates ? C ????? macro ??? ???? ??
???? ?? ? Template ???? ? header file, ??? ?????,
?? ? inline function - ???? ?? ?? ?????? ??? ?????? ??? ?? ????????
??????
28???????? ??????
- int
- major (int a , int b , int c )
- if (a b a c) return a
- if (b c) return b
- exit(1)
-
- ?????
- int j major (1,3,3) // ?
- char c major (w,w,w)// ?
- String s1 major(s1,s2,s3) // ?
- int j major (1,a,a) // ? ?
- template ltclass Tgt
- T
- major (T a ,T b , T c )
- if (a b a c) return a
- if (b c) return b
- exit(1)
-
- ?????
- int j major (1,3,3) // ?
- char c major (w,w,w) //?
- String s1 major(s1,s2,s3) // ? ?
- int j major (1,a,a) //?
- int j majorltintgt (1,a,a) // ?
29Templates - advanced issues
- ????? ? ? Template ?? ??????? ?????????
???????? ?? ?????? ?????? ?????? ??? ???? ?????
????? ???? ????? ?? ???????? ?? ?????? ???????
??. - ???? ????? ???? major ?? T ? ???? stack ?
- ???? ?????? ??? ??????? ????? ??????
- template ltclass K, class Dgt class hashtable
- bool find (const K k, D d)
-
30??????? ? Template
???? ?????? ? Template ????? ????? ?????. ?????
???? ?????, ??? ????, ???? ?? ??????? template
ltclass T, int Dimgt class Vector T arrayDim
Vector ltint, 5gt v Vector lt
double, 12 gt v1 Vector ltComplex, 3gt v2 Vector
ltint , 6gt v3 ?? ?????? ??? ????? ? buffer ???
??? ??????? ? (??? ?? ???? ? vv3 ? )
31Templates - advanced issues
- ????? ????? ?? template ???? ????? ??? ???. ????
???? ??? ????? ??? ???? ?????? ??? ?????? ??????
??? - typedef stackltintgt stack_int
- ?? ???? ?????? ?????? ?? ??????? ??? ????? ??
?????? - typedef stackltstack_intgt stack_stack_int
- ????? ??
- stack_int s1(100)
- stack_stack_int s2(5)
- s2.push(s1) // in this case push should have
better // accepted const T instead of T ...