0% found this document useful (0 votes)
14 views57 pages

Presentations PPT Unit-2 OOP

Uploaded by

Zeel Goyani
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)
14 views57 pages

Presentations PPT Unit-2 OOP

Uploaded by

Zeel Goyani
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/ 57

102000212

Object Oriented
Programming

Unit-2
C++ Basics
Prof. Jinal Bhagat
Simple C++ Program
A Simple C++ Program
#include <iostream> //include header file
using namespace std;
int main()
{
cout << "Hello World"; // C++ statement

}
I like C++ so much
return 0;

I iostream
will score good
is just like marks
we include stdio.hin
in c 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.

Unit-2 C++ Basics A D Patel Institute of Technology 3


A Simple C++ Program (Cont…)
#include <iostream> //include header file
using namespace std;
int main()
{
cout << "Hello World"; // C++ statement

}
I like C++ so much
return 0;

I will score good marks in C++


 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++.

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

}
I like C++ so much
return 0;

I will score good marks in C++


 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
Unit-2 C++ Basics World”; 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

}
I like C++ so much
return 0;

I In
will score
C++, main() good
returns marks
an integer in
type value. C++
 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.

Unit-2 C++ Basics A D Patel Institute of Technology 6


Insertion Operator <<
cout << "Hello World";

 The operator << is called the


insertion operator.
 It inserts the contents of the
I like C++ so much
variable on its right to the object
on its left.

I will score good marks in C++


 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. Output Using Insertion Operator
 The operator << is used as bitwise
left shift operator also.

Unit-2 C++ Basics A D Patel Institute of Technology 7


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 8


Program: Basic C++ program
#include <iostream>
using namespace std;
int main()
{
cout << "Name: Darshan";
I like C++ so much
cout << "City: Rajkot";

I will score good marks in C++


cout << "Country:
India";
return 0;
}

Output
Name: DarshanCity: RajkotCountry: India
Unit-2 C++ Basics A D Patel Institute of Technology 9
Program: Basic C++ program(Cont…)
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main() int main()
{ {
cout << "Name: Darshan\n"; cout << "Name: Darshan"<<endl;
cout << "City: Rajkot\n"; cout << "City: Rajkot"<<endl;

I like C++ so much


cout << "Country: India"; cout << "Country: India"<<endl;
return 0; return 0;
} }

I will score good


Output The endlmarks inandC++
manipulator \n has same
Name: Darshan effect. Both inserts new line to output.
City: Rajkot  But, difference is endl immediate flush to the
Country: India
output while \n do not.

Unit-2 C++ Basics A D Patel Institute of Technology 10


Extraction Operator >>
cin >> number1;
 The operator >> is called the
extraction operator.
 It extracts (or takes) the value Object Extraction Operator
from keyboard and assigns it to
I like C++ so much
cin >> number1
the variable on its right.
 The identifier cin is a predefined Variable

I will score good marks in C++


object that represents standard
input stream in C++.
 Here, standard input stream KeyBoard
represents the Keyboard.
 The operator >> is used as
bitwise right shift operator also.

Unit-2 C++ Basics A D Patel Institute of Technology 11


Program: Basic C++ program
#include<iostream>
using namespace std;
int main()
{
int number1,number2;

I like C++ so much


cout<<"Enter First Number: ";
cin>>number1; //accept first number

I will score good marks in C++


cout<<"Enter Second Number: ";
cin>>number2; //accept first number

cout<<"Addition : ";
cout<<number1+number2; //Display Addition
return 0;
}

Unit-2 C++ Basics A D Patel Institute of Technology 12


C++ Tokens
C++ Tokens
 The smallest individual unit of a program is known as token.
 C++ has the following tokens:
− Keywords #include <iostream>
− Identifiers using namespace std;

I like C++ so much


− Constants
int main()
{

I will score good marks in C++


− Strings cout << "Hello
World";
− Special Symbols return 0;
− Operators }

Unit-2 C++ Basics A D Patel Institute of Technology 14


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
I like C++ so much
program elements, such as variable, function and arrays.
 Some of Predefined identifiers are cout, cin, main
I will score good marks in C++
 We cannot use Keyword as user defined identifier.

Unit-2 C++ Basics A D Patel Institute of Technology 15


Keywords in C++
asm double new switch
auto else operator template
break enum private this
case extern protected throw
catch float public try
I like C++ so much
char for register typeof
class friend return union
I will score good marks in C++
const goto short unsigned
continue if signed virtual
default inline sizeof void
delete int static volatile
do long struct while

