Functions and Methods. Questions:

Similar documents
Object Reference and Memory Allocation. Questions:

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

CSE 303: Concepts and Tools for Software Development

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Pointers, Dynamic Data, and Reference Types

C++ for Java Programmers

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

a data type is Types

Implementing an ADT with a Class

CS2141 Software Development using C/C++ C++ Basics

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

CS201 Some Important Definitions

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

CA31-1K DIS. Pointers. TA: You Lu

Introduction Of Classes ( OOPS )

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

Lecture 23: Pointer Arithmetic

Exercise 1.1 Hello world


Short Notes of CS201

Function Overloading

CS201 - Introduction to Programming Glossary By

Introduction to C++ Introduction to C++ 1

Intermediate Programming, Spring 2017*

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

Outline. 1 About the course

Dynamic Allocation of Memory

8. The C++ language, 1. Programming and Algorithms II Degree in Bioinformatics Fall 2017

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Lecture 2, September 4

pointers + memory double x; string a; int x; main overhead int y; main overhead

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

Extending Classes (contd.) (Chapter 15) Questions:

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

Function Declarations. Reference and Pointer Pitfalls. Overloaded Functions. Default Arguments

Intermediate Programming & Design (C++) Classes in C++

Arrays. Week 4. Assylbek Jumagaliyev

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Practice question Answers

Scope. Scope is such an important thing that we ll review what we know about scope now:

A brief introduction to C programming for Java programmers

ECE Fall 20l2, Second Exam

Memory and Pointers written by Cathy Saxton

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

6. Pointers, Structs, and Arrays. 1. Juli 2011

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

CSC 211 Intermediate Programming. Arrays & Pointers

CPSC 427: Object-Oriented Programming

6.096 Introduction to C++ January (IAP) 2009

CSC1322 Object-Oriented Programming Concepts

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

Inheritance, and Polymorphism.

IS0020 Program Design and Software Tools Midterm, Fall, 2004

Chapter 7 - Notes User-Defined Functions II

ENERGY 211 / CME 211. Functions

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

C11: Garbage Collection and Constructors

Classes, The Rest of the Story

CSE143 Exam with answers MIDTERM #1, 1/26/2001 Problem numbering may differ from the test as given.

Classes in C++98 and C++11

Chapter 9: Pointers. Copyright 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.

Chapter 9: Getting the Address of a Variable. Something Like Pointers: Arrays. Pointer Variables 8/23/2014. Getting the Address of a Variable

Week 3: Pointers (Part 2)

Pointers II. Class 31

Java Basic Syntax. Java vs C++ Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

CS 376b Computer Vision

Object Oriented Programming COP3330 / CGS5409

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

CHAPTER 4 FUNCTIONS. 4.1 Introduction

C++ Programming Lecture 7 Software Engineering Group

Introduction to C++ Part II. Søren Debois. Department of Theoretical Computer Science IT University of Copenhagen. September 12th, 2005

Pointers and Terminal Control

