Introduction to C++ Programming Lesson Plan Knowledge of simple C++ program Write simple input/output statements Knowledge of data types, variable declaration, arithmetic operators Simple C++ Program by example /* This is HelloWorld program in C++ */ #include <iostream> using namespace std; int main() { cout << "Hello World!\n This is my first C++ program\n"; return 0; } Simple C++ Program by example /* This is HelloWorld program in C++ */ #include <iostream> This is C++ using namespace std; comment int main() { cout << "Hello World!\n This is my first C++ program\n"; return 0; } C++ comments Comments: Must-have, to explain what a programmer is doing while coding Single line comments: // this is my comment Multiple line comments: /* This is my comment This also is my comment */ Simple C++ Program by example /* This is HelloWorld program in C++ */ #include <iostream> This is C++ using namespace std; Preprocessor directive int main() { cout << "Hello World!\n This is my first C++ program\n"; return 0; } 1. Comment out #include <iostream> from your hello.cpp 2. Save and Compile it 3. Describe the error Simple C++ by example Preprocessor directive: This is processed by the preprocessor before the program is compiled. Notifies the preprocessor to include in the program the content of corresponding header file from C++ header library (i.e *.h file) Simple C++ Program by example /* This is HelloWorld program in C++ */ #include <iostream> Using using namespace std; declarations int main() { cout << "Hello World!\n This is my first C++ program\n"; 0; out using std::cout from your hello.cpp 1.return Comment }2. Save and Compile it 3. Describe the error 4. Change cout to be std:: cout, save and compile Simple C++ Program by example Using declarations eliminates the needs to repeat std::<prefix> So we use: using std::cout; // for program uses cout using std::cin; // for program uses cin using std::endl; // for program uses endl; …. Or we can use: using namespace std; Simple C++ Program by example /* This is HelloWorld program in C++ */ #include <iostream> using namespace std; int main() { function in is my first C++ program\n"; cout << "HelloMain World!\n This C++ return 0; } Simple C++ Program by example • Syntax: int main() { } • Main function is a part of every C++ Program • Exactly one function in every program must be named main • int: means return an integer (in this context only) 1. Rename main to Main in your hello.cpp 2. Save and Compile it 3. Describe the error Simple C++ Program by example /* This is HelloWorld program in C++ */ #include <iostream> using namespace std; int main() { cout << "Hello World!\n This is my first C++ program\n"; return 0; } C++ statement Simple C++ Program by example Every C++ statement must end with ; Preprocessor directives do not end with ; << operator: stream insertion operator Insert the value on the right to the output stream \n (escape sequence): new line Simple C++ Program by example /* This is HelloWorld program in C++ */ #include <iostream> using namespace std; int main() { cout << "Hello World!\n This is my first C++ program\n"; return 0; Return } statement to exit a function Steps to develop a C++ Program Editor Preprocessor Compiler Linker “On-the-fly” questions /* This is a valid C++ comment /* True b. False a. “On-the-fly” questions /* This is a valid C++ comment /* True b. False a. “On-the-fly” question Every C++ statement ends with a(n): a. } b. { c. ; d. return “On-the-fly” question Every C++ statement ends with a(n): a. } b. { c. ; d. return “On-the-fly” question Every C++ program begins execution at the function a. Main b. MAIN c. static main d. main “On-the-fly” question Every C++ program begins execution at the function a. Main b. MAIN c. static main d. main Variable declaration Syntax: <data type> <variable_name>; If more than one variable has the same data type: <data type> <name1>, <name2>..; Fundamental numerical data types Name char short int (short) Description Size* Character or small integer. 1byte signed: -128 to 127 unsigned: 0 to 255 Short Integer. 2bytes signed: -32768 to 32767 unsigned: 0 to 65535 int Integer. 4bytes long int (long) Long integer. 4bytes Floating point number. 4bytes float double long double Range* signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 +/- 3.4e +/- 38 (~7 digits) Double precision floating point number. 8bytes +/- 1.7e +/- 308 (~15 digits) Long double precision floating point number. +/- 1.7e +/- 308 (~15 digits) 8bytes Variable names It must be a legal identifier. It must not be a keyword, a boolean literal (true or false), or the reserved word. It must be unique within its scope. Identifier A valid identifier is a sequence of one or more letters, digits or underscore characters (_). Identifiers may only begin with a letter, or _. Keyword: http://www.cplusplus.com/doc/tutorial/variables/ C++ is case sensitive language Example int firstNumber; int secondNumber; int sum; All variables in C++ MUST be declared before being used int firstNumber, secondNumber, sum; Reading a value from keyboard Example: cin >> firstNumber; A cin statement uses the input stream object cin (of namespace std) and the stream extraction operator, >>, to obtain a value from the keyboard. Usually you will need a prompt it directs the user to take a specific action before putting cin statement Assignment statement <variable name> = <expression>; Example: sum = firstNumber + secondNumber; Or sum = 6+7; Arithmetic Expression Any expression involving numerical value is called arithmetic expression For example: x+15 y /16 Arithmetic operators Addition: + x+y Subtraction: x-y Multiplication: * x*y Division: / x/y Modulo: % x%y Arithmetic Most programs perform arithmetic calculations. The asterisk (*) indicates multiplication. The percent sign (%) is the modulus operator: C++ provides the modulus operator, %, that yields the remainder after integer division. The modulus operator can be used only with integer operands. Integer division (i.e., where both the numerator and the denominator are integers) yields an integer quotient. Any fractional part in integer division is discarded (i.e., truncated)—no rounding occurs. Arithmetic C++ applies the operators in arithmetic expressions in a precise sequence determined by the following rules of operator precedence, which are generally the same as those followed in algebra. Arithmetic There is no arithmetic operator for exponentiation in C++, so x2 is represented as x * x. As in algebra, it’s acceptable to place unnecessary parentheses in an expression to make the expression clearer. These are called redundant parentheses. Constant and variables Constant: Value it contains doesn’t change Two ways to declare a constant in C++: #define <constant name> <value>; // using preprocessor directives const <datatype> <constant name>; // inside the program Variables: Value it contains may vary int sum; sum =0; sum = firstNumber + secondNumber; Operator << Using multiple stream insertion operators (<<) in a single statement is referred to as concatenating, chaining or cascading stream insertion operations. Calculations can also be performed in output statements. Memory Concepts Variable names such as firstNumber, secondNumber and sum actually correspond to locations in the computer’s memory. Every variable has a name, a type, a size and a value. When a value is placed in a memory location, the value overwrites the previous value in that location; thus, placing a new value into a memory location is said to be destructive. © 1 9 9 2 2 0 1 0 b y P e a r s o n E d u c a