SlideShare a Scribd company logo
Prof. Jinal Bhagat
102000212
Object Oriented
Programming
Unit-2
C++ Basics
Simple C++ Program
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 3
A Simple C++ Program
#include <iostream> //include header file
using namespace std;
int main()
{
cout << "Hello World"; // C++ statement
return 0;
}
 iostream is just like we include stdio.h in c program.
 It contains declarations for the identifier cout and the insertion
operator <<.
 iostream should be included at the beginning of all programs
that use input/output statements.
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 4
A Simple C++ Program (Cont…)
#include <iostream> //include header file
using namespace std;
int main()
{
cout << "Hello World"; // C++ statement
return 0;
}
 A namespace is a declarative region.
 A namespace is a part of the program in which certain names are
recognized; outside of the namespace they’re unknown.
 namespace defines a scope for the identifies that are used in a
program.
 using and namespace are the keywords of C++.
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 5
A Simple C++ Program (Cont…)
#include <iostream> //include header file
using namespace std;
int main()
{
cout << "Hello World"; // C++ statement
return 0;
}
 std is the namespace where ANSI C++ standard class libraries are
defined.
 Various program components such as cout, cin, endl are
defined within std namespace.
 If we don’t use the using directive at top, we have to add the std
followed by :: in the program before identifier.
std::cout << “Hello
World”;
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 6
A Simple C++ Program (Cont…)
#include <iostream> //include header file
using namespace std;
int main()
{
cout << "Hello World"; // C++ statement
return 0;
}
 In C++, main() returns an integer type value.
 Therefore, every main() in C++ should end with a return 0;
statement; otherwise error will occur.
 The return value from the main() function is used by the
runtime library as the exit code for the process.
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 7
Insertion Operator <<
 The operator << is called the
insertion operator.
 It inserts the contents of the
variable on its right to the object
on its left.
 The identifier cout is a predefined
object that represents standard
output stream in C++.
 Here, Screen represents the
output. We can also redirect the
output to other output devices.
 The operator << is used as bitwise
left shift operator also.
Output Using Insertion Operator
cout << "Hello World";
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 8
Program: Basic C++ program
Write a C++ Program to print following
Name: Darshan
City: Rajkot
Country: India
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 9
Program: Basic C++ program
#include <iostream>
using namespace std;
int main()
{
cout << "Name: Darshan";
cout << "City: Rajkot";
cout << "Country:
India";
return 0;
}
Output
Name: DarshanCity: RajkotCountry: India
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 10
Program: Basic C++ program(Cont…)
#include <iostream>
using namespace std;
int main()
{
cout << "Name: Darshann";
cout << "City: Rajkotn";
cout << "Country: India";
return 0;
}
Output
Name: Darshan
City: Rajkot
Country: India
#include <iostream>
using namespace std;
int main()
{
cout << "Name: Darshan"<<endl;
cout << "City: Rajkot"<<endl;
cout << "Country: India"<<endl;
return 0;
}
 The endl manipulator and n has same
effect. Both inserts new line to output.
 But, difference is endl immediate flush to the
output while n do not.
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 11
Extraction Operator >>
 The identifier cin is a predefined
object that represents standard
input stream in C++.
 Here, standard input stream
represents the Keyboard.
 The operator >> is used as
bitwise right shift operator also.
 The operator >> is called the
extraction operator.
 It extracts (or takes) the value
from keyboard and assigns it to
the variable on its right.
KeyBoard
cin number1
Object Extraction Operator
Variable
>>
cin >> number1;
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 12
Program: Basic C++ program
#include<iostream>
using namespace std;
int main()
{
int number1,number2;
cout<<"Enter First Number: ";
cin>>number1; //accept first number
cout<<"Enter Second Number: ";
cin>>number2; //accept first number
cout<<"Addition : ";
cout<<number1+number2; //Display Addition
return 0;
}
C++ Tokens
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 14
C++ Tokens
 The smallest individual unit of a program is known as token.
 C++ has the following tokens:
− Keywords
− Identifiers
− Constants
− Strings
− Special Symbols
− Operators
#include <iostream>
using namespace std;
int main()
{
cout << "Hello
World";
return 0;
}
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 15
Keywords and Identifier
 C++ reserves a set of 84 words for its own use.
 These words are called keywords (or reserved words), and each of
these keywords has a special meaning within the C++ language.
 Identifiers are names that are given to various user defined
program elements, such as variable, function and arrays.
 Some of Predefined identifiers are cout, cin, main
 We cannot use Keyword as user defined identifier.
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 16
Keywords in C++
asm double new switch
auto else operator template
break enum private this
case extern protected throw
catch float public try
char for register typeof
class friend return union
const goto short unsigned
continue if signed virtual
default inline sizeof void
delete int static volatile
do long struct while
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 17
Rules for naming identifiers in C++
1. First Character must be an alphabet or underscore.
2. It can contain only letters(a..z A..Z), digits(0 to 9) or
underscore(_).
3. Identifier name cannot be keyword.
4. Only first 31 characters are significant.
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 18
Valid, Invalid Identifiers
1) Darshan
2) A
3) Age
4) void
5) MAX-ENTRIES
6) double
7) time
8) G
9) Sue's
10) return
11) cout
12) xyz123
13) part#2
14) "char"
15) #include
16) This_is_a_
17) _xyz
18) 9xyz
19) main
20) mutable
21) double
22) max?out
Valid
Valid
Valid
Reserved word
Invalid
Reserved word
Valid
Valid
Invalid
Reserved word
Standard identifier
Valid
Invalid
Invalid
Invalid
Valid
Valid
Invalid
Standard identifier
Reserved word
Reserved word
Invalid
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 19
Constants / Literals
 Constants in C++ refer to fixed values that do not change during
