Presentations PPT Unit-2 OOP
Presentations PPT Unit-2 OOP
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.
}
I like C++ so much
return 0;
}
I like C++ so much
return 0;
}
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.
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;
cout<<"Addition : ";
cout<<number1+number2; //Display Addition
return 0;
}
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
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 (+=, -=, *=, /=)
Operator Meaning
< Is less than
<= Is less than or equal to
I like C++ so> much
Is greater than
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
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.
x = 10 ; After execution
x = 10 ; After execution
p = x++; x will be 11
p will be 10
First assign value of x
(A) 749735
(B) 736749
(C) 367497
(D) none of the mentioned
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
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)
long
I like C++ so much float double double
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
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
dollar 102