Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "More About Data Types & Functions. General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements."— Presentation transcript:

1 More About Data Types & Functions

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

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

5 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

6 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, 0..19 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

7 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

8 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

9 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

10 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

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

12 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


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

Similar presentations


Ads by Google