Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 192 Lecture 3 Winter 2003 December 5, 2003 Dr. Shafay Shamail.

Similar presentations


Presentation on theme: "CS 192 Lecture 3 Winter 2003 December 5, 2003 Dr. Shafay Shamail."— Presentation transcript:

1 CS 192 Lecture 3 Winter 2003 December 5, 2003 Dr. Shafay Shamail

2 C++ Keywords asm dynamic_cast new template auto else operator this bool enum private throw break extern protected true case false public try catch float register typedef char for reinterpret_cast typeid class friend return union const goto short unsigned const_cast if signed using continue inline sizeof virtual default int static void delete long static_cast volatile do mutable struct wchar_t double namespace switch while

3 Basic Data Types TypeBit WidthRange char8-128 to 127 int16-32768 to 32767 float323.4E-38 to 3.4E+38 double641.7E-308 to 1.7E+308 void-valueless

4 A number code used by C++ to store characters internally (it’s easy for a computer to store numbers) Each character corresponds to a binary code Most commonly used binary code is ASCII (American Standard Code for Information Interchange) CharacterASCII CodeInteger Equivalent %010010137 3011001151 A100000165 a110000197 b110001098 c110001199 Larger character sets e.g. Unicode Exercise: Use char as a small int in a program

5 Type Modifiers signed unsigned short long

6 auto private register const protected static extern public virtual friend volatile mutable Access Modifiers

7 break forwhile case goto continue if default return else switch Control Keywords

8 CS 192 Lecture 3 Winter 2003 December 5, 2003 Dr. Shafay Shamail

9 Functions Building blocks of a C++ program Each function has a name, which is used to call the function; functions call each other You will write your own functions, e.g. main(), and also use pre-written functions from standard library

10 /* This program contains two functions: main() and myfunc(). */ #include void myfunc(); // myfunc's Protoype int main() { cout << "In main()"; myfunc(); // call myfunc() cout << "Back in main()"; return 0; } void myfunc() // myfunc’s Definition { cout << " Inside myfunc() "; }

11 Function Prototype void myfunc(); // myfunc's Protoype Like variable declaration; tells the compiler about the return type of the function and the number and type of parameters it needs from the calling function: return_type FunctionName (parameter list); So, place prototypes before main() main() is predefined in C++ and does not need a prototype Can’t the compiler look ahead and get definitions? Prototype can be omitted if whole function placed before it is called; is that a good practice?

12 Returning a Value // Returning a value. #include int mul(int x, int y); // mul()'s prototype int main() { int answer; answer = mul(10, 11); // assign return value cout << "The answer is " << answer; return 0; } // This function returns a value. int mul(int x, int y) { return x * y; // return product of x and y }

13 Library Functions Pre-written functions in libraries that you can use Just include the proper header file Compiler gets prototype from the header file and searches appropriate libraries itself to get function definition e.g. math library has mathematical functions in it #include #include //or int main() { cout << sqrt(9.0); return 0; }


Download ppt "CS 192 Lecture 3 Winter 2003 December 5, 2003 Dr. Shafay Shamail."

Similar presentations


Ads by Google