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

Slides:



Advertisements
Similar presentations
Complete Structure class Date {class Date { private :private : // private data and functions// private data and functions public :public : // public data.
Advertisements

Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
M The University Of Michigan Andrew M. Morgan Andrew M Morgan1 EECS Lecture 03 Savitch Ch. 3-4, Misc More on Functions Memory, Activation Records,
For(int i = 1; i
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Starting Out with C++, 3 rd Edition 1 Chapter 10 – Characters, Strings, and the string Class.
Inheritance (2).
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Template. 2 Using templates, it is possible to create generic functions and classes. In a generic function or class, the type of data upon which the function.
Functions Prototypes, parameter passing, return values, activation frams.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
OBJECT ORIENTED FEATURES AND IMPLEMENTATION OF UNIVERSAL QUANTIFICATION IN C++ Karoly Bosa RISC SS2000.
Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
Introduction to Programming Lecture 39. Copy Constructor.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
EC-241 Object-Oriented Programming
Scientific Programming for(i=0; i b) { a = func1(c,d,i*10); } else if(a < b) { a = func2(e,f,i*10); } else { a = func3(g,h,i*10);
A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
1 Class Vehicle #include #define N 10../.. 2 Class Vehicle class vehicle { public: float speed; char colour[N+1]; char make[N+1];
Union, bitfield, typedef, enum union nama_u{ }; union nama_u{ struct nama_s byte; }; enum{ }; Tipedef var BYTE.
Single Right rotation (compare to RotateWithLeftChild page 148 text) void rotateRight(AVLNode *& curr) { AVLNode * kid = curr->left; curr->left = kid->right;
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Passing information through Parameters ( our setter methods or mutators) Formal Parameter – parameter in the method. public void SetA(int x){ (int x) is.
CS Feb 2007 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
CS Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
Functions Pass by Value Pass by Reference IC 210.
Chapter 7: Pointers Basic concept of pointers Pointer declaration Pointer operator (& and *) Parameter passing by reference.
ITEC 320 C++ Examples.
1 CSC241: Object Oriented Programming Lecture No 22.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
The This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros
Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
1 Object-Oriented Programming Using C++ A tutorial for pointers.
Constructors Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction.
C:\Temp\Templates 4 5 Use This Main Program 6.
1 FUNCTIONS - I Chapter 5 ANIMATION. 2 3 Demos Demo of a simple value-returning function Demo of a void function Demo of a void function calling a value-
CONSTRUCTOR AND DESTRUCTOR. Topics to be discussed……………….. 1. Introduction Introduction 2. FeaturesFeatures 3. Types of ConstructorTypes of Constructor.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Object Oriented Programming (OOP) Lecture No.2 Downloaded From:
Pointers and Dynamic Arrays
Overloaded Constructors and Multiple Classes
Object Oriented Programming (OOP) Lecture No. 8
CSC241: Object Oriented Programming
Command line arguments
group work #hifiTeam
HI !.
Pointers & Functions.
Lec 14 Oct 23, 02.
Inheritance: Polymorphism and Virtual Functions
Default Arguments.
Dynamic Memory A whole heap of fun….
Introduction to Programming
Passing Arguments and The Big 5
Function Overloading.
Guidelines for Writing Functions
CS150 Introduction to Computer Science 1
Functions Lecture 5.
Pointers & Functions.
The Stack.
Review of Function Overloading
Class: Special Topics Overloading (methods) Copy Constructors
BETONLINEBETONLINE A·+A·+
Constructors & Destructors
Announcements Exam 2 Lecture Grades Posted on blackboard
Presentation transcript:

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”; 10 a:

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

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

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

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

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”; 10 a:

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

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

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

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

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: