Derived data types Dealing with data –Where the information is stored –What value is kept there –What kind of information is stored Address operator Pointers.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Advertisements

Copyright © 2003 Pearson Education, Inc. Slide 1.
1 Pointers and Strings Section 5.4, , Lecture 12.
For(int i = 1; i
Computer Science 1620 Math Library. Remember this program? suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to.
PHYS707: Special Topics C++ Lectures Lecture 3. Summary of Today’s lecture: 1.Functions (call-by-referencing) 2.Arrays 3.Pointers 4.More Arrays! 5.More.
 Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?
Factorial Preparatory Exercise #include using namespace std; double fact(double); int fact(int); int main(void) { const int n=20; ofstream my_file("results.txt");
Chapter 16 Exception Handling. What is Exception Handling? A method of handling errors that informs the user of the problem and prevents the program from.
Vectors, lists and queues

Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne
Exercise 2.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
File and I/O system calls int open(const char* path, int flags, mode_t modes) int creat(const char *path, mode_t mode) ssize_t read(int fd, void *buf,
第三次小考. #include using namespace std; int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout
Tinaliah, S. Kom.. * * * * * * * * * * * * * * * * * #include using namespace std; void main () { for (int i = 1; i
Triana Elizabeth, S.Kom. #include using namespace std; void main () { for (int i = 1; i
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
General Computer Science for Engineers CISC 106 Lecture 26 Dr. John Cavazos Computer and Information Sciences 04/24/2009.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
1 Writing a Good Program 8. Elementary Data Structure.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Dynamic memory allocation and Pointers Lecture 4.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
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.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
Understanding Polymorphism tMyn1 Understanding Polymorphism Polymorphism requires the use of derived classes. It always involves the use of a pointer to.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Revision on C++ Pointers TCP1201: 2013/2014. Pointer Basics  Why pointer is important? 1. Reference/Point to existing data without cloning the data.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0]
A First Book of C++ Chapter 12 Extending Your Classes.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
POINTERS AND MEMORY ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
#define #include<iostream> using namespace std; #define GO
Pointers & Arrays.
Pointers Psst… over there.
Polymorphism Lec
Pointer Data Type and Pointer Variables
Dynamic Memory Allocation Reference Variables
Pointers Psst… over there.
Variables with Memory Diagram
Array of objects.
Dynamic Memory A whole heap of fun….
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
Anatomy of a Function Part 1
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Array of objects.
Destructor CSCE 121.
C Programming Lecture-8 Pointers and Memory Management
Pointers & Arrays.
Pointers and dynamic objects
Pointers & Functions.
Pointer Data Type and Pointer Variables
Pointer Data Type and Pointer Variables
Introduction to Algorithms and Programming COMP151
Presentation transcript:

Derived data types Dealing with data –Where the information is stored –What value is kept there –What kind of information is stored Address operator Pointers Static and dynamic binding –new and delete oprators

Address operator // address.cpp _ using the & operator to find addresses #include using namespace std; int main() { int donuts = 6; double cups = 4.5; cout << "donuts value = " << donuts; cout << " and donuts address = " << &donuts << "\n"; // NOTE: you may need to use unsigned (&donuts) // and unsigned (&cups) cout << "cups value = " << cups; cout << " and cups address = " << &cups << "\n"; return 0; }

Pointer variable // pointer.cpp _ our first pointer variable #include using namespace std; int main() { int updates = 6; // declare a variable int * p_updates; // declare pointer to an int p_updates = &updates; // assign address of int to pointer // express values two ways cout << "Values: updates = " << updates; cout << ", *p_updates = " << *p_updates << "\n"; // express address two ways cout << "Addresses: &updates = " << &updates; cout << ", p_updates = " << p_updates << "\n"; // use pointer to change value *p_updates = *p_updates + 1; cout << "Now updates = " << updates << "\n"; return 0; }

Pointer variable

Declaring and initializing pointers

The new operator // use_new.cpp _ using the new operator #include using namespace std; int main() { int * pi = new int; // allocate space for an int *pi = 1001; // store a value there cout << "int "; cout << "value = " << *pi << ": location = " << pi << "\n"; double * pd = new double; // allocate space for a double *pd = ; // store a double there cout << "double "; cout << "value = " << *pd << ": location = " << pd << "\n"; cout << "size of pi = " << sizeof pi; cout << ": size of *pi = " << sizeof *pi << "\n"; cout << "size of pd = " << sizeof pd; cout << ": size of *pd = " << sizeof *pd << "\n"; return 0; }

The delete operator Freeing memory with delete int * ps = new int;// allocate memory....// use the memory delete ps;// free memory

The new operator for arrays // arraynew.cpp _ using the new operator for arrays #include using namespace std; int main() { double * p3 = new double [3]; // space for 3 doubles p3[0] = 0.2; // treat p3 like an array name p3[1] = 0.5; p3[2] = 0.8; cout << "p3[1] is " << p3[1] << ".\n"; p3 = p3 + 1; // increment the pointer cout << "Now p3[0] is " << p3[0] << " and "; cout << "p3[1] is " << p3[1] << ".\n"; p3 = p3 - 1; // point back to beginning delete [] p3; // free the memory return 0; }

Pointer arithmetic // addpntrs.cpp -- pointer addition #include //using namespace std; int main() { double wages[3] = { , , }; short stacks[3] = {3, 2, 1}; // Here are two ways to get the address of an array double * pw = wages; // name of an array = address short * ps = &stacks[0]; // or use address operator // with array element cout << "pw = " << pw << ", *pw = " << *pw << "\n"; pw = pw + 1; cout << "add 1 to the pw pointer:\n"; cout << "pw = " << pw << ", *pw = " << *pw << \n\n"; cout << "ps = " << ps << ", *ps = " << *ps << "\n"; ps = ps + 1; cout << "add 1 to the ps pointer:\n"; cout << "ps = " << ps << ", *ps = " << *ps << "\n\n"; cout << "access two elements with array notation\n"; cout << stacks[0] << " " << stacks[1] << "\n"; cout << "access two elements with pointer notation\n"; cout << *stacks<< " "<<*(stacks +1) << "\n"; cout << sizeof wages << " = size of wages array\n"; cout << sizeof pw << " = size of pw pointer\n"; return 0; }

The new operator for structures // newstrct.cpp _ using new with a structure #include using namespace std; struct inflatable // structure template { char name[20]; float volume; double price; }; int main() { inflatable * ps = new inflatable; // allot structure space cout << "Enter name of inflatable item: "; cin.get(ps->name, 20); // method 1 for member access cout << "Enter volume in cubic feet: "; cin >> (*ps).volume; // method 2 for member access cout << "Enter price: $"; cin >> ps->price; cout << "Name: " << (*ps).name << "\n"; // method 2 cout volume << " cubic feet\n"; cout price << "\n"; // method 1 return 0; }

Local and external structure declarations