Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout << a << “\n”; a:"— Presentation transcript:

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

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

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

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

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

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

7 Passing arguments by reference void func (int *x) { *x = 4; }... int *a = new int(10);... func(a); cout << *a << “\n”; a: 10

8 Passing arguments by reference void func (int *x) { *x = 4; }... int *a = new int(10);... func(a); cout << *a << “\n”; a: 10

9 Passing arguments by reference void func (int *x) { *x = 4; }... int *a = new int(10);... func(a); cout << *a << “\n”; a: 10 x:

10 Passing arguments by reference void func (int *x) { *x = 4; }... int *a = new int(10);... func(a); cout << *a << “\n”; a: 4 x:

11 Passing arguments by reference void func (int *x) { *x = 4; }... int *a = new int(10);... func(a); cout << *a << “\n”; a: 4

12 Passing arguments by name void func (int &x) { x = 4; }... int a = 10;... func(a); cout << a << “\n”; 10 a:

13 Passing arguments by name void func (int &x) { x = 4; }... int a = 10;... func(a); cout << a << “\n”; 10 a:

14 Passing arguments by name void func (int &x) { x = 4; }... int a = 10;... func(a); cout << a << “\n”; 10 x:

15 Passing arguments by name void func (int &x) { x = 4; }... int a = 10;... func(a); cout << a << “\n”; 4 x:

16 Passing arguments by name void func (int &x) { x = 4; }... int a = 10;... func(a); cout << a << “\n”; 4 x:

17 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

18 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

19 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:

20 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:

21 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

22 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

23 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

24 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:

25 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

26 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

27 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

28 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

29 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

30 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:

31 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:

32 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:

33 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:

34 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:

35 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:

36 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:

37 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:

38 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:

39 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:


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

Similar presentations


Ads by Google