110 likes | 294 Views
計算機概論實習 2007-06-15. Claim a Parameter. int a; a = 5;. 5. 'A'. char c; c = 'A';. Print a Parameter. cout << a; cout << &a;. 5. 'A'. Claim a Pointer. int* ap; ap = &a;. 5. 0x0001. Print a Point. cout << ap; cout << &ap; cout << *ap;. 5. 0x0001. Claim a Class (Object).
E N D
Claim a Parameter int a; a = 5; 5 'A' char c; c = 'A';
Print a Parameter cout << a; cout << &a; 5 'A'
Claim a Pointer int* ap; ap = &a; 5 0x0001
Print a Point cout << ap; cout << ≈ cout << *ap; 5 0x0001
Claim a Class (Object) class A{ int p; int f(){return p;} }; A a; a.a = 5; 5 A &ap = a;
Print the Content of Object 5 4 cout << "&a: " << &a << endl; cout << "&ap: " << &ap << endl; cout << "a.f(): " << a.f() << endl; cout << "ap.f(): " << ap.f() << endl; a.a = 4; cout << "a.f(): " << a.f() << endl; cout << "ap.f(): " << ap.f() << endl; &a: 0x0001 &ap: 0x0001 a.f(): 5 ap.f(): 5 a.f(): 4 ap.f(): 4
Call by Value void main(){ int a; a = 5; f(a); } void f(int c){ c = 4; } 1 2 5 3 2 3 4 4 5 3 4 1 2 4 3
Call by Address void main(){ int a = 5; f(a); } void f(int* ap){ *ap = 4; ap = ap + 1; } 1 2 4 5 2 1 3 3 4 1 2 3 4 2 4 0x0001 0x0005
Call by Reference void main(){ A a; a.a = 5; f(a); } void f(A& ao){ ao.a = 4; } 1 2 4 5 1 3 2 4 3 4 1 2 3 4
Try it!!! • Please explain that what happen in the following code. • Please write a sample code to prove your mind. A a1,a2; a1.a = 5; a2 = a1; : :