Unit-2 C++ Basics A D Patel Institute of Technology 16


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.
I4. like C++
Only first so much
31 characters are significant.

I will score good marks in C++

Unit-2 C++ Basics A D Patel Institute of Technology 17


Valid, Invalid Identifiers
1) Darshan Valid 12) xyz123 Valid
2) A Valid 13) part#2 Invalid
3) Age 14) "char"
Valid Invalid
4) void 15) #include
Reserved word Invalid
5) MAX-ENTRIES 16) This_is_a_
I like C++ so much17) _xyz
6) double Invalid Valid

I will score
7) time
8) G Valid
good
Reserved word
marks
18) 9xyz
19) main
in C++
Valid
Invalid
9) Sue's 20) mutable
Valid Standard identifier
10) return 21) double
Invalid Reserved word
11) cout 22) max?out
Reserved word Reserved word
Standard identifier Invalid
Unit-2 C++ Basics A D Patel Institute of Technology 18
Constants / Literals
 Constants in C++ refer to fixed values that do not change during
execution of program.

CONSTANTS

I like C++ so much


I will score
CONSTANTS good marks in
NUMERIC
C++
CHARACTER
CONSTANTS

SINGLE
INTEGER REAL STRING
CHARACTER
CONSTANTS CONSTANTS CONSTANTS
CONSTANTS
i.e. i.e. i.e.
i.e.
123,-321, 6543 0.0083, -0.75 “Hello”, “197”
‘5’, ‘X’, ‘;’
Unit-2 C++ Basics A D Patel Institute of Technology 19
C++ Operators
C++ Operators
 All C language operators are valid in C++.
1. Arithmetic operators (+, - , *, /, %)
2. Relational operators (<, <=, >, >=, ==, !=)
3. Logical operators (&&, ||, !)
I like C++ operators
4. Assignment so much (+=, -=, *=, /=)

I will score good marks in


5. Increment and decrement operators (++, --)
6. Conditional operators (?:)
C++
7. Bitwise operators (&, |, ^, <<, >>)
8. Special operators ()

Unit-2 C++ Basics A D Patel Institute of Technology 21


Arithmetic Operators

Operator example Meaning


+ a+b Addition

I like C++ so much


- a–b Subtraction
* a*b
Multiplication
I will score goodDivision
/ marks in C++
a/b
% a%b Modulo division- remainder

Unit-2 C++ Basics A D Patel Institute of Technology 22


Relational Operators

Operator Meaning
< Is less than
<= Is less than or equal to
I like C++ so> much
Is greater than

I will score== good


>=
marks in C++
Is greater than or equal to
Equal to
!= Not equal to

Unit-2 C++ Basics A D Patel Institute of Technology 23


Logical Operators
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT

I liketrue
C++ sotrue
a b
muchatrue
&& b a || b
true
I willtruescore false
good marks
false intrueC++
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

Unit-2 C++ Basics A D Patel Institute of Technology 24


Assignment operator
 We assign a value to a variable using the basic assignment
operator (=).
 Assignment operator stores a value in memory.
 The syntax is
I like C++leftSide
so much = rightSide ;

I will score good marks in C++


It is either a literal |
Always it is a
a variable identifier |
variable identifier.
an expression.

Literal: ex. i = 1;
Variable identifier: ex. start = i;
Expression: ex. sum = first + second;
Unit-2 C++ Basics A D Patel Institute of Technology 25
Assignment Operators (Shorthand)
Syntax:
leftSide Op= rightSide ;

It is an arithmetic
operator.
IEx:like C++ so much
Simple assignment
Shorthand operator
operator
I will score gooda =marks
x=x+3;
x+=3; a+1
in C++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
Unit-2 C++ Basics A D Patel Institute of Technology 26
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:
I like C++ x=100;so much
x++;
IAfter
will scorethegood
the execution value of x marks
will be 101. in C++
Example:
x=100;
x--;
After the execution the value of x will be 99.

Unit-2 C++ Basics A D Patel Institute of Technology 27


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 ; After execution

I like C++ so much


p = ++x;
First increment value of
x by one
x will be 11
p will be 11

I will score goodDescription


Operator marks in C++
Post increment operator (x++) value of x is incremented after assigning it
to the variable on the left

x = 10 ; After execution
p = x++; x will be 11
p will be 10
First assign value of x

Unit-2 C++ Basics A D Patel Institute of Technology 28


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;
I like C++ so much
x = 5;
y = x++ * ++x;
cout << x << y;
I will score good marks in C++
}