execution of program.
CONSTANTS
INTEGER
CONSTANTS
i.e.
123,-321, 6543
REAL
CONSTANTS
i.e.
0.0083, -0.75
NUMERIC
CONSTANTS
SINGLE
CHARACTER
CONSTANTS
i.e.
‘5’, ‘X’, ‘;’
STRING
CONSTANTS
i.e.
“Hello”, “197”
CHARACTER
CONSTANTS
C++ Operators
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 21
C++ Operators
 All C language operators are valid in C++.
1. Arithmetic operators (+, - , *, /, %)
2. Relational operators (<, <=, >, >=, ==, !=)
3. Logical operators (&&, ||, !)
4. Assignment operators (+=, -=, *=, /=)
5. Increment and decrement operators (++, --)
6. Conditional operators (?:)
7. Bitwise operators (&, |, ^, <<, >>)
8. Special operators ()
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 22
Arithmetic Operators
Operator example Meaning
+ a + b Addition
- a – b Subtraction
* a * b Multiplication
/ a / b Division
% a % b Modulo division- remainder
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 23
Relational Operators
Operator Meaning
< Is less than
<= Is less than or equal to
> Is greater than
>= Is greater than or equal to
== Equal to
!= Not equal to
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 24
Logical Operators
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
a b a && b a || b
true true true true
true false false true
false true false true
false false false false
 a && b : returns false if any of the expression is false
 a || b : returns true if any of the expression is true
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 25
Assignment operator
 We assign a value to a variable using the basic assignment
operator (=).
 Assignment operator stores a value in memory.
 The syntax is
leftSide = rightSide ;
Always it is a
variable identifier.
It is either a literal |
a variable identifier |
an expression.
Literal: ex. i = 1;
Variable identifier: ex. start = i;
Expression: ex. sum = first + second;
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 26
Assignment Operators (Shorthand)
Syntax:
leftSide Op= rightSide ;
Simple assignment
operator Shorthand operator
a = a+1 a += 1
a = a-1 a -= 1
a = a * (m+n) a *= m+n
a = a / (m+n) a /= m+n
a = a % b a %= b
It is an arithmetic
operator.
Ex:
x=x+3;
x+=3;
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 27
Increment and Decrement Operators
 Increment ++
The ++ operator used to increase the value of the variable by one
 Decrement ─ ─
The ─ ─ operator used to decrease the value of the variable by one
Example:
x=100;
x++;
After the execution the value of x will be 101.
Example:
x=100;
x--;
After the execution the value of x will be 99.
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 28
Pre & Post Increment operator
Operator Description
Pre increment operator (++x) value of x is incremented before assigning
it to the variable on the left
x = 10 ;
p = ++x;
After execution
x will be 11
p will be 11
First increment value of
x by one
Operator Description
Post increment operator (x++) value of x is incremented after assigning it
to the variable on the left
x = 10 ;
p = x++;
After execution
x will be 11
p will be 10
First assign value of x
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 29
What is the output of this program?
#include <iostream>
using namespace std;
int main ()
{
int x, y;
x = 5;
y = ++x * ++x;
cout << x << y;
x = 5;
y = x++ * ++x;
cout << x << y;
}
(A) 749735
(B) 736749
(C) 367497
(D) none of the mentioned
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 30
Conditional Operator
Syntax:
exp1 ? exp2 : exp3
Working of the ? Operator:
 exp1 is evaluated first
• if exp1 is true(nonzero) then
- exp2 is evaluated and its value becomes the value of the
expression
• If exp1 is false(zero) then
- exp3 is evaluated and its value becomes the value of the
expression
Ex:
m=2;
n=3;
r=(m>n) ? m :
n;
Ex:
m=2;
n=3;
r=(m<n) ? m :
n;
Value of r will be 3 Value of r will be 2
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 31
Bitwise Operator
Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< Shift left
>> Shift right
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 32
Bitwise Operator Examples
8 = 1000 (In
Binary)
6 = 0110 (In
Binary)
Bitwise & (AND)
int a=8,b=6,c;
c = a & b;
cout<<"Output ="<< c;
Output = 0
Bitwise | (OR)
int a=8,b=6,c;
c = a | b;
cout<<"Output ="<< c;
Output = 14
Bitwise << (Shift Left)
int a=8,b=6,c;
c = a << 1;
cout<<"Output ="<< c;
Output = 16
left shifting is the equivalent
of multiplying a by a power of
two
Bitwise >> (Shift Right)
int a=8,b=6,c;
c = a >> 1;
cout<<"Output ="<< c;
Output = 4
right shifting is the
equivalent of dividing a by a
power of two
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 33
New Operators in C++
:: Scope Resolution
::* Pointer-to-member declarator
->* Pointer-to-member operator
.* Pointer-to-member operator
delet
e
Memory release operator
endl Line feed operator
new Memory allocation operator
setw Field width operator
It allows to access to the global
version of variable
Declares a pointer to a member of
a class
To access pointer to class members
To access pointer to data members
of class
Deallocates memory at run time
It is a manipulator causes a linefeed
to be inserted
Allocates memory at run time
It is a manipulator specifies a field
width for printing value
Scope Resolution
Operator
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 35
Scope Resolution Operator(::)
....
....
{
int x=10;
....
....
{
int x=1;
....
....
}
....
}
Block-2
Block-1
Declaration of x in inner block hides
declaration of same variable declared in an
outer block.
Therefore, in this code both variable x
refers to different data.
 In C language, value of x declared in Block-1 is
not accessible in Block-2.
 In C++, using scope resolution operator (::),
