0% found this document useful (0 votes)
12 views54 pages

c++ note

This document provides an overview of C++ programming concepts including simple program structure, data types, variable declaration, and control flow structures such as loops and conditionals. It covers essential topics like namespaces, functions, type conversion, and function overloading. Additionally, it includes examples of various programming constructs and their usage in C++.

Uploaded by

May Zin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views54 pages

c++ note

This document provides an overview of C++ programming concepts including simple program structure, data types, variable declaration, and control flow structures such as loops and conditionals. It covers essential topics like namespaces, functions, type conversion, and function overloading. Additionally, it includes examples of various programming constructs and their usage in C++.

Uploaded by

May Zin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 54

C++ Note

Simple Program
Main Function
Main Function
Comments
Header

 #include <iostream>
 Allow the program to have input (cin) and output (cout) streams.

https://docs.microsoft.com/en-us/cpp/standard-
library/cmath?view=msvc-170
Namespaces

 Namespace std;
 Is called using a directive.
End of line manuipulator (endl)

 Cout << “\n”;


 Cout << endl;
Variable declaration

 Declare statements (create a variable)


 Int carrots;
 Assignment statements
 Carrots = 25;
Concatenation
C++ Data Types
Built-in Data Types – Primitive types
C++ Data Types

 Built-in Data Types – Primitive types


 Signed and Unsigned Data Types
 A signed data type permits storing negative values in addition to 0 and
positive values, so int is a signed data type.
 An unsigned data type provides for only non-negative values (that is, 0 and
positive values).
 Some applications require only unsigned numerical values. For example, many date
applications store dates in the numerical form yearmonthday (storing 12/25/2012 as
20121225, for example) and are concerned only with dates after 0 CE.
 For these applications, which never require a negative value, an
unsigned data type can be used.
C++ Data Types
C++ Data Types
C++ Data Types
C++ Data Types
C++ Data Types
C++ Type Modifiers
Example programs
CH-3: Assignment Operations

 variable = expression;
CH-3: Assignment Operations
sum += 96;
 variable = expression; sum -= 97;
sum *= 7;
sum /= 9;
sum %= 4;
CH-3: Counting Operations (Counter)
 variable = expression;

i = i + 1; i += 1;
n = n + 1; n += 1;
count = count + count += 1;
1; j += 2;
j = j + 2; m += 2;
m = m + 2; kk += 3;
kk = kk + 3;

i ++;
n ++;
count ++;
j ++;
m ++;
kk ++;
CH-3: Example Program
CH-3: Manipulators
Flags
 The flag refers to an item, such as a variable or an argument, that sets
a condition usually considered active or nonactive.
Using Mathematical Library Functions

 #include <cmath>
Degree to Radian
 Persian calendar = 360 days (The sun follows the ecliptic path over the
year)
 One degree each day
Degree to Radian
 Persian calendar = 360 days (The sun follows the ecliptic path over the
year)
 One degree each day

360 degree = 2𝝅 radian


1 degree = 𝝅/180 radian

𝝅/180 radian
180/ 𝝅
1 degree = => radian = degree *

1 radian = 180/ 𝝅 degree => degree = radian *


Cast – Type Conversion

 Cast => (type) expression; for example: c = (int) a;


CH4: Selection Structures

Single Statement Compound Statements

 If else  If else
  if “I am hungry”
if “I am hungry”
{
“I will eat” ;
“I will go to restaurant.” ;
 else
“I will eat.”;
“I won’t eat”; “I will go home after that.”
}
 Else
{
“I won’t eat.”;
“I won’t drink.”;
}
CH4: Selection Structures
 If else
 if 5>4
“do something” ;
 else
“do nothing”;
CH4: Logical Operators
Example
CH4: Selection Structures

“If - else if – else” Statement “Nested if” Statement

 If - else if - else  Nested if


 if  If
 Else if  If

 Else if  else

 Else

 If
 If
 else

 else
Nested if
If - else if - else
CH4: Selection Structures
“switch” Statement

 switch ( expression )
{
case value 1:
statement1;
etc…
break;
case value 2:
statement2;
etc…
break;
default:
statement;
}
If and Switch

“if” Statement “switch” Statement


CH5: Repetition Statements
“while” Statement “for” Statements
count = 1; for (i = 1; i <= 10;
while (count <= 10) i++)
{ {
cout << count << " "; cout << i << " ";
count++;
}
}

“nested while” Statement “nested for” Statement


while (count <= 10) for (i = 1; i <= 10; i++)
{ {
while ( count < = 2) for (j = 1; j <= 10; j++)
{ {
// ….. ; // ….. ;
} }
} }
Examples
Example

y = 10x2 + 3x – 2

while (x <= 3)
for ( x = 1; x <= 3; x++)
{
{ y = (10 * x * x)
+ (3 * x) – 2;
y = (10 * x * x) + (3 * x) – 2;
x ++;
} }

 Output:
x=1 => y = 10+3-2 = 11
x=2 => y = 40+6-2 = 44
x=3 => y = 90+9-2 = 97
X=4 (condition false)
Nested loop

Output

*****
*****
*****
Random number

rand() => to generate the random number

srand( time(NULL) ) function => use system time

time() – reads the computer’s internal clock in seconds, to initialize


the random number.
CH5: Repetition Statements

“do while” Statement

do
{
statement 1;
etc….
}
while (count != 0);
CH6: Function

 Main Function – door to program


 int main()
{
return 0;
}

 Other Function (module) – to be called from main() function.


 int findMax (int a, int b)
{
Statements to find maximum number between a and b.
return 0;
}
CH6: Function

 Main Function – door to program


 void main()
{
return 0;
}

 Other Function (module) – to be called from main() function.


 void findMax (int a, int b)
{
Statements to find maximum number between a and b.
return 0;
}
CH6: Function Overloading

 void cdabs (int x)  void cdabs (float x)

{ {

if (x < 0) if (x < 0)

X = -x; X = -x;

cout << “The absolute value is “ << cout << “The absolute value is “ <<
x << endl; x << endl;

} }

 void cdabs (double x)


{
if (x < 0)
X = -x;
cout << “The absolute value is “ <<
x << endl;
}
Inline Function
Passing by Values and
Passing by References
Passing by Values and
Passing by References
Global Variable

You might also like