39
assing arguments by value void func (int x) { x = 4; } ... int a = 10; ... func(a); cout << a << “\n”; a:

Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

  • View
    229

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by value

void func (int x) { x = 4;}

...

int a = 10;...func(a);cout << a << “\n”;

a:

Page 2: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by value

void func (int x) { x = 4;}

...

int a = 10;...func(a);cout << a << “\n”;

10a:

Page 3: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by value

void func (int x) { x = 4;}

...

int a = 10;...func(a);cout << a << “\n”;

10a:

Page 4: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by value

void func (int x) { x = 4;}

...

int a = 10;...func(a);cout << a << “\n”;

10a:

10x:

Page 5: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by value

void func (int x) { x = 4;}

...

int a = 10;...func(a);cout << a << “\n”;

10a:

4x:

Page 6: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by value

void func (int x) { x = 4;}

...

int a = 10;...func(a);cout << a << “\n”;

10a:

Page 7: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by reference

void func (int *x) { *x = 4;}

...

int *a = new int(10);...func(a);cout << *a << “\n”;

a: 10

Page 8: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by reference

void func (int *x) { *x = 4;}

...

int *a = new int(10);...func(a);cout << *a << “\n”;

a: 10

Page 9: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by reference

void func (int *x) { *x = 4;}

...

int *a = new int(10);...func(a);cout << *a << “\n”;

a: 10

x:

Page 10: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by reference

void func (int *x) { *x = 4;}

...

int *a = new int(10);...func(a);cout << *a << “\n”;

a: 4

x:

Page 11: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by reference

void func (int *x) { *x = 4;}

...

int *a = new int(10);...func(a);cout << *a << “\n”;

a: 4

Page 12: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by name

void func (int &x) { x = 4;}

...

int a = 10;...func(a);cout << a << “\n”;

10a:

Page 13: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by name

void func (int &x) { x = 4;}

...

int a = 10;...func(a);cout << a << “\n”;

10a:

Page 14: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by name

void func (int &x) { x = 4;}

...

int a = 10;...func(a);cout << a << “\n”;

10x:

Page 15: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by name

void func (int &x) { x = 4;}

...

int a = 10;...func(a);cout << a << “\n”;

4x:

Page 16: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by name

void func (int &x) { x = 4;}

...

int a = 10;...func(a);cout << a << “\n”;

4x:

Page 17: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by value

class A {public: char *s; A(char *t) { s = new char[strlen(t)]; strcpy(s,t); }}; void func (A x) { x.s = “bye”;}...A a(“hi”);...func(a);cout << a << “\n”;

a.s: hi

Page 18: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by value

class A {public: char *s; A(char *t) { s = new char[strlen(t)]; strcpy(s,t); }}; void func (A x) { x.s = “bye”;}...A a(“hi”);...func(a);cout << a << “\n”;

a.s: hi

Page 19: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by value

class A {public: char *s; A(char *t) { s = new char[strlen(t)]; strcpy(s,t); }}; void func (A x) { x.s = “bye”;}...A a(“hi”);...func(a);cout << a << “\n”;

a.s: hi

x.s:

Page 20: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by value

class A {public: char *s; A(char *t) { s = new char[strlen(t)]; strcpy(s,t); }}; void func (A x) { x.s = “bye”;}...A a(“hi”);...func(a);cout << a << “\n”;

a.s: bye

x.s:

Page 21: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by value

class A {public: char *s; A(char *t) { s = new char[strlen(t)]; strcpy(s,t); }}; void func (A x) { x.s = “bye”;}...A a(“hi”);...func(a);cout << a << “\n”;

a.s: bye

Page 22: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by value