value of x declared in Block-1 can be accessed
in Block-2.
#include <iostream>
using namespace std;
int m=10;
int main()
{
int m=20;
{
int k=m;
int m=3;
cout<<"we are in inner block
n";
cout<<"k="<<k<<endl;
cout<<"m="<<m<<endl;
cout<<"::m="<<::m<<endl;
}
cout<<"we are in outer blockn";
cout<<"m="<<m<<endl;
cout<<"::m="<<::m<<endl;
return 0;
}
Global declaration of variable m
variable m declared , local to main
variable m
declared again local to inner block
Output:
we are in inner block
k=20
m=3
::m=10
we are in outer block
m=20
::m=10
Scope resolution example
C++ Data Types
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 38
Basic Data types
C++ datatypes
User-defined Built-in Derived
Integral Void Floating
structure
union
class
enumeration
array
function
pointer
reference
int char float double
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 39
Built in Data types
Data Type Size (bytes) Range
char 1 -128 to 127
unsigned char 1 0 to 255
short or int 2 -32,768 to 32,767
unsigned int 2 0 to 65535
long 4 -2147483648 to 2147483647
unsigned long 4 0 to 4294967295
float 4 3.4e-38 to 3.4e+308
double 8 1.7e-308 to 1.7e+308
long double 10 3.4e-4932 to 1.1e+4932
Type Conversion
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 41
Type Conversion
 Type Conversion is the process of converting one predefined data
type into another data type.
Type Conversion
Implicit
(Automatically converts
one datatype to another
datatype)
Explicit
(Forcefully converts one
datatype to another
datatype)
 Explicit type conversion is also known as type casting.
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 42
Type Conversion(Cont…)
int a;
double b=2.55;
a = b; // implicit type conversion
cout << a << endl; // this will print
2
a = int(b); //explicit type
conversion
cout << a << endl; // this will print
2
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 43
Implicit type conversion hierarchy
char
int
unsigned
int
long int
float
double
long
double
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 44
Implicit Type Conversion
#include <iostream>
using namespace std;
int main()
{
int count = 5;
float avg = 10.01;
double ans;
ans = count * avg;
cout<<"Answer=:"<<ans;
return 0;
} Output:
Answer = 50.05
5
10.0
1
*
=
double int float
ans count avg
5.0
float 50.0
5
float
50.0
5
double
*
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 45
Type Casting
 In C++ explicit type conversion is called type casting.
 Syntax
type-name (expression) //C++ notation
 Example
average = sum/(float) i; //C notation
average = sum/float (i); //C++ notation
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
a = 19.99 + 11.99;
b = (int) 19.99 + (int) 11.99;
c = int (19.99) + int (11.99);
cout << "a = " << a << ", b = " << b;
cout << ", c = " << c << endl;
char ch = 'Z';
cout << "The code for " << ch << " is ";
cout << int(ch) << endl;
return 0;
}
Output:
a = 31, b = 30, c = 30
The code for Z is 90
Type Casting Example
//adds the values as float
// then converts the result to int
// old C
syntax
// new C++
syntax
//print as
char
//print as int
Reference Variable
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 48
Reference Variable
 A reference provides an alias or a different name for a variable.
 One of the most important uses for references is in passing
arguments to functions.
int a=5;
int &ans = a;
cout<<"a="<<a<<endl;
cout<<"&a="<<&a<<endl;
cout<<"ans="<<ans<<endl;
cout<<"&ans="<<&ans<<endl;
ans++;
cout<<"a="<<a<<endl;
cout<<"ans="<<ans<<endl;
declares variable a
declares ans as reference to a
OUTPUT
a=5
&a=0x6ffe34
ans=5
&ans=0x6ffe34
a=6
ans=6
Its necessary to
initialize the
Reference at the
time of declaration
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 49
Reference Variable(Cont…)
 C++ references allow you to create a second name for the a
variable.
 Reference variable for the purpose of accessing and modifying the
value of the original variable even if the second name (the
reference) is located within a different scope.
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 50
Reference Vs Pointer
Pointers
int *p = &i;
References
int i;
int &r = i;
i
r
addr
p
addr
A pointer is a variable
which stores the address
of another variable.
A reference is a
variable which refers
to another variable.
Enumeration
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 52
Enumeration (A user defined Data Type)
 An enumeration is set of named integer constants.
 Enumerations are defined much like structures.
enum days{Sun,Mon,Tues,Wed,Thur,Fri,Sat};
Keyword
Tag
name Integer Values for symbolic constants
0 1 2 3 4 5 6
 Above statement creates days the name of datatype.
 By default, enumerators are assigned integer values starting with 0.
 It establishes Sun, Mon… and so on as symbolic constants for
the integer values 0-6.
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 53
Enumeration Behaviour(Cont…)
enum coin { penny, nickel, dime, quarter=100,
half_dollar, dollar};
The values of these symbols are
penny 0
nickel 1
dime 2
quarter 100
half_dollar 101
dollar 102
Enumeration Behaviour
enum days{ sun, mon, tue, wed, thu, fri, sat
};
days today;
today = tue;
today = 6;
today++;
today = mon + fri;
int num = sat;
num = 5 + mon;
variable today declared of type days
Valid, because tue is an enumerator. Value 2 will
be assigned in today
Invalid, because 6 is not an enumerator
Invalid, today is of type days. We can not apply +
+ to structure variable also
Invalid
Valid, days data type converted to int,
value 6 will be assigned to num
Valid, mon converted to int with value 1
Control Structures
I like C++ so much
I will score good marks in C++
Unit-2 C++ Basics A D Patel Institute of Technology 56
Control Structures
 The if statement:
• Simple if statement
• if…else statement
• else…if ladder
• if…else nested
 The switch statement :
 The do-while statement: An exit controlled loop
 The while Statement: An entry controlled loop
 The for statement: An entry controlled loop
Thank You

More Related Content

PPTX
Module 2 | Object Oriented Programming with C++ | Basics of C++
ADITYATANDONKECCSE
 
