C++ chapter 2
-
Upload
shrirang-pinjarkar -
Category
Engineering
-
view
6 -
download
0
Transcript of C++ chapter 2
![Page 2: C++ chapter 2](https://reader035.fdocuments.us/reader035/viewer/2022081605/58e8d0e41a28abb3398b55ef/html5/thumbnails/2.jpg)
UNIT -2DATA TYPES ,
VARIABLES AND OPERATORS
![Page 3: C++ chapter 2](https://reader035.fdocuments.us/reader035/viewer/2022081605/58e8d0e41a28abb3398b55ef/html5/thumbnails/3.jpg)
OverviewObjectivesC++ data types, constant and variableC++ keywordsInput Output : cin, coutHands On!
![Page 4: C++ chapter 2](https://reader035.fdocuments.us/reader035/viewer/2022081605/58e8d0e41a28abb3398b55ef/html5/thumbnails/4.jpg)
DATA TYPE A data type determines the type of the
data that will be stored, usually, in the computer memory (RAM).
Type statements in C++ are used to allow the compiler to: reserve blocks of memory to store information give the reserved blocks of memory a symbolic
name so that the data contained in this block of memory can be manipulated by referring to this name in future C++ statements.
![Page 5: C++ chapter 2](https://reader035.fdocuments.us/reader035/viewer/2022081605/58e8d0e41a28abb3398b55ef/html5/thumbnails/5.jpg)
Data TypesC++ provides three fundamental data types:
- int (integers ) ex: 1 , -8 ,0 ,etc - float (decimal numbers) ex: 2.03 , -7.15 ,
0.0 , etc - char (character) ex: ‘a’ , ‘A’ , ‘1’, etc
0 is not float 0.0 is float
![Page 6: C++ chapter 2](https://reader035.fdocuments.us/reader035/viewer/2022081605/58e8d0e41a28abb3398b55ef/html5/thumbnails/6.jpg)
Name Description Size* Range*char Character 1byte signed: -128 to 127
unsigned: 0 to 255short int(short) Short Integer. 2bytes
signed: -32768 to 32767unsigned: 0 to 65535
int Integer. 2 or 4 bytes
signed: -2147483648 to 2147483647unsigned: 0 to 4294967295
long int(long) Long integer. 4bytes
signed: -2147483648 to 2147483647unsigned: 0 to 4294967295
boolBoolean value. It can take one of two values: true or false.
1byte true or false
float Floating point number. 4bytes +/- 3.4e +/- 38 (~7 digits)
double Double precision floating point number. 8bytes +/- 1.7e +/- 308 (~15
digits)long double Long double precision
floating point number. 8bytes +/- 1.7e +/- 308 (~15 digits)
![Page 7: C++ chapter 2](https://reader035.fdocuments.us/reader035/viewer/2022081605/58e8d0e41a28abb3398b55ef/html5/thumbnails/7.jpg)
Exercise What is the suitable data type for the following?
number of student in your class - unsigned int
your matrix elements - float
assignment marks for this subject - float
the distance to the moon (the distance to the moon is over 200,000 miles) - long double
last month's checking account balance - float /double
a counter used to count the number of lines in a text file - int
number of people living in Malaysia - long
the temperature used in a chemistry formula - float
![Page 8: C++ chapter 2](https://reader035.fdocuments.us/reader035/viewer/2022081605/58e8d0e41a28abb3398b55ef/html5/thumbnails/8.jpg)
Variables variable
a valid identifier whose value can change during the course of execution of a program
general form of the declarations: data-type variable_name;
example:int mass; double x, speed, dragForce;
![Page 9: C++ chapter 2](https://reader035.fdocuments.us/reader035/viewer/2022081605/58e8d0e41a28abb3398b55ef/html5/thumbnails/9.jpg)
Declaration of Variables when a variable is declared, you can initialize it in two alternative but equivalent ways
int mass = 22; or int mass; //(garbage value)mass = 22;
Declaration of string variableexample:
string name = “Mohamad”;
![Page 10: C++ chapter 2](https://reader035.fdocuments.us/reader035/viewer/2022081605/58e8d0e41a28abb3398b55ef/html5/thumbnails/10.jpg)
Variables// Declaration of variables
#include <iostream> using namespace std;
int main () { short x = 22, y = 11, z; z = x - y; cout << "z = " << z << endl; int p = 3; int q = x * y * z - 2 * p; cout << "q = " << q << endl; return 0; }
![Page 11: C++ chapter 2](https://reader035.fdocuments.us/reader035/viewer/2022081605/58e8d0e41a28abb3398b55ef/html5/thumbnails/11.jpg)
Exercise :Correct the following errors long Float x; long x; int code = three, int code = 3; const int array size; const int array_size;
Declare the following variable:Name Type Initial value
marks double Nonegrade char Aprice float 10.0num_1 int 5msg string Hello Worldresult bool true
![Page 12: C++ chapter 2](https://reader035.fdocuments.us/reader035/viewer/2022081605/58e8d0e41a28abb3398b55ef/html5/thumbnails/12.jpg)
Scope of Variablevariable can have either local or global scopescope (visibility) of local variables is limited to the block enclosed in braces ({ }) where they are declaredglobal variables are declared outside of all blocks and their scope are the entire program, i.e. the main body of the source code.
![Page 13: C++ chapter 2](https://reader035.fdocuments.us/reader035/viewer/2022081605/58e8d0e41a28abb3398b55ef/html5/thumbnails/13.jpg)
What is the Scope of A variable ?
Scope refers to the visibility of variables. In otherwords, which parts of your program can see or use it. Normally, every variable has a global scope. Once defined, every part of your program can access a variable.
![Page 14: C++ chapter 2](https://reader035.fdocuments.us/reader035/viewer/2022081605/58e8d0e41a28abb3398b55ef/html5/thumbnails/14.jpg)
A scope is a region of the program and broadly speaking there are three places, where variables can be
declared:
Inside a function or a block which is called local variables,In the definition of function parameters which is called formal parameters.Outside of all functions which is called global variables.
![Page 15: C++ chapter 2](https://reader035.fdocuments.us/reader035/viewer/2022081605/58e8d0e41a28abb3398b55ef/html5/thumbnails/15.jpg)
Operators in C++An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. C++ is rich in built-in operators and provides the following types of operators:Arithmetic OperatorsRelational OperatorsLogical OperatorsBitwise OperatorsAssignment Operators
![Page 16: C++ chapter 2](https://reader035.fdocuments.us/reader035/viewer/2022081605/58e8d0e41a28abb3398b55ef/html5/thumbnails/16.jpg)
Arithmetic Operators:Assume variable A holds 10 and variable B holds 20
Operator
Description Result
+ Adds two operands 30
- Subtracts second operand from first -10
* Multiplies both Operands 200
/ Divides numerator by De-numerator B/A=2
% Modulus Operator and reminder of after integer division
A%B=0
++ Increment operator , increases integer value by one
11 A++
-- Decrement Operator, Decreases Integer Value by One
9
![Page 17: C++ chapter 2](https://reader035.fdocuments.us/reader035/viewer/2022081605/58e8d0e41a28abb3398b55ef/html5/thumbnails/17.jpg)
7/8/2014
By Himanshu Kaushik | ApplicationDeveloper.in |
Himanshukaushik.in
main(){
int a = 21; int b = 10; int c ; c = a + b; cout << "Line 1 - Value of c is :" << c << endl ; c = a - b; cout << "Line 2 - Value of c is :" << c << endl ; c = a * b; cout << "Line 3 - Value of c is :" << c << endl ;
c = a / b; cout << "Line 4 - Value of c is :" << c << endl ; c = a % b; cout << "Line 5 - Value of c is :" << c << endl ; c = a++; cout << "Line 6 - Value of c is :" << c << endl ; c = a--; cout << "Line 7 - Value of c is :" << c << endl ; return 0;
}
![Page 18: C++ chapter 2](https://reader035.fdocuments.us/reader035/viewer/2022081605/58e8d0e41a28abb3398b55ef/html5/thumbnails/18.jpg)
Relational OperatorsAssume variable A holds 10 and variable holds 20
Operator
Description Example
== Checks if the Value of two Operands are Equal or not , if yes then condition becomes True
(A==B) is not true
!= Checks if the value of two operands are equal or not , if values are not equal then condition becomes true
(A!=B) is true
> Checks if the value of Left operand is greater than the value of right operand , if yes then condition becomes true,
(A>B) is not true
< , >= , <= are relationals operators
![Page 19: C++ chapter 2](https://reader035.fdocuments.us/reader035/viewer/2022081605/58e8d0e41a28abb3398b55ef/html5/thumbnails/19.jpg)
Bitwise OperatorsBitwise operator works on bits and perform bit-by-bit operation.
P Q P & Q P|Q P^Q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
![Page 20: C++ chapter 2](https://reader035.fdocuments.us/reader035/viewer/2022081605/58e8d0e41a28abb3398b55ef/html5/thumbnails/20.jpg)
if A = 60; and B = 13Assume if A = 60; and B = 13; now in binary format they will be as follows:
A = 0011 1100 B = 0000 1101 A&B = 0000 1100 A|B = 0011 1101 A^B = 0011 0001 ~A = 1100 0011
![Page 21: C++ chapter 2](https://reader035.fdocuments.us/reader035/viewer/2022081605/58e8d0e41a28abb3398b55ef/html5/thumbnails/21.jpg)
Operator
Description Result
& Binary AND Operator copies a bit to the result if it exists in both operands.
(A & B) will give 12 which is 0000 1100
| Binary OR Operator copies a bit if it exists in either operand.
(A | B) will give 61 which is 0011 1101
^ Binary XOR Operator copies the bit if it is set in one operand but not both.
(A ^ B) will give 49 which is 0011 0001
![Page 22: C++ chapter 2](https://reader035.fdocuments.us/reader035/viewer/2022081605/58e8d0e41a28abb3398b55ef/html5/thumbnails/22.jpg)
Operator
Description Result
~
Binary Ones Complement Operator is unary and has the effect of 'flipping' bits.
(~A ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number.
<< Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.
A << 2 will give 240 which is 1111 0000
>> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.
A >> 2 will give 15 which is 0000 1111
![Page 23: C++ chapter 2](https://reader035.fdocuments.us/reader035/viewer/2022081605/58e8d0e41a28abb3398b55ef/html5/thumbnails/23.jpg)
By Himanshu Kaushik | ApplicationDeveloper.in |
Himanshukaushik.in
main(){ unsigned int a = 60; // 60 = 0011 1100 unsigned int b = 13; // 13 = 0000 1101 int c = 0;
c = a & b; // 12 = 0000 1100 cout << "Line 1 - Value of c is : " << c << endl ;
c = a | b; // 61 = 0011 1101 cout << "Line 2 - Value of c is: " << c << endl ;
c = a ^ b; // 49 = 0011 0001 cout << "Line 3 - Value of c is: " << c << endl ;
c = ~a; // -61 = 1100 0011 cout << "Line 4 - Value of c is: " << c << endl ;
c = a << 2; // 240 = 1111 0000 cout << "Line 5 - Value of c is: " << c << endl ;
c = a >> 2; // 15 = 0000 1111 cout << "Line 6 - Value of c is: " << c << endl ;
return 0;}
![Page 24: C++ chapter 2](https://reader035.fdocuments.us/reader035/viewer/2022081605/58e8d0e41a28abb3398b55ef/html5/thumbnails/24.jpg)
Assignment Operators
By Himanshu Kaushik | ApplicationDeveloper.in |
Himanshukaushik.in
Operator Description Example
= Simple assignment operator, Assigns values from right side operands to left side operand
C = A + B will assign value of A + B into C
+= Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand
C += A is equivalent to C = C + A
-= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand
C -= A is equivalent to C = C - A
![Page 25: C++ chapter 2](https://reader035.fdocuments.us/reader035/viewer/2022081605/58e8d0e41a28abb3398b55ef/html5/thumbnails/25.jpg)
Assignment OperatorsOperator Description Example
*= Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand
C *= A is equivalent to C = C * A
/= Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand
C /= A is equivalent to C = C / A
%= Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand
C %= A is equivalent to C = C % A
![Page 26: C++ chapter 2](https://reader035.fdocuments.us/reader035/viewer/2022081605/58e8d0e41a28abb3398b55ef/html5/thumbnails/26.jpg)
7/8/2014
By Himanshu Kaushik | ApplicationDeveloper.in |
Himanshukaushik.in
main(){ int a = 21; int c ;
c = a; cout << "Line 1 - = Operator, Value of c = : " <<c<< endl ;
c += a; cout << "Line 2 - += Operator, Value of c = : " <<c<< endl ;
c -= a; cout << "Line 3 - -= Operator, Value of c = : " <<c<< endl ;
c *= a; cout << "Line 4 - *= Operator, Value of c = : " <<c<< endl ;
c /= a; cout << "Line 5 - /= Operator, Value of c = : " <<c<< endl ;
c = 200; c %= a; cout << "Line 6 - %= Operator, Value of c = : " <<c<< endl ;
c <<= 2; cout << "Line 7 - <<= Operator, Value of c = : " <<c<< endl ;
c >>= 2; cout << "Line 8 - >>= Operator, Value of c = : " <<c<< endl ;
c &= 2; cout << "Line 9 - &= Operator, Value of c = : " <<c<< endl ;
c ^= 2; cout << "Line 10 - ^= Operator, Value of c = : " <<c<< endl ;
c |= 2; cout << "Line 11 - |= Operator, Value of c = : " <<c<< endl ;
return 0;}
![Page 27: C++ chapter 2](https://reader035.fdocuments.us/reader035/viewer/2022081605/58e8d0e41a28abb3398b55ef/html5/thumbnails/27.jpg)
Conditional operator Operator Example
() ? : a = 2;b = 9;c = (a>b) ? 2:7;Cout<<“c = ”<<c;Output :c = 7
![Page 28: C++ chapter 2](https://reader035.fdocuments.us/reader035/viewer/2022081605/58e8d0e41a28abb3398b55ef/html5/thumbnails/28.jpg)
ArraysIt is continuous memory of same data typesArray can be of int , float or charSyntax :
data_type variable_name[n]; where n is size of arrayFor example,int arry[5] ;
![Page 29: C++ chapter 2](https://reader035.fdocuments.us/reader035/viewer/2022081605/58e8d0e41a28abb3398b55ef/html5/thumbnails/29.jpg)
Initilization of arrayint a[3];a[0] = 0; a[1] = 1; a[2] = 3;
ORint a[3] = {0,1,2};
![Page 30: C++ chapter 2](https://reader035.fdocuments.us/reader035/viewer/2022081605/58e8d0e41a28abb3398b55ef/html5/thumbnails/30.jpg)
THANK YOU