(A) 749735
(B) 736749
(C) 367497
(D) none of the mentioned

Unit-2 C++ Basics A D Patel Institute of Technology 29


Conditional Operator
Syntax:
exp1 ? exp2 : exp3
Working of the ? Operator:
 exp1 is evaluated first
I like C++ so much
• if exp1 is true(nonzero) then
- exp2 is evaluated and its value becomes the value of the

I will score good marks in C++


expression
• If exp1 is false(zero) then
- exp3 is evaluated and its value becomes the value of the
Ex:
expression Ex:
m=2; m=2;
n=3; n=3;
r=(m>n) ? m : r=(m<n) ? m :
n; n;
Value of r will be 3 Value of r will be 2
Unit-2 C++ Basics A D Patel Institute of Technology 30
Bitwise Operator
Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
I like C++ so much <<
>>
Shift left
Shift right
I will score good marks in C++

Unit-2 C++ Basics A D Patel Institute of Technology 31


Bitwise Operator Examples
8 = 1000 (In
Binary)
Bitwise & (AND) 6 = 0110 (In Bitwise | (OR)
Binary)
int a=8,b=6,c; int a=8,b=6,c;
c = a & b; c = a | b;
I like C++ so much
cout<<"Output ="<< c; cout<<"Output ="<< c;
Output = 0 Output = 14
I will
Bitwise score
<< (Shift Left) good marks in Right)
Bitwise >> (Shift C++
int a=8,b=6,c; int a=8,b=6,c;
c = a << 1; c = a >> 1;
cout<<"Output ="<< c; cout<<"Output ="<< c;
Output = 16 Output = 4
left shifting is the equivalent right shifting is the
of multiplying a by a power of equivalent of dividing a by a
two power of two
Unit-2 C++ Basics A D Patel Institute of Technology 32
New Operators in C++
It allows to access to the global
version of variable
:: Scope Resolution Declares a pointer to a member of
Pointer-to-member declarator a class
::*
->* Pointer-to-member operator To access pointer to class members

I .*likePointer-to-member
C++ so much operator To access pointer to data members
of class
I willMemory
score good
new Memory allocation operator
release operator
marks in
Allocates C++
memory at run time
delet
e
endl Line feed operator Deallocates memory at run time

setw Field width operator It is a manipulator causes a linefeed


to be inserted
It is a manipulator specifies a field
width for printing value
Unit-2 C++ Basics A D Patel Institute of Technology 33
Scope Resolution
Operator
Scope Resolution Operator(::)
....
....
Declaration of x in inner block hides
{ declaration of same variable declared in an
int x=10; outer block.
....
.... Therefore, in this code both variable x

I like C++ so much


{ refers to different data.
int x=1; Block-1
.... Block-2
I will score good
}
.... marks
 In C language, valuein C++ in Block-1 is
of x declared
not accessible in Block-2.
....  In C++, using scope resolution operator (::),
}
value of x declared in Block-1 can be accessed
in Block-2.

Unit-2 C++ Basics A D Patel Institute of Technology 35


#include <iostream> Scope resolution example
using namespace std;
int m=10; Global declaration of variable m
int main()
{
int m=20; variable m declared , local to main
{
int k=m;
int m=3;
cout<<"we are in inner block\
n"; variable m
cout<<"k="<<k<<endl; declared again local to inner block
cout<<"m="<<m<<endl;
cout<<"::m="<<::m<<endl; Output:
} we are in inner block
cout<<"we are in outer block\ k=20
n"; m=3
cout<<"m="<<m<<endl; ::m=10
cout<<"::m="<<::m<<endl; we are in outer block
return 0; m=20
} ::m=10
C++ Data Types
Basic Data types
C++ datatypes

User-defined Built-in Derived


I like C++ so much
structure
union
array
function
I will score good marks in C++
class
enumeration
pointer
reference

Integral Void Floating

int char float double

Unit-2 C++ Basics A D Patel Institute of Technology 38


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
I like C++ so much
unsigned int 2 0 to 65535
I will score good
long 4 marks
-2147483648into 2147483647
C++
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

Unit-2 C++ Basics A D Patel Institute of Technology 39


Type Conversion
Type Conversion
 Type Conversion is the process of converting one predefined data
type into another data type.

Type Conversion
I like C++ so much
I will score good
Implicit
marks in
Explicit
C++
(Automatically converts (Forcefully converts one
one datatype to another datatype to another
datatype) datatype)

 Explicit type conversion is also known as type casting.

Unit-2 C++ Basics A D Patel Institute of Technology 41


Type Conversion(Cont…)
int a;
double b=2.55;
a = b; // implicit type conversion
Icout
like<<C++
a << so much
endl; // this will print

I will score good marks in C++


2
a = int(b); //explicit type
conversion
cout << a << endl; // this will print
2

Unit-2 C++ Basics A D Patel Institute of Technology 42


Implicit type conversion hierarchy

long
I like C++ so much float double double

I will score intgood marks in C++


long int
unsigned
int
char

Unit-2 C++ Basics A D Patel Institute of Technology 43


Implicit Type Conversion
#include <iostream>
using namespace std;
ans = count * avg
int main()
10.0
{ 5
1
int count = 5;
I like C++ so much
float avg = 10.01;
double ans;
double int float

I will score good


ans = count * avg;
marks
5.0 in C++ *
float 50.0
50.0
5
5
cout<<"Answer=:"<<ans; float
double
return 0;
} Output:
Answer = 50.05
Unit-2 C++ Basics A D Patel Institute of Technology 44
Type Casting
 In C++ explicit type conversion is called type casting.
 Syntax
type-name (expression) //C++ notation
 Example
I likeaverage
C++ so much i; //C notation
= sum/(float)

I will score good marks in C++


average = sum/float (i); //C++ notation

Unit-2 C++ Basics A D Patel Institute of Technology 45


#include <iostream>
using namespace std;
int main()
Type Casting Example
{
int a, b, c;
a = 19.99 + 11.99;
//adds the values as float
// then converts the result to int
b = (int) 19.99 + (int) 11.99;
// old C
c = int (19.99) + int (11.99);
syntax
// new C++
syntax
cout << "a = " << a << ", b = " << b;
cout << ", c = " << c << endl;

char ch = 'Z';
cout << "The code for " << ch << " is //print
"; as
cout << int(ch) << endl;//print as intchar
return 0;
}
Output:
a = 31, b = 30, c = 30
The code for Z is 90
Reference Variable
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.
declares variable a
int a=5;
I like C++ so much OUTPUT
int &ans = a; declares ans as reference to a
Its necessary to
Icout<<"&a="<<&a<<endl;
will score good marks
cout<<"a="<<a<<endl; a=5 in C++
initialize the
&a=0x6ffe34 Reference at the
time of declaration
cout<<"ans="<<ans<<endl; ans=5
cout<<"&ans="<<&ans<<endl;&ans=0x6ffe34
ans++;
cout<<"a="<<a<<endl; a=6
cout<<"ans="<<ans<<endl; ans=6

Unit-2 C++ Basics A D Patel Institute of Technology 48


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
I like C++ so much
reference) is located within a different scope.

