計算機概論實習 2007-06-15. 2 Claim a Parameter int a; a = 5; typenameaddress inta0x0001 addressvalue 0x0001 0x0002 0x0003 0x0004 0x0005 0x0006 0x0007 0x0008.

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

Functions Prototypes, parameter passing, return values, activation frams.
Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
Revision.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Pointer to Structure. Structure variable can be access using pointers int a=10,*p; Here p  is an integer type pointer variable, p can hold the address.
1 Class Vehicle #include #define N 10../.. 2 Class Vehicle class vehicle { public: float speed; char colour[N+1]; char make[N+1];
Les pointeurs 1. 00A0 00A1 00A2 00A3 00A4 00A5 00A6 00A7 00A8 00A9 00AA 00AB 00AC 00AD 00AE 00AF 00B0 2 Code du programme BSSTas void main(){ node exemple(0);
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Local and Global Variables. COMP104 Local and Global / Slide 2 Scope The scope of a declaration is the block of code where the identifier is valid for.
Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout
計算機概論實習 Files and Streams C++ views file as sequence of bytes Ends with end-of-file marker n-1 end-of-file marker 67 This is.
計算機概論實習 Review of Practice 8 (P8) PLEASE write a program that when you fail to open a file, you have to throw a exception. Directly using.
計算機概論實習 What is Class In C++, class is the keyword which means encapsulation. Property and method are used to define what is class. Materialize.
Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects.
計算機概論實習 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators.
計算機概論實習 Stream Stream: sequence of bytes Input: from device (keyboard, disk drive) to memory Output: from memory to device (monitor, printer,
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Multiple-Subscripted Array
計算機概論實習 Last Practice (P4) members StudentTeacher private: char name[80]; protected: int number; public: void setName(char *); void getName(char*);
計算機概論實習 Class Sample: RECT Class weight length class RECT{ private: int weight, length; public: RECT(); int surface(); int perimeter();
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
計算機概論實習 A Simple Way to Handle Error if(b != 0) { cout
CS 240: Data Structures Supplemental: Command Line Input.
Chapter 05 (Part V) Control Statements: Part II. Nested For-Structures Consider the following codes: for (int i=0; i
 Review structures  Program to demonstrate a structure containing a pointer.
Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public:
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
Pointer Data Type and Pointer Variables II By: Nouf Aljaffan Edited by : Nouf Almunyif.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation.
1 Workin’ with Pointas An exercise in destroying your computer.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
Functions Sujana Jyothi C++ Workshop Day 2. Functions 3 Parameter transmission modes pass by value (default) pass by reference (&) pass by const reference.
Instructor - C. BoyleFall Semester
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
The This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros
Introduction to Programming Lecture 40. Class Class is a user defined data type.
Revision on C++ Pointers TCP1201: 2013/2014. Pointer Basics  Why pointer is important? 1. Reference/Point to existing data without cloning the data.
Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT9: Pointer I CS2311 Computer Programming.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
1 A Pointer: an address, a reference, a location of the computer memory A pointer of what? int, char, bool, double, or any kind of data type need to know.
 Review building a complete linked list  List traversal in main ( )  List traversal using a function.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
Popping Items Off a Stack Using a Function Lesson xx
Pointers & Arrays.
Programming fundamentals 2 Chapter 2:Pointer
CSC 253 Lecture 8.
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
CSC 253 Lecture 8.
Linked List Lesson xx   In this presentation, we introduce you to the basic elements of a linked list.
Dynamic Memory A whole heap of fun….
Introduction to Programming
Pointers & Functions.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Dynamic Memory A whole heap of fun….
Pointers Lecture 1 Thu, Jan 15, 2004.
Observing how the machine acts
-.&- ·Af& Q 0 "i'/
C++ Pointers and Strings
Function Overloading.
Pointers & Arrays.
Dynamic Memory Copy Challenge
Pointers & Functions.
The Stack.
Presentation transcript:

計算機概論實習

2 Claim a Parameter int a; a = 5; typenameaddress inta0x0001 addressvalue 0x0001 0x0002 0x0003 0x0004 0x0005 0x0006 0x0007 0x0008 0x0009 typenameaddress 5 char c; c = 'A'; typenameaddress inta0x0001 charc0x0006 'A'

3 Print a Parameter typenameaddress inta0x0001 charc0x0006 addressvalue 0x0001 0x0002 0x0003 0x0004 0x0005 0x0006 0x0007 0x0008 0x cout << a; cout << &a; 'A'

4 Claim a Pointer typenameaddress inta0x0001 int*ap0x0008 addressvalue 0x0001 0x0002 0x0003 0x0004 0x0005 0x0006 0x0007 0x0008 0x0009 int* ap; ap = &a; 0x0001 5

5 Print a Point typenameaddress inta0x0001 int*ap0x0008 addressvalue 0x0001 0x0002 0x0003 0x0004 0x0005 0x0006 0x0007 0x0008 0x0009 0x cout << ap; cout << &ap; cout << *ap;

6 Claim a Class (Object) typenameaddress value 0x0001 0x0002 0x0003 0x0004 0x0005 0x0006 0x0007 0x0008 0x0009 A a; a.a = 5; class A{ int p; int f(){return p;} }; typenameaddress Aa0x0001 a.a0x A &ap = a; typenameaddress Aa0x0001 a.a0x0001 Aap0x0001 ap.a0x0001

7 Print the Content of Object addressvalue 0x0001 0x0002 0x0003 0x0004 0x0005 0x0006 0x0007 0x0008 0x typenameaddress Aa0x0001 a.a0x0001 Aap0x0001 ap.a0x0001 cout << "&a: " << &a << endl; cout << "&ap: " << &ap << endl; &a: 0x0001 &ap: 0x0001 cout << "a.f(): " << a.f() << endl; cout << "ap.f(): " << ap.f() << endl; a.f(): 5 ap.f(): 5 a.a = 4; cout << "a.f(): " << a.f() << endl; cout << "ap.f(): " << ap.f() << endl; a.f(): 4 ap.f(): 4 4

8 Call by Value addressvalue 0x0001 0x0002 0x0003 0x0004 0x0005 0x0006 0x0007 0x0008 0x0009 typenameaddress inta0x0001 charc0x0006 void main(){ int a; a = 5; f(a); } void f(int c){ c = 4; }

9 Call by Address typenameaddress inta0x0001 int*ap0x0008 addressvalue 0x0001 0x0002 0x0003 0x0004 0x0005 0x0006 0x0007 0x0008 0x0009 void main(){ int a = 5; f(a); } void f(int* ap){ *ap = 4; ap = ap + 1; } x x

10 Call by Reference addressvalue 0x0001 0x0002 0x0003 0x0004 0x0005 0x0006 0x0007 0x0008 0x0009 typenameaddress Aa0x0001 a.a0x0001 Aao0x0001 ao.a0x void main(){ A a; a.a = 5; f(a); } void f(A& ao){ ao.a = 4; }

11 Try it!!! Please explain that what happen in the following code. Please write a sample code to prove your mind. A a1,a2; a1.a = 5; a2 = a1; :