0% found this document useful (0 votes)
17 views

Chap 2 (C++ Programming Basics)

Programming lecture#2

Uploaded by

wahabkhalid245
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Chap 2 (C++ Programming Basics)

Programming lecture#2

Uploaded by

wahabkhalid245
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 58

Programming Fundamentals

(C++ Programming Basics)

Muhammad Ali
Lecture Outline
Getting Started

Basic Program Construction

Output Using cout

Directives

Comments

Integer Variables
Lecture Outline
 Input with cin

 Floating Point Types

 Type bool

 The setw Manipulator

 Variable Type Summary

 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”

The ‘\n’ character at the end of the string constant is


an example of an escape sequence.
Preprocessor Directives
#include <iostream>

It’s called a preprocessor directive


Header Files
In the FIRST example, the preprocessor directive
#include tells the compiler to add the source file
IOSTREAM to the FIRST.CPP source file before
compiling.

WHY?
The using Directive
using namespace std;

std::cout << “Every age has a language of its own.”;


Comment Syntax
// comments.cpp
// demonstrates comments

#include <iostream> //preprocessor directive


using namespace std; //”using” directive

int main() //function name “main”


{ //start function body
cout << “Every age has a language of its own\n”;

//statement
return 0; //statement

} //end function body


Old Style Comments
Method 1
/* this is an old-style comment */

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 setw Manipulator


Output

The setw Manipulator


Basic Variable Types
Unsigned Variable Type
Variable Overflow Output
Automatic Conversion

 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.

 The lower-type variable is converted to the type of the higher-type variable.


Output

Type Conversion
Automatic Conversion
Casts
 In C++ the term applies to data conversions specified by the programmer as opposed to the
automatic data conversions.

 Casts are also called type casts.

 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.

 There are several kinds of casts in Standard C++:


 Static casts,.
 Dynamic casts.
 Reinterpret casts,
 Const casts.

 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

The decrement operator “--” , behaves very much like


the increment operator, except that it subtracts 1 from
its operand.

It too can be used in both prefix and postfix forms.


Library Functions
Two Ways to Use #include
Header Files and Library Files

You might also like