PDF
Chap 2 c++
Widad Jamaluddin
 
PDF
BASIC C++ PROGRAMMING
gufranresearcher
 
PPT
02a fundamental c++ types, arithmetic
Manzoor ALam
 
PPT
CPLUSPLUS UET PESHAWAR BS ELECTRICAL 2ND SEMSTER Lecture02.ppt
abdurrahimk182
 
PPT
Savitch ch 022
Dr .Ahmed Tawwab
 
PPTX
POLITEKNIK MALAYSIA
Aiman Hud
 
Module 2 | Object Oriented Programming with C++ | Basics of C++
ADITYATANDONKECCSE
 
Chap 2 c++
Widad Jamaluddin
 
BASIC C++ PROGRAMMING
gufranresearcher
 
02a fundamental c++ types, arithmetic
Manzoor ALam
 
CPLUSPLUS UET PESHAWAR BS ELECTRICAL 2ND SEMSTER Lecture02.ppt
abdurrahimk182
 
Savitch ch 022
Dr .Ahmed Tawwab
 
POLITEKNIK MALAYSIA
Aiman Hud
 

Similar to Presentat ions_PPT_Unit-2_OOP.pptx (20)

PDF
Introduction of OOPs and C++. Object oriented programming
Jatin541436
 
PPTX
UNIT - 1- Ood ddnwkjfnewcsdkjnjkfnskfn.pptx
crazysamarth927
 
PPT
Basics of c++ Programming Language
Ahmad Idrees
 
PPTX
Introduction to c++ programming language
harinipradeep15
 
PPT
Introduction to C++,Computer Science
Abhinav Vishnoi
 
PPTX
Introduction to C++ lecture ************
Emad Helal
 
PPT
Chapter02-S11.ppt
GhulamHussain638563
 
PPTX
COMPUTER PROGRAMMING LANGUAGE C++ 1.pptx
atokoosetaiwoboluwat
 
PDF
1-19 CPP Slides 2022-02-28 18_22_ 05.pdf
dhruvjs
 
PDF
2 BytesC++ course_2014_c1_basicsc++
kinan keshkeh
 
PPT
Chapter 2 Introduction to C++
GhulamHussain142878
 
PDF
Introduction to cpp
Nilesh Dalvi
 
PPTX
Basic Programming of c++
MMAbdullah17
 
PPTX
INTRODUCTION TO C++.pptx
MamataAnilgod
 
PPTX
C Programming- Harsh Sharma
Harsh Sharma
 
PPTX
Basics Of C++.pptx
DineshDhuri4
 
PPTX
additional.pptx
Yuvraj994432
 
PPT
Intro cpp
hamza239523
 
PPTX
Introduction to C++.pptx learn c++ and basic concepts of OOP
UbaidKhan930128
 
Introduction of OOPs and C++. Object oriented programming
Jatin541436
 
UNIT - 1- Ood ddnwkjfnewcsdkjnjkfnskfn.pptx
crazysamarth927
 
Basics of c++ Programming Language
Ahmad Idrees
 
Introduction to c++ programming language
harinipradeep15
 
Introduction to C++,Computer Science
Abhinav Vishnoi
 
Introduction to C++ lecture ************
Emad Helal
 
Chapter02-S11.ppt
GhulamHussain638563
 
COMPUTER PROGRAMMING LANGUAGE C++ 1.pptx
atokoosetaiwoboluwat
 
1-19 CPP Slides 2022-02-28 18_22_ 05.pdf
dhruvjs
 
2 BytesC++ course_2014_c1_basicsc++
kinan keshkeh
 
Chapter 2 Introduction to C++
GhulamHussain142878
 
Introduction to cpp
Nilesh Dalvi
 
Basic Programming of c++
MMAbdullah17
 
INTRODUCTION TO C++.pptx
MamataAnilgod
 
C Programming- Harsh Sharma
Harsh Sharma
 
Basics Of C++.pptx
DineshDhuri4
 
additional.pptx
Yuvraj994432
 
Intro cpp
hamza239523
 
Introduction to C++.pptx learn c++ and basic concepts of OOP
UbaidKhan930128
 
Ad

More from ZeelGoyani (16)

PPTX
COA Project.pptxkjnjnjbjkbn,n,njkjjnnjln , nkn n,k
ZeelGoyani
 
PPTX
Unit 2- Machine Learninnonjjnkbhkhjjljknkmg.pptx
ZeelGoyani
 
PPTX
Presentations_PPT_ Unit-6_OOP.pptx
ZeelGoyani
 
PPTX
Unit-6.pptx lk kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
ZeelGoyani
 
PPTX
Unit 1 (1).ppt,, x
ZeelGoyani
 
PPTX
Creating-a-Tkinter-Python-Calculator.pptx
ZeelGoyani
 
PPT
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
ZeelGoyani
 
PPTX
Os presentation final.pptxjjjjjdakajwsjjdhdfjff
ZeelGoyani
 
PPTX
Java Presentation[1].pptxhhuvbhujhhujnnjoj
ZeelGoyani
 
PPTX
pptof5gtechnology16btece014-191201094719.pptx
ZeelGoyani
 
PPTX
Journey-of-Personal-Development-Part-1.pptx
ZeelGoyani
 
PPTX
12202080601124-Sargam Desai.ppt df TG vs TV TB yx
ZeelGoyani
 
PPTX
ppt-personalitydevelopment-130903024646-phpapp01 (2).pptx
ZeelGoyani
 
PPT
Self Discovery.pptnmkkeusuziiwudhwhdhueuur v
ZeelGoyani
 
PPTX
hhuhuihuhuihPresentations_PPT_Unit-5_OOP.pptx
ZeelGoyani
 