I will score good marks in C++

Unit-2 C++ Basics A D Patel Institute of Technology 49


Reference Vs Pointer
References Pointers
int i; int *p = &i;
int &r = i;

I like C++ so much p


addr
I will score good marks in C++
r i
addr
 A reference is a  A pointer is a variable
variable which refers which stores the address
to another variable. of another variable.

Unit-2 C++ Basics A D Patel Institute of Technology 50


Enumeration
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};
0 1 2 3 4 5 6
I like C++
Keyword
Tag
so much
I will score good marks in C++
name Integer Values for symbolic constants

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

Unit-2 C++ Basics A D Patel Institute of Technology 52


Enumeration Behaviour(Cont…)
enum coin { penny, nickel, dime, quarter=100,
half_dollar, dollar};

The values of these symbols are


penny 0
I like C++ so muchnickel
dime
1
2
I will score good marks in C++
quarter
half_dollar 101
100

dollar 102

Unit-2 C++ Basics A D Patel Institute of Technology 53


Enumeration Behaviour
enum days{ sun, mon, tue, wed, thu, fri, sat
};
days today; variable today declared of type days
Valid, because tue is an enumerator. Value 2 will
today = tue; be assigned in today

today = 6; Invalid, because 6 is not an enumerator


Invalid, today is of type days. We can not apply +
today++; + to structure variable also

today = mon + fri; Invalid


Valid, days data type converted to int,
int num = sat; value 6 will be assigned to num
Valid, mon converted to int with value 1
num = 5 + mon;
Control Structures
Control Structures
 The if statement:
• Simple if statement
• if…else statement
• else…if ladder
I like C++nested
• if…else so much
I will score good
 The switch statement :
marks in C++
The do-while statement: An exit controlled loop
 The while Statement: An entry controlled loop
 The for statement: An entry controlled loop

Unit-2 C++ Basics A D Patel Institute of Technology 56


Thank You

You might also like