Download presentation
1
Introduction to C++ Programming
2
Lesson Plan Knowledge of simple C++ program
Write simple input/output statements Knowledge of data types, variable declaration, arithmetic operators
3
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; }
4
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
5
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 */
6
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
7
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)
8
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
9
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;
10
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++
11
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
12
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
13
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
14
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
15
Steps to develop a C++ Program
Editor Preprocessor Compiler Linker
16
“On-the-fly” questions
/* This is a valid C++ comment /* True False
17
“On-the-fly” questions
/* This is a valid C++ comment /* True False
18
“On-the-fly” question
Every C++ statement ends with a(n): } { ; return
19
“On-the-fly” question
Every C++ statement ends with a(n): } { ; return
20
“On-the-fly” question
Every C++ program begins execution at the function Main MAIN static main main
21
“On-the-fly” question
Every C++ program begins execution at the function Main MAIN static main main
22
Variable declaration Syntax: <data type> <variable_name>; If more than one variable has the same data type: <data type> <name1>, <name2>..;
23
Fundamental numerical data types
24
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.
25
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: C++ is case sensitive language
26
Example int firstNumber; int secondNumber; int sum; int firstNumber, secondNumber, sum; All variables in C++ MUST be declared before being used
27
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
28
Assignment statement <variable name> = <expression>; Example: sum = firstNumber + secondNumber; Or sum = 6+7;
29
Arithmetic Expression
Any expression involving numerical value is called arithmetic expression For example: x+15 y /16
30
Arithmetic operators Addition: + Subtraction: - Multiplication: *
x+y Subtraction: - x-y Multiplication: * x*y Division: / x/y Modulo: % x%y
31
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.
32
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.
33
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.
34
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;
35
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.
36
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.
37
©1992-2010 by Pearson Education, Inc. All Rights Reserved.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.