PPT
291-02.l20.ppt
ZeelGoyani
 
COA Project.pptxkjnjnjbjkbn,n,njkjjnnjln , nkn n,k
ZeelGoyani
 
Unit 2- Machine Learninnonjjnkbhkhjjljknkmg.pptx
ZeelGoyani
 
Presentations_PPT_ Unit-6_OOP.pptx
ZeelGoyani
 
Unit-6.pptx lk kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
ZeelGoyani
 
Unit 1 (1).ppt,, x
ZeelGoyani
 
Creating-a-Tkinter-Python-Calculator.pptx
ZeelGoyani
 
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
ZeelGoyani
 
Os presentation final.pptxjjjjjdakajwsjjdhdfjff
ZeelGoyani
 
Java Presentation[1].pptxhhuvbhujhhujnnjoj
ZeelGoyani
 
pptof5gtechnology16btece014-191201094719.pptx
ZeelGoyani
 
Journey-of-Personal-Development-Part-1.pptx
ZeelGoyani
 
12202080601124-Sargam Desai.ppt df TG vs TV TB yx
ZeelGoyani
 
ppt-personalitydevelopment-130903024646-phpapp01 (2).pptx
ZeelGoyani
 
Self Discovery.pptnmkkeusuziiwudhwhdhueuur v
ZeelGoyani
 
hhuhuihuhuihPresentations_PPT_Unit-5_OOP.pptx
ZeelGoyani
 
291-02.l20.ppt
ZeelGoyani
 
Ad

Recently uploaded (20)

PPTX
Models of screening of Adrenergic Blocking Drugs.pptx
Dr Fatima Rani
 
PPTX
QUIZ Questions Emergency department.pptx
smiti24dml064
 
PPT
9. Applied Biomechanics (fracture fixation)etc.ppt
Bolan University of Medical and Health Sciences ,Quetta
 
PDF
CA & Simple Goitre , surgery, Faculty of medicine .pdf
MostafaMohammed95
 
PPTX
Digital Dichoptic Therapy for Amblyopia.
Gamal Saif
 
PPTX
HALITOSIS presentation for Ear, Nose and throat.pptx
stefanumerah1
 
PPTX
A Detailed Overview of Sterols Chemistry, Sources, Functions and Applications...
Indranil Karmakar
 
PPTX
DEVELOPMENTAL DYSPLASIA OF HIP , Congenital Dislocation of Hip
Deep Desai
 
PPTX
the comoany external environment crafting
immrahaman62
 
PPTX
IMPORTANCE of WORLD ORS DAY July 29 & ORS.pptx
MedicalSuperintenden19
 
PPT
Diagnosis-and-treatment-planning-in-CD - DR.SONIA.ppt
drsoniabithi1987
 
PPTX
Describe Thyroid storm & it’s Pharmacotherapy Drug Interaction: Pyridoxine + ...
Dr. Deepa Singh Rana
 
PPTX
HANAU ARTICULATORS AND CLASSIFICATION.pptx
Priya Singaravelu
 
PPTX
perioperative management and ERAS protocol.pptx
Fahad Ahmad
 
PPTX
Cosmetics and cosmeceuticals : sunscreen and sunprotection.pptx
SahilKasture2
 
PDF
ICF around the World - Keynote presentation
Olaf Kraus de Camargo
 
PPTX
AUG 2025 ONCOLOGY CARTOONS BY DR KANHU CHARAN PATRO
Kanhu Charan
 
PDF
Digital literacy note level 6 perioperative theatre technician
mercylindah47
 
PPTX
COPD chronic obstructive pulmonary disease.pptx
pearlprincess7557
 
PPTX
Models for Screening of DIURETICS- Dr. ZOYA KHAN.pptx
Zoya Khan
 
Models of screening of Adrenergic Blocking Drugs.pptx
Dr Fatima Rani
 
QUIZ Questions Emergency department.pptx
smiti24dml064
 
9. Applied Biomechanics (fracture fixation)etc.ppt
Bolan University of Medical and Health Sciences ,Quetta
 
CA & Simple Goitre , surgery, Faculty of medicine .pdf
MostafaMohammed95
 
Digital Dichoptic Therapy for Amblyopia.
Gamal Saif
 
HALITOSIS presentation for Ear, Nose and throat.pptx
stefanumerah1
 
A Detailed Overview of Sterols Chemistry, Sources, Functions and Applications...
Indranil Karmakar
 
DEVELOPMENTAL DYSPLASIA OF HIP , Congenital Dislocation of Hip
Deep Desai
 
the comoany external environment crafting
immrahaman62
 
IMPORTANCE of WORLD ORS DAY July 29 & ORS.pptx
MedicalSuperintenden19
 
Diagnosis-and-treatment-planning-in-CD - DR.SONIA.ppt
drsoniabithi1987
 
Describe Thyroid storm & it’s Pharmacotherapy Drug Interaction: Pyridoxine + ...
Dr. Deepa Singh Rana
 
HANAU ARTICULATORS AND CLASSIFICATION.pptx
Priya Singaravelu
 
perioperative management and ERAS protocol.pptx
Fahad Ahmad
 
Cosmetics and cosmeceuticals : sunscreen and sunprotection.pptx
SahilKasture2
 
ICF around the World - Keynote presentation
Olaf Kraus de Camargo
 
AUG 2025 ONCOLOGY CARTOONS BY DR KANHU CHARAN PATRO
Kanhu Charan
 
Digital literacy note level 6 perioperative theatre technician
mercylindah47
 
COPD chronic obstructive pulmonary disease.pptx
pearlprincess7557
 
Models for Screening of DIURETICS- Dr. ZOYA KHAN.pptx
Zoya Khan
 