Computer Components. Software{ User Programs. Operating System. Hardware

the gamedesigninitiative at cornell university Lecture 6 C++: Basics

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Fast Introduction to Object Oriented Programming and C++

04-19 Discussion Notes

Chapter 8. Operator Overloading, Friends, and References. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Lecture 7: Binding Time and Storage

Object-Oriented Principles and Practice / C++

Recap: Pointers. int* int& *p &i &*&* ** * * * * IFMP 18, M. Schwerhoff

9.2 Pointer Variables. Pointer Variables CS Pointer Variables. Pointer Variables. 9.1 Getting the Address of a. Variable

Object Oriented Programming in C#

CSE 333 Midterm Exam July 24, Name UW ID#

Exam 3 Chapters 7 & 9

Object Oriented Software Design II

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

Template Issue Resolutions from the Stockholm Meeting

C and C++ I. Spring 2014 Carola Wenk

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

C10: Garbage Collection and Constructors

CSE 307: Principles of Programming Languages

Pointers and References

Transcription:

Functions and Methods Questions: 1

1. What is a reference in C++? 2

2. In the following code, which variable is being declared as a reference? Also, suppose we attempt to change the object of a reference, as in the fifth line below, will that work? int i = 30; int j = 100; int& r = i; r++; // i is now? r = j; // i is now? r++; // i is now? 3

3. If q is of type int*, is the following C++ syntax legal? int*& p = q; 4

4. Do the following two statements use the same operator for memory allocation? string* s1 = new string( "hello" ); string* s2 = new string[100]; 5

5. With regard to memory allocation, what s the crucial difference between the following two statements? In both cases we are allocating memory for an int array of 1000 elements. int* data = new int[1000]; int data[1000]; 6

6. What s the relationship between the operators new and new[], on the one hand, and the following two functions on the other? void* operator new( size_t size ); void* operator new[] (size_t size ); 7

7. Are there any memory leaks in the following program? Is so how will you plug them? Is there anything else wrong with the code? If so, how will you fix it? Will the program compile as it is? #include <string> class X ; int main() string* words[1000] = 0; X* xp = new X(); X* xdata = new X[1000]; delete xp; delete xdata; // More code that could possibly contain references // to xp and xdata. But this additional code does // not deallocate the memory allocated above. 8

8. What is the rule-of-thumb to remember to prevent memory leaks in C++ programs? 9

9. What s the access privilege of the two data members in the following C++ structure? struct User string name; int age; ; 10

10. What s the main difference between C and C++ with regard to the following structure? struct User string name; int age; ; 11

11. Is this syntax legal in C++? enum weight light, medium, heavy, very_heavy; int main() weight w1; w1 = heavy; weight w2 = (weight) (w1 + 200); cout << w2; 12

Function Declarations In C++, a function cannot be called unless it was declared previously. C++ function declarations, also called function prototypes, look like this: double cos( double ); double cos( double x); void f(int, int, int*); void g(); A function prototype may contain parameter names for better program readability, as is the case with the second example. However, the compiler simply ignores such names. Java does not require function declarations. 13

Passing Arguments in C++ When a function is called, each of its parameters is initialized with the corresponding argument in the function call, provided their types agree after taking into account any allowable type conversions. There are three different ways to pass arguments to functions in C++: pass by value pass by pointer pass by reference 14

Passing a Primitive Type Argument by Value #include <iostream> void g( int ); int main() int x = 100; g(x); //(A) cout << x << endl; // will output 100 void g( int y ) y++; //(B) 15

Passing a Primitive Type Argument by Pointer #include <iostream> void g( int* ); void h( int* ); int main() int x = 100; int* p = &x; g(p); //(A) cout << x << endl; // outputs 100 h(p); cout << x << endl; // outputs 200 void g( int* q ) int y = 200; q = &y; void h( int* q ) *q = 200; 16

Passing a Primitive Type Argument by Reference #include <iostream> void g( int& ); int main() int x = 100; g(x); //(A) cout << x << endl; // outputs 101 void g( int& y ) y++; 17

Passing a Class Type Argument by Value Passing a class type argument by value works the same way as passing a primitive type argument. The called function makes a local copy of the argument elsewhere in the memory and any changes made to this local copy by the called function are not visible in the calling function. 18

#include <iostream> #include <string> class User public: string name; int age; User( string nam, int yy ) name = nam; age = yy; ; void g( User ); int main() User u( "Xenon", 89 ); //(A) g(u); //(B) cout << u.name << " " << u.age << endl; // Xenon 89 void g( User v ) v.name = "Yukon"; v.age = 200; //(C) 19

Passing a Class Type Argument by Pointer When an argument is passed by pointer in a C++ program, any changes brought about inside the called function can become visible in the calling function through side effect, as was the case with the primitive types. 20

#include <iostream> #include <string> class User public: string name; int age; User( string nam, int yy ) name = nam; age = yy; ; void g( User* ); void h( User* ); int main() User* p = new User( "Xeno", 89 ); //(A) g( p ); //(B) cout << p->name << " " << p->age << endl; // Xeno 89 h( p ); //(C) cout << p->name << " " << p->age << endl; // Yuki 200 void g( User* q ) q = new User( "Yuki", 200 ); //(D) void h( User* q ) q->name = "Yuki"; q->age = 200; 21

Passing a Class Type Argument by Reference #include <iostream> #include <string> class User public: string name; int age; User( string nam, int yy ) name = nam; age = yy; ; void g( User& ); int main() User u( "Xenon", 89 ); // (A) g(u); // (B) cout << u.name << " " << u.age << endl; // Yukon 200 void g( User& v ) v.name = "Yukon"; v.age = 200; 22

Passing Arguments in Java Java gives you just one mode for passing arguments to methods pass by value. For class type arguments, a more precise way to say how arguments are passed to methods is that it is pass by value of object reference. 23

Passing a Primitive Type Argument class Test public static void main( String[] args ) int x = 100; // (A) g(x); // (B) System.out.println( x ); // outputs 100 static void g( int y ) y++; // (C) 24

Passing a Class Type Argument Passing a class type argument by value in Java is different from passing a class type argument by value in C++. 25

class User String name; int age; User( String nam, int yy ) name = nam; age = yy; ; class Test public static void main( String[] args ) User u = new User( "Xeno", 89 ); // (A) g(u); // (B) System.out.println( u.name + " " + u.age ); // Yuki 200 static void g( User v ) v.name = "Yuki"; v.age = 200; 26

The mode pass argument by value of object reference in Java is similar to pass by pointer in C++. 27

It might seem that there exists a similarity between pass by value of object reference in Java and the pass by reference in C++. In both cases, the changes brought about by the called function were visible in the calling function. So can we say the mode for argument passing in Java is similar to pass argument by reference in C++? The answer is a categorical No. 28

class User String name; int age; User(String nm, int a) name=nm; age=a; class test public static void main(string[] args) User u1 = new User("Xeno", 95); User u2 = new User("Yuki", 98); swap( u1, u2 ); System.out.println("u1 s name is: " + u1.name + ", and u2 s name is " + u2.name); static void swap(user s, User t) User temp = s; s = t; t = temp; 29

#include <iostream> #include <string> class User public: string name; int age; User(string nm, int a) name=nm; age=a; ; void swap( User&, User& ); int main() User u1 = User("Xeno", 95); User u2 = User("Yuki", 98); swap( u1, u2 ); cout << "u1 is: " << u1.name << ", and u2 is " << u2.name << endl; void swap(user& s, User& t) User temp = s; s = t; t = temp; 30

#include <iostream> #include <string> class User public: string name; int age; User(string nm, int a) name=nm; age=a; ; void swap( User*, User* ); int main() User u1 = User("Xeno", 95); User u2 = User("Yuki", 98); swap( &u1, &u2 ); cout << "u1 is: " << u1.name << ", and u2 is " << u2.name << endl; void swap(user* s, User* t) User temp = *s; *s = *t; *t = temp; 31

To pass an argument by value in C++ means that the parameter of the called function is handed a copy of the argument object in the calling function. On the other hand, to pass an argument by value in Java means that the parameter of the called function is handed a copy of the object reference held by the argument. Said another way, in call by value in Java, the calling function hands the called function a copy of the object reference, but not a copy of the object itself. On the other hand, in call by value in C++, the calling function hands to the called function a copy of the object. A call by reference not available in Java means that you are providing to the function parameter the object itself, the object that is held by the argument in the calling function. The reference parameter in the called function simply serves as just another name for the argument in the calling function. 32

Functions Returning Reference Types What exactly is returned by a function whose return has been specified as not void? Is it the local object itself, or is it a copy of the local object? 33

#include <iostream> #include <string> class User public: string name; int age; User( string nam, int yy ) name = nam; age = yy; ; User f( User u ) return u; int main() User x( "Xino", 120 ); User y = f( x ); cout << "y: " << y.name << endl; 34

What the function f returns is a copy of the local object it is supposed to return. Therefore, the program will make a copy of the argument object when it invokes the functionfand then another copy of that copy when it executes its return statement. That can amount to a lot of copying for large objects. We have already seen how this copying can be eliminated when passing arguments by using either a reference parameter or a pointer parameter. But what about the copying entailed in the objects that are returned? 35

#include <iostream> #include <string> class User public: string name; int age; User( string nam, int yy ) name = nam; age = yy; ; User& f( User u ) return u; int main() User x( "Xino", 120 ); User y = f( x ); cout << "y: " << y.name << endl; 36

If you wanted to guarantee that the User object being passed to the function as a reference would not get corrupted inside the function, you could use the following version of the function: User f( const User& u ) return u; And if in addition to guaranteeing that the object passed by reference would not get corrupted inside the function, you also wanted to suppress copying upon return, you could use the following version of the function: const User& f( const User& u ) return u; User& f( const User& u ) return u; 37