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> using namespace std; int main() { cout << "Hello World!\n This is my first C++ program\n"; return 0; } This is C++ comment
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> using namespace std; int main() { cout << "Hello World!\n This is my first C++ program\n"; return 0; } This is C++ Preprocessor directive 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 namespace std; int main() { cout << "Hello World!\n This is my first C++ program\n"; return 0; } Using declarations 1. Comment out using std::cout from your hello.cpp 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() { cout << "Hello World!\n This is my first C++ program\n"; return 0; } Main function in C++
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 False
“On-the-fly” questions /* This is a valid C++ comment /* True False
“On-the-fly” question Every C++ statement ends with a(n): } { ; return
“On-the-fly” question Every C++ statement ends with a(n): } { ; return
“On-the-fly” question Every C++ program begins execution at the function Main MAIN static main main
“On-the-fly” question Every C++ program begins execution at the function Main MAIN static main 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
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; int firstNumber, secondNumber, sum; All variables in C++ MUST be declared before being used
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: + Subtraction: - Multiplication: * 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 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.
©1992-2010 by Pearson Education, Inc. All Rights Reserved.