Presentat ions_PPT_Unit-2_OOP.pptx

  • 1. Prof. Jinal Bhagat 102000212 Object Oriented Programming Unit-2 C++ Basics
  • 3. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 3 A Simple C++ Program #include <iostream> //include header file using namespace std; int main() { cout << "Hello World"; // C++ statement return 0; }  iostream is just like we include stdio.h in c program.  It contains declarations for the identifier cout and the insertion operator <<.  iostream should be included at the beginning of all programs that use input/output statements.
  • 4. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 4 A Simple C++ Program (Cont…) #include <iostream> //include header file using namespace std; int main() { cout << "Hello World"; // C++ statement return 0; }  A namespace is a declarative region.  A namespace is a part of the program in which certain names are recognized; outside of the namespace they’re unknown.  namespace defines a scope for the identifies that are used in a program.  using and namespace are the keywords of C++.
  • 5. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 5 A Simple C++ Program (Cont…) #include <iostream> //include header file using namespace std; int main() { cout << "Hello World"; // C++ statement return 0; }  std is the namespace where ANSI C++ standard class libraries are defined.  Various program components such as cout, cin, endl are defined within std namespace.  If we don’t use the using directive at top, we have to add the std followed by :: in the program before identifier. std::cout << “Hello World”;
  • 6. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 6 A Simple C++ Program (Cont…) #include <iostream> //include header file using namespace std; int main() { cout << "Hello World"; // C++ statement return 0; }  In C++, main() returns an integer type value.  Therefore, every main() in C++ should end with a return 0; statement; otherwise error will occur.  The return value from the main() function is used by the runtime library as the exit code for the process.
  • 7. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 7 Insertion Operator <<  The operator << is called the insertion operator.  It inserts the contents of the variable on its right to the object on its left.  The identifier cout is a predefined object that represents standard output stream in C++.  Here, Screen represents the output. We can also redirect the output to other output devices.  The operator << is used as bitwise left shift operator also. Output Using Insertion Operator cout << "Hello World";
  • 8. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 8 Program: Basic C++ program Write a C++ Program to print following Name: Darshan City: Rajkot Country: India
  • 9. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 9 Program: Basic C++ program #include <iostream> using namespace std; int main() { cout << "Name: Darshan"; cout << "City: Rajkot"; cout << "Country: India"; return 0; } Output Name: DarshanCity: RajkotCountry: India
  • 10. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 10 Program: Basic C++ program(Cont…) #include <iostream> using namespace std; int main() { cout << "Name: Darshann"; cout << "City: Rajkotn"; cout << "Country: India"; return 0; } Output Name: Darshan City: Rajkot Country: India #include <iostream> using namespace std; int main() { cout << "Name: Darshan"<<endl; cout << "City: Rajkot"<<endl; cout << "Country: India"<<endl; return 0; }  The endl manipulator and n has same effect. Both inserts new line to output.  But, difference is endl immediate flush to the output while n do not.
  • 11. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 11 Extraction Operator >>  The identifier cin is a predefined object that represents standard input stream in C++.  Here, standard input stream represents the Keyboard.  The operator >> is used as bitwise right shift operator also.  The operator >> is called the extraction operator.  It extracts (or takes) the value from keyboard and assigns it to the variable on its right. KeyBoard cin number1 Object Extraction Operator Variable >> cin >> number1;
  • 12. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 12 Program: Basic C++ program #include<iostream> using namespace std; int main() { int number1,number2; cout<<"Enter First Number: "; cin>>number1; //accept first number cout<<"Enter Second Number: "; cin>>number2; //accept first number cout<<"Addition : "; cout<<number1+number2; //Display Addition return 0; }
  • 14. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 14 C++ Tokens  The smallest individual unit of a program is known as token.  C++ has the following tokens: − Keywords − Identifiers − Constants − Strings − Special Symbols − Operators #include <iostream> using namespace std; int main() { cout << "Hello World"; return 0; }
  • 15. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 15 Keywords and Identifier  C++ reserves a set of 84 words for its own use.  These words are called keywords (or reserved words), and each of these keywords has a special meaning within the C++ language.  Identifiers are names that are given to various user defined program elements, such as variable, function and arrays.  Some of Predefined identifiers are cout, cin, main  We cannot use Keyword as user defined identifier.
  • 16. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 16 Keywords in C++ asm double new switch auto else operator template break enum private this case extern protected throw catch float public try char for register typeof class friend return union const goto short unsigned continue if signed virtual default inline sizeof void delete int static volatile do long struct while
  • 17. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 17 Rules for naming identifiers in C++ 1. First Character must be an alphabet or underscore. 2. It can contain only letters(a..z A..Z), digits(0 to 9) or underscore(_). 3. Identifier name cannot be keyword. 4. Only first 31 characters are significant.
  • 18. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 18 Valid, Invalid Identifiers 1) Darshan 2) A 3) Age 4) void 5) MAX-ENTRIES 6) double 7) time 8) G 9) Sue's 10) return 11) cout 12) xyz123 13) part#2 14) "char" 15) #include 16) This_is_a_ 17) _xyz 18) 9xyz 19) main 20) mutable 21) double 22) max?out Valid Valid Valid Reserved word Invalid Reserved word Valid Valid Invalid Reserved word Standard identifier Valid Invalid Invalid Invalid Valid Valid Invalid Standard identifier Reserved word Reserved word Invalid
  • 19. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 19 Constants / Literals  Constants in C++ refer to fixed values that do not change during execution of program. CONSTANTS INTEGER CONSTANTS i.e. 123,-321, 6543 REAL CONSTANTS i.e. 0.0083, -0.75 NUMERIC CONSTANTS SINGLE CHARACTER CONSTANTS i.e. ‘5’, ‘X’, ‘;’ STRING CONSTANTS i.e. “Hello”, “197” CHARACTER CONSTANTS
  • 21. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 21 C++ Operators  All C language operators are valid in C++. 1. Arithmetic operators (+, - , *, /, %) 2. Relational operators (<, <=, >, >=, ==, !=) 3. Logical operators (&&, ||, !) 4. Assignment operators (+=, -=, *=, /=) 5. Increment and decrement operators (++, --) 6. Conditional operators (?:) 7. Bitwise operators (&, |, ^, <<, >>) 8. Special operators ()
  • 22. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 22 Arithmetic Operators Operator example Meaning + a + b Addition - a – b Subtraction * a * b Multiplication / a / b Division % a % b Modulo division- remainder
  • 23. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 23 Relational Operators Operator Meaning < Is less than <= Is less than or equal to > Is greater than >= Is greater than or equal to == Equal to != Not equal to
  • 24. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 24 Logical Operators Operator Meaning && Logical AND || Logical OR ! Logical NOT a b a && b a || b true true true true true false false true false true false true false false false false  a && b : returns false if any of the expression is false  a || b : returns true if any of the expression is true
  • 25. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 25 Assignment operator  We assign a value to a variable using the basic assignment operator (=).  Assignment operator stores a value in memory.  The syntax is leftSide = rightSide ; Always it is a variable identifier. It is either a literal | a variable identifier | an expression. Literal: ex. i = 1; Variable identifier: ex. start = i; Expression: ex. sum = first + second;
  • 26. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 26 Assignment Operators (Shorthand) Syntax: leftSide Op= rightSide ; Simple assignment operator Shorthand operator a = a+1 a += 1 a = a-1 a -= 1 a = a * (m+n) a *= m+n a = a / (m+n) a /= m+n a = a % b a %= b It is an arithmetic operator. Ex: x=x+3; x+=3;
  • 27. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 27 Increment and Decrement Operators  Increment ++ The ++ operator used to increase the value of the variable by one  Decrement ─ ─ The ─ ─ operator used to decrease the value of the variable by one Example: x=100; x++; After the execution the value of x will be 101. Example: x=100; x--; After the execution the value of x will be 99.
  • 28. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 28 Pre & Post Increment operator Operator Description Pre increment operator (++x) value of x is incremented before assigning it to the variable on the left x = 10 ; p = ++x; After execution x will be 11 p will be 11 First increment value of x by one Operator Description Post increment operator (x++) value of x is incremented after assigning it to the variable on the left x = 10 ; p = x++; After execution x will be 11 p will be 10 First assign value of x
  • 29. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 29 What is the output of this program? #include <iostream> using namespace std; int main () { int x, y; x = 5; y = ++x * ++x; cout << x << y; x = 5; y = x++ * ++x; cout << x << y; } (A) 749735 (B) 736749 (C) 367497 (D) none of the mentioned
  • 30. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 30 Conditional Operator Syntax: exp1 ? exp2 : exp3 Working of the ? Operator:  exp1 is evaluated first • if exp1 is true(nonzero) then - exp2 is evaluated and its value becomes the value of the expression • If exp1 is false(zero) then - exp3 is evaluated and its value becomes the value of the expression Ex: m=2; n=3; r=(m>n) ? m : n; Ex: m=2; n=3; r=(m<n) ? m : n; Value of r will be 3 Value of r will be 2
  • 31. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 31 Bitwise Operator Operator Meaning & Bitwise AND | Bitwise OR ^ Bitwise exclusive OR << Shift left >> Shift right
  • 32. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 32 Bitwise Operator Examples 8 = 1000 (In Binary) 6 = 0110 (In Binary) Bitwise & (AND) int a=8,b=6,c; c = a & b; cout<<"Output ="<< c; Output = 0 Bitwise | (OR) int a=8,b=6,c; c = a | b; cout<<"Output ="<< c; Output = 14 Bitwise << (Shift Left) int a=8,b=6,c; c = a << 1; cout<<"Output ="<< c; Output = 16 left shifting is the equivalent of multiplying a by a power of two Bitwise >> (Shift Right) int a=8,b=6,c; c = a >> 1; cout<<"Output ="<< c; Output = 4 right shifting is the equivalent of dividing a by a power of two
  • 33. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 33 New Operators in C++ :: Scope Resolution ::* Pointer-to-member declarator ->* Pointer-to-member operator .* Pointer-to-member operator delet e Memory release operator endl Line feed operator new Memory allocation operator setw Field width operator It allows to access to the global version of variable Declares a pointer to a member of a class To access pointer to class members To access pointer to data members of class Deallocates memory at run time It is a manipulator causes a linefeed to be inserted Allocates memory at run time It is a manipulator specifies a field width for printing value
  • 35. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 35 Scope Resolution Operator(::) .... .... { int x=10; .... .... { int x=1; .... .... } .... } Block-2 Block-1 Declaration of x in inner block hides declaration of same variable declared in an outer block. Therefore, in this code both variable x refers to different data.  In C language, value of x declared in Block-1 is not accessible in Block-2.  In C++, using scope resolution operator (::), value of x declared in Block-1 can be accessed in Block-2.
  • 36. #include <iostream> using namespace std; int m=10; int main() { int m=20; { int k=m; int m=3; cout<<"we are in inner block n"; cout<<"k="<<k<<endl; cout<<"m="<<m<<endl; cout<<"::m="<<::m<<endl; } cout<<"we are in outer blockn"; cout<<"m="<<m<<endl; cout<<"::m="<<::m<<endl; return 0; } Global declaration of variable m variable m declared , local to main variable m declared again local to inner block Output: we are in inner block k=20 m=3 ::m=10 we are in outer block m=20 ::m=10 Scope resolution example
  • 38. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 38 Basic Data types C++ datatypes User-defined Built-in Derived Integral Void Floating structure union class enumeration array function pointer reference int char float double
  • 39. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 39 Built in Data types Data Type Size (bytes) Range char 1 -128 to 127 unsigned char 1 0 to 255 short or int 2 -32,768 to 32,767 unsigned int 2 0 to 65535 long 4 -2147483648 to 2147483647 unsigned long 4 0 to 4294967295 float 4 3.4e-38 to 3.4e+308 double 8 1.7e-308 to 1.7e+308 long double 10 3.4e-4932 to 1.1e+4932
  • 41. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 41 Type Conversion  Type Conversion is the process of converting one predefined data type into another data type. Type Conversion Implicit (Automatically converts one datatype to another datatype) Explicit (Forcefully converts one datatype to another datatype)  Explicit type conversion is also known as type casting.
  • 42. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 42 Type Conversion(Cont…) int a; double b=2.55; a = b; // implicit type conversion cout << a << endl; // this will print 2 a = int(b); //explicit type conversion cout << a << endl; // this will print 2
  • 43. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 43 Implicit type conversion hierarchy char int unsigned int long int float double long double
  • 44. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 44 Implicit Type Conversion #include <iostream> using namespace std; int main() { int count = 5; float avg = 10.01; double ans; ans = count * avg; cout<<"Answer=:"<<ans; return 0; } Output: Answer = 50.05 5 10.0 1 * = double int float ans count avg 5.0 float 50.0 5 float 50.0 5 double *
  • 45. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 45 Type Casting  In C++ explicit type conversion is called type casting.  Syntax type-name (expression) //C++ notation  Example average = sum/(float) i; //C notation average = sum/float (i); //C++ notation
  • 46. #include <iostream> using namespace std; int main() { int a, b, c; a = 19.99 + 11.99; b = (int) 19.99 + (int) 11.99; c = int (19.99) + int (11.99); cout << "a = " << a << ", b = " << b; cout << ", c = " << c << endl; char ch = 'Z'; cout << "The code for " << ch << " is "; cout << int(ch) << endl; return 0; } Output: a = 31, b = 30, c = 30 The code for Z is 90 Type Casting Example //adds the values as float // then converts the result to int // old C syntax // new C++ syntax //print as char //print as int
  • 48. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 48 Reference Variable  A reference provides an alias or a different name for a variable.  One of the most important uses for references is in passing arguments to functions. int a=5; int &ans = a; cout<<"a="<<a<<endl; cout<<"&a="<<&a<<endl; cout<<"ans="<<ans<<endl; cout<<"&ans="<<&ans<<endl; ans++; cout<<"a="<<a<<endl; cout<<"ans="<<ans<<endl; declares variable a declares ans as reference to a OUTPUT a=5 &a=0x6ffe34 ans=5 &ans=0x6ffe34 a=6 ans=6 Its necessary to initialize the Reference at the time of declaration
  • 49. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 49 Reference Variable(Cont…)  C++ references allow you to create a second name for the a variable.  Reference variable for the purpose of accessing and modifying the value of the original variable even if the second name (the reference) is located within a different scope.
  • 50. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 50 Reference Vs Pointer Pointers int *p = &i; References int i; int &r = i; i r addr p addr A pointer is a variable which stores the address of another variable. A reference is a variable which refers to another variable.
  • 52. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 52 Enumeration (A user defined Data Type)  An enumeration is set of named integer constants.  Enumerations are defined much like structures. enum days{Sun,Mon,Tues,Wed,Thur,Fri,Sat}; Keyword Tag name Integer Values for symbolic constants 0 1 2 3 4 5 6  Above statement creates days the name of datatype.  By default, enumerators are assigned integer values starting with 0.  It establishes Sun, Mon… and so on as symbolic constants for the integer values 0-6.
  • 53. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 53 Enumeration Behaviour(Cont…) enum coin { penny, nickel, dime, quarter=100, half_dollar, dollar}; The values of these symbols are penny 0 nickel 1 dime 2 quarter 100 half_dollar 101 dollar 102
  • 54. Enumeration Behaviour enum days{ sun, mon, tue, wed, thu, fri, sat }; days today; today = tue; today = 6; today++; today = mon + fri; int num = sat; num = 5 + mon; variable today declared of type days Valid, because tue is an enumerator. Value 2 will be assigned in today Invalid, because 6 is not an enumerator Invalid, today is of type days. We can not apply + + to structure variable also Invalid Valid, days data type converted to int, value 6 will be assigned to num Valid, mon converted to int with value 1
  • 56. I like C++ so much I will score good marks in C++ Unit-2 C++ Basics A D Patel Institute of Technology 56 Control Structures  The if statement: • Simple if statement • if…else statement • else…if ladder • if…else nested  The switch statement :  The do-while statement: An exit controlled loop  The while Statement: An entry controlled loop  The for statement: An entry controlled loop

Editor's Notes

  • #3: On startup, control always start from main(). If there is no function called main() in your program, a linker error will be generated. Majority of the Statements in c++ are identical to statements in c. Iostream is morden style header which is defined by standard c++. Contains declarations for cout identifier and << operator. #include tells compiler to ass the sourcefile iostream to first.cpp file before compiling.
  • #4: C++ program
  • #36: :: operator means to use the global version of that variable.
  • #41: Type conversion occurs when the expression has data of mixed data types.  Implicit Type Conversion is the conversion performed by the compiler without programmer’s intervention. Type Conversion is the process of converting one predefined type into another type. and type Casting is the converting one predefined type into another type forcefully.
  • #52: It is a way for attaching names to numbers Enum keyword automatically enumerates a list of words by assigning the values 0,1,2 and so on Here the tag name days become a new data type. By using this tag name you can declare new variables
  • #56: Used when control is transferred to one point to many possible points.