Presentation is loading. Please wait.

Presentation is loading. Please wait.

More About Data Types & Functions

Similar presentations


Presentation on theme: "More About Data Types & Functions"— Presentation transcript:

1 More About Data Types & Functions

2 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"

3 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

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

5 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

6 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

7 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????

8 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

9 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

10 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

11 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!!!!!!

12 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


Download ppt "More About Data Types & Functions"

Similar presentations


Ads by Google