More About Data Types & Functions. General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements.

Slides:



Advertisements
Similar presentations
Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
Advertisements

Introduction to Programming Lecture 39. Copy Constructor.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Chapter 10.
Objectives Learn about objects and reference variables Explore how to use predefined methods in a program.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Chapter 11 Structure. 2 Objectives You should be able to describe: Structures Arrays of Structures Structures as Function Arguments Dynamic Structure.
A function (procedure) code to perform a task (carry out an algorithm) Return_type Func(parameters) ---- Func (arguments) --- Return value.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
In Addition... To the string class from the standard library accessed by #include C++ also has another library of string functions for C strings that can.
1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real.
C Tokens Identifiers Keywords Constants Operators Special symbols.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
1 C++ Syntax and Semantics, and the Program Development Process.
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems.
C++ Programming: Basic Elements of C++.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Dynamic memory allocation and Pointers Lecture 4.
More C++ Features True object initialisation
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
A FIRST BOOK OF C++ CHAPTER 16 DATA STRUCTURES. OBJECTIVES In this chapter, you will learn about: Single Structures Arrays of Structures Structures as.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
1 Object-Oriented Programming Using C++ A tutorial for pointers.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
1 A Simple “Hello World” Example #include // input-output library using namespace std; int main() // function main { cout
Functions CMSC 202. Topics Covered Function review More on variable scope Call-by-reference parameters Call-by-value parameters Function overloading Default.
C++ Functions A bit of review (things we’ve covered so far)
Pointers and Dynamic Arrays
Student Book An Introduction
DATA HANDLING.
Enumeration used to make a program more readable
More About Data Types & Functions
Presentation transcript:

More About Data Types & Functions

General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements (declarations) – public & private class data global variables & global structs external variables functions functions cannot be "nested"

Class Program Structure #include statements #include for class header // declarations member implementation functions (methods) Class header is used in TWO places: – the class definition file – user code file (where you solve your problem) – usually only contains prototypes, NO CODE – ties the definitions to the usage

a function (procedure) code to perform a task (implement an algorithm) Return_type Fname(parameters) User program ---- Func (arguments) --- Return value

Parameters Java parameters are either – primitive types - parameter is a copy of the argument (argument changes NOT copied back to caller) – objects - parameter is an alias for the argument – (argument changes ARE copied back to caller) C++ parameters are either – value parameters (unchangeable) – reference parameters (changeable) – constant reference parameters (unchangeable) – array parameters

C-style strings -1 Often need to work with collections of characters (like a person's name) C has a "string" (called a C-string) char mytext[20]; //20 characters, mytext[3]='a'; //note single quotes!!! How long is mytext? Compiler says 20 Runtime code DOES NOT KNOW!! So how do I copy the string???? 6

C-style strings - 2 C runtime functions ASSUME a delimiter – (0x00) – Inserted by YOU at the end of the USABLE part mytext[19]=0x00; // force real end of string mytext[0]='a'; mytext[1]='b'; mytext[2]=0x00; mytext[3]='m'; printf("%s",mytext); Displays the string ab because of the 0x00 0x00 STOPS the code from looking further 7

C++ strings early versions of C++ used “C-style" strings – array of char with a special character (null character) marking the end – or for "old" C-style strings C++ standard library provides the class string – #include – uses C++ string class, not C-style strings

C++ strings #include #include // no ".h" using namespace std; // tells compiler what names to use string a,b; // initially empty (NOT blanks), no specific length a="hello "; b="worldclass users"; cout<<a+b; // result is "hello worldclass users" format control is available too add #include to get: – endl, left, right, skipws, width, setw, noskipws, setprecision – endl adds a proper end-of-line

Pointers - refresher a pointer is a memory address (points to some data) lets you refer to data without the data's name int x[5] ; // this takes up 20 bytes 5*(4 bytes/int) int *z; // z is a pointer to an int and is 4 bytes int (*y)[5];// uses only 4 bytes y is a pointer to an array of 5 ints y=&x;// y now points at X ( ≠ X) z=&x[3]; // z now points to x[3] (has x's address) // z does not EQUAL the value in X[3] int *P[8]; // array of 8 "pointers to int's" (8*4bytes) NOT the integers themselves!!!!!! 10

Dynamic allocation Often need to make space at runtime – Size/amount of data not pre-defined – Data may need to be ordered by some rule (e.g.; "<") Complx * p; // defines ONLY the POINTER, p // p "points" to an object, not the data // the object contains the data p=new Complx(3.5, 5.6); //reserves RAM, p points to the object // equivalent to: p-> j

C++ Parameters C++ parameters are either – value - (type param_name) copy of the argument, argument cannot be modified – reference - (type & param_name) alias (pointer) to argument, argument is modified if parameter is modified – constant reference - (const type & param_name) alias (pointer) to the argument, but compiler disallows assignment to parameter