More About Data Types & Functions

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter 10.
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.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
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.
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.
Chapter 6: User-Defined Functions
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.
C++ Programming: Basic Elements of C++.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
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.
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.
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
More About Data Types & Functions. General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements.
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.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
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++
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Bill Tucker Austin Community College COSC 1315
Pointers and Dynamic Arrays
Computer Organization and Design Pointers, Arrays and Strings in C
Chapter Topics The Basics of a C++ Program Data Types
Chapter 7 Pointers and C-Strings
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Andy Wang Object Oriented Programming in C++ COP 3330
Java Primer 1: Types, Classes and Operators
Arrays Arrays exist in almost every computer language.
Basic Elements of C++.
Objectives Identify the built-in data types in C++
Introduction to C++.
FUNCTIONS IN C++.
Student Book An Introduction
C Basics.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
week 1 - Introduction Goals
DATA HANDLING.
Basic Elements of C++ Chapter 2.
Enumeration used to make a program more readable
7 Arrays.
2.1 Parts of a C++ Program.
Lists, Strings & Multi-dimensional arrays
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Pointers and Pointer-Based Strings
Week 9 – Lesson 1 Arrays – Character Strings
Chapter 3 Input output.
CPS120: Introduction to Computer Science
Engineering Problem Solving with C++, Etter
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Chapter 2: Introduction to C++.
7 Arrays.
Arrays Arrays A few types Structures of related data items
Computing and Statistical Data Analysis Lecture 5
COMS 261 Computer Science I
The C Language: Intro.
4.1 Introduction Arrays A few types Structures of related data items
Introduction to Classes and Objects
Presentation transcript:

More About Data Types & Functions

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

C++ 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) User program ---- Func (arguments) --- Return value Return_type Fname(parameters) code to perform a task (implement an algorithm)

Parameters Java parameters are either C and C++ 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 and C++ parameters are either value parameters (unchangeable) Reference (i.e.; pointer) parameters (changeable) constant reference parameters (unchangeable) array parameters

Using Parameters Argument (the thing passed by the caller) Parameter (the ALIAS for the thing being passed) syntax value - (type param_name) Myfcn(int x); copy of the argument, argument cannot be modified reference - (type & param_name) Myfcn(int &x); alias (pointer) to argument, argument is modified if parameter is modified constant reference - (const type & param_name) Myfcn(const int &x); alias (pointer) to the argument, but compiler disallows assignment to parameter

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

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

C++ strings Early versions of C++ used “C-style" strings array of char with a special character (null character) marking the end <string.h> or <cstring> for "old" C-style strings C++ standard library provides the class string #include <string> // note: no ".h" uses C++ string class, not C-style strings

C++ strings #include <iostream> #include <string> // no ".h" using namespace std; // tells compiler how to distinguish names string a,b; // initially empty (NOT blanks), no specific length a="hello world"; b="class users"; //Automatically sized to proper length. No NULL to insert. cout<<a+b; // result is "hello worldclass users" format control is available too add #include <iomanip> to get: endl, left, right, skipws, width, setw, noskipws, setprecision endl adds a proper end-of-line

Pointers – refresher (assumes a 32-bit machine) 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!!!!!!

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-> 3.5+5.6j