Chap 2 (C++ Programming Basics)
Chap 2 (C++ Programming Basics)
Muhammad Ali
Lecture Outline
Getting Started
Directives
Comments
Integer Variables
Lecture Outline
Input with cin
Type bool
Type Conversion
Arithmetic Operators
Library Functions
Basic Program Construction First.cpp
#include <iostream>
using namespace std; single function called main()
int main()
{
cout << “Every age has a language of its own\n”;
return 0;
}
OUTPUT
Every age has a language of its own
Always Start with main()
Program Statements
cout << “Every age has a language of its own\n”;
return 0;
Whitespace
Output Using cout
cout << “Every age has a language of its own\n”;
String Constants
“Every age has a language of its own\n”
WHY?
The using Directive
using namespace std;
//statement
return 0; //statement
Method 2
/* this
is a
potentially
very long
multiline
comment
*/
Defining Integer Variables
Output
Character Variable Char
Common Escape Sequence
Statement
Output
Defining Character Variable
Output
Input With Cin
cin>> variable_name
Input With Cin
Output
Floating Point Types
float
double
long double
Type Float
Output
Float Variables
The const Qualifier
#define Directive
Output
The arithmetic operators such as + and * like to operate on two operands of the
same type.
When two operands of different types are encountered in the same expression.
Type Conversion
Automatic Conversion
Casts
In C++ the term applies to data conversions specified by the programmer as opposed to the
automatic data conversions.
It is Used to convert a value from one type to another in a situation where the compiler will not
do it automatically or without complaining.
Here’s a statement that uses a C++ cast to change a variable of type int into a variable of type
char:
Constant Cast Example
Output
Programming Fundamentals
(C++ Programming Basics)
Arithmetic Operator
Remainder Operator
Arithmetic Assignment Operators
Airthmatic Assignment Example
Increment Operators
Prefix and Postfix
Prefix
Postfix
Prefix and Postfix(Example)
Prefix and Postfix(Example)
The Decrement (--) Operator