class A {public: char *s; A(char *t) { s = new char[strlen(t)]; strcpy(s,t); } A(A &t) { // copy constructor s = new char[strlen(t.s)]; strcpy(s,t.s); }};void func (A x) { x.s = “bye”; }...A a(“hi”);...func(a);cout << a << “\n”;

a.s: hi

Page 23: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by value

class A {public: char *s; A(char *t) { s = new char[strlen(t)]; strcpy(s,t); } A(A &t) { // copy constructor s = new char[strlen(t.s)]; strcpy(s,t.s); }};void func (A x) { x.s = “bye”; }...A a(“hi”);...func(a);cout << a << “\n”;

a.s: hi

Page 24: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by value

class A {public: char *s; A(char *t) { s = new char[strlen(t)]; strcpy(s,t); } A(A &t) { // copy constructor s = new char[strlen(t.s)]; strcpy(s,t.s); }};void func (A x) { x.s = “bye”; }...A a(“hi”);...func(a);cout << a << “\n”;

a.s: hi

x.s:

Page 25: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by value

class A {public: char *s; A(char *t) { s = new char[strlen(t)]; strcpy(s,t); } A(A &t) { // copy constructor s = new char[strlen(t.s)]; strcpy(s,t.s); }};void func (A x) { x.s = “bye”; }...A a(“hi”);...func(a);cout << a << “\n”;

a.s: hi

x.s:

t = a

Page 26: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by value

class A {public: char *s; A(char *t) { s = new char[strlen(t)]; strcpy(s,t); } A(A &t) { // copy constructor s = new char[strlen(t.s)]; strcpy(s,t.s); }};void func (A x) { x.s = “bye”; }...A a(“hi”);...func(a);cout << a << “\n”;

a.s: hi

x.s:

t = a

Page 27: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by value

class A {public: char *s; A(char *t) { s = new char[strlen(t)]; strcpy(s,t); } A(A &t) { // copy constructor s = new char[strlen(t.s)]; strcpy(s,t.s); }};void func (A x) { x.s = “bye”; }...A a(“hi”);...func(a);cout << a << “\n”;

a.s: hi

x.s: hi

t = a

Page 28: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by value

class A {public: char *s; A(char *t) { s = new char[strlen(t)]; strcpy(s,t); } A(A &t) { // copy constructor s = new char[strlen(t.s)]; strcpy(s,t.s); }};void func (A x) { x.s = “bye”; }...A a(“hi”);...func(a);cout << a << “\n”;

a.s: hi

x.s: bye

Page 29: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by value

class A {public: char *s; A(char *t) { s = new char[strlen(t)]; strcpy(s,t); } A(A &t) { // copy constructor s = new char[strlen(t.s)]; strcpy(s,t.s); }};void func (A x) { x.s = “bye”; }...A a(“hi”);...func(a);cout << a << “\n”;

a.s: hi

Page 30: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by reference

class A {public: char *s; A(char *t) { s = new char[strlen(t)]; strcpy(s,t); }}; void func (A *x) { x->s = “bye”;}...A *a = new A(“hi”);...func(a);cout << a << “\n”;

a: hi

a->s:

Page 31: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by reference

class A {public: char *s; A(char *t) { s = new char[strlen(t)]; strcpy(s,t); }}; void func (A *x) { x->s = “bye”;}...A *a = new A(“hi”);...func(a);cout << a << “\n”;

a: hi

a->s:

Page 32: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by reference

class A {public: char *s; A(char *t) { s = new char[strlen(t)]; strcpy(s,t); }}; void func (A *x) { x->s = “bye”;}...A *a = new A(“hi”);...func(a);cout << a << “\n”;

a: hi

a->s:

x:

Page 33: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by reference

class A {public: char *s; A(char *t) { s = new char[strlen(t)]; strcpy(s,t); }}; void func (A *x) { x->s = “bye”;}...A *a = new A(“hi”);...func(a);cout << a << “\n”;

a: bye

a->s:

x:

Page 34: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by reference

class A {public: char *s; A(char *t) { s = new char[strlen(t)]; strcpy(s,t); }}; void func (A *x) { x->s = “bye”;}...A *a = new A(“hi”);...func(a);cout << a << “\n”;

a: bye

a->s:

x:

Page 35: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by name

class A {public: char *s; A(char *t) { s = new char[strlen(t)]; strcpy(s,t); }}; void func (A &x) { x.s = “bye”;}...A a(“hi”);...func(a);cout << a << “\n”;

a: hi

a.s:

Page 36: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by name

class A {public: char *s; A(char *t) { s = new char[strlen(t)]; strcpy(s,t); }}; void func (A &x) { x.s = “bye”;}...A a(“hi”);...func(a);cout << a << “\n”;

a: hi

a.s:

Page 37: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by name

class A {public: char *s; A(char *t) { s = new char[strlen(t)]; strcpy(s,t); }}; void func (A &x) { x.s = “bye”;}...A a(“hi”);...func(a);cout << a << “\n”;

x: hi

a.s:

Page 38: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by name

class A {public: char *s; A(char *t) { s = new char[strlen(t)]; strcpy(s,t); }}; void func (A &x) { x.s = “bye”;}...A a(“hi”);...func(a);cout << a << “\n”;

x: bye

a.s:

Page 39: Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout

Passing arguments by name

class A {public: char *s; A(char *t) { s = new char[strlen(t)]; strcpy(s,t); }}; void func (A &x) { x.s = “bye”;}...A a(“hi”);...func(a);cout << a << “\n”;

x: bye

a.s: