Chp2_C++ Programming Basics
Chp2_C++ Programming Basics
Chapter 2
Programming in C++
\b Backspace
\f Form feed
\n New line
\r Carriage return
\t Horizontal tab
\\ Backslash
\’ Single quote
\” Double quote
Computer
▪ cout<<“Computer\nScience”; →
Science
• is a symbolic name
• can be given a variety of values
• is a memory location whose contents can be changed.
• is used to stores some data in a C++ program
Data
double,
int float long bool char string
etc…
▪ Grade ▪ My Name
▪ myName ▪ 3rd_Number
▪ movie_name ▪ first-name
▪ a_123 ▪ Grade(Test)
▪ abc ▪ short
▪ _temp ▪ 123
• Short - 2 bytes
- Both have fixed sizes no matter what system is used.
• int var1=10; in this example, the value of variable is assigned within the
program which is making the program to restrict to this value only.
• We have a function called “cin” function which provides opportunity to
make more generalize program.
• It provides an option to the users to enter the values at run time from
the keyboard.
• cin>>variablename;
• For example , cin>>var1;
Faculty of Computing (PU-Maubin) 21
Input with cin
Example:
Example:
addNumber.cpp
#include<iostream.h>
using namespace std;
int main()
{
int a,b,c;
cout<<“Enter Value for a ”;
cin>>a;
cout<<“Enter Value for b ”;
cin>>b;
c = a + b;
cout << “ Sum = ” << c;
return 0;
}
• # define PI 3.14159
#define Directive
▪ Constants can also be specified using the preprocessor directive
#define
▪ Can’t specify the data type of the constant using #define
For example:
#define PI 3.14159
setw manipulator
#include<iostream>
#include<iomanip> //used for setw function
using namespace std;
int main()
{
cout <<setw(10)<< "City" << setw(20)<<"Nation"<< endl;
cout<<setw(10)<<"Yangon"<<setw(20)<<"Myanmar"<<endl;
Explanation
cout<<setw(10)<<"Seoul"<<setw(20)<<"Korea"<<endl;
●●●●●●City●●●●●●●●●●●●●●Nation
return 0;
●●●●Yangon●●●●●●●●●●●●Myanmar
} ●●●●●Seoul●●●●●●●●●●●●●●●Korea
+ addition ans = 7 + 3
- subtraction ans = 7 - 3
* multiplication ans = 7 * 3
/ division ans = 7 / 3
% modulus ans = 7 % 3
return 0;
}
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
cout<<ans<<endl;
47
return 0; Faculty of Computing (PU-Maubin)
Increment Operators
Increment operators
++ //increment operator
Decrement operators
-- //decrement operator
▪ Both increment and decrement operators are used on single operand or variable, so it is
also called as unary operator.
▪ The decrement (--)operator works like the ++operator except it decreases the value by 1.
#include <iostream>
using namespace std;
int main() var1 var2 Output
{ 5 5 5
int var1 = 5, var2 = 5; 6 6 7
cout << var1++ << endl; 7 6
5
cout << ++var1 << endl; 6
4
cout << ++var2 << endl; 5
5
cout << var2 <<endl;
cout<< - -var2 <<endl;
cout<< var2-- <<endl;
return 0;
}
▪ consists of one or more operands, zero or more operators to compute a value and
produces some value.
▪ (x/y) – z
▪ alpha +12
= 40 + 320 / 3 + 7 – 5 % 3
= 40 + 106 + 7 – 5 % 3
= 40 + 106 + 7 – 2
= 146 + 7 – 2
= 153 – 2
= 151
▪ Implicit conversion is used when an expression contains variables of more than one
type.
▪ All the data types present in the expression are converted to data type of the
variable with the largest data type.
▪ Explicit conversion also called type casting is user defined and the user can type
cast the result to make it of a particular data type.
▪ Static Cast
▪ Dynamic Cast
▪ Const Cast
▪ Reinterpret Cast
int b;
// using cast operator
b = static_cast<int>(f);
cout << "b = "<< b;
}
▪ Many functions and symbols needed to run a C++ program are provided as
collection of libraries.
▪ Every library has a name and is referred to by a header file.
▪ Each header file contains file access, mathematical computations, and data
conversion, among other its information for a particular group of library
functions.
▪ iostream header file →I/O functions and objects, including cout and cin.
▪ iomanip header file → changes the field width of output.
▪ math header file → information for mathematics functions such as sqrt().
▪ string header file →string functions such as strcpy()and so on.