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

Post on 19-Dec-2015

229 views 0 download

Tags:

Transcript of 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:

Passing arguments by value

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

...

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

10a:

Passing arguments by value

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

...

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

10a:

Passing arguments by value

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

...

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

10a:

10x:

Passing arguments by value

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

...

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

10a:

4x:

Passing arguments by value

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

...

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

10a:

Passing arguments by reference

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

...

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

a: 10

Passing arguments by reference

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

...

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

a: 10

Passing arguments by reference

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

...

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

a: 10

x:

Passing arguments by reference

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

...

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

a: 4

x:

Passing arguments by reference

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

...

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

a: 4

Passing arguments by name

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

...

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

10a:

Passing arguments by name

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

...

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

10a:

Passing arguments by name

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

...

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

10x:

Passing arguments by name

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

...

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

4x:

Passing arguments by name

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

...

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

4x:

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

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

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:

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:

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

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

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

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:

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

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

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

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

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

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:

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:

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:

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:

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:

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:

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:

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:

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:

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: