unit 1 oocp
unit 1 oocp
unit 1 oocp
Name: xyz
City: Rajkot
Country: India
Program: Basic C++ program
#include <iostream>
using namespace std;
int main()
{
cout << "Name: xyz";
cout << "City: Rajkot";
cout << "Country: India";
return 0;
}
Output
Name: DarshanCity: RajkotCountry: India
Program: Basic C++ program(Cont…)
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main() int main()
{ {
cout << "Name: xyz\n"; cout << "Name: xyz"<<endl;
cout << "City: Rajkot\n"; cout << "City: Rajkot"<<endl;
cout << "Country: India"; cout << "Country: India"<<endl;
return 0; return 0;
} }
cout<<"Addition : ";
cout<<number1+number2; //Display Addition
return 0;
}
C++ Tokens
C++ Tokens
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;
int main()
− Constants
{
− Strings cout << "Hello World";
− Special Symbols return 0;
}
− Operators
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
CONSTANTS
NUMERIC CHARACTER
CONSTANTS 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’, ‘;’
C++ Operators
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 ()
Arithmetic 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
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
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;
Assignment Operators (Shorthand)
Syntax:
leftSide Op= rightSide ;
It is an arithmetic
operator.
x = 10 ; After execution
p = ++x; x will be 11
First increment value of p will be 11
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 ; After execution
p = x++; x will be 11
p will be 10
First assign value of x
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
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: Ex:
m=2; m=2;
n=3; n=3;
r=(m>n) ? m : n; r=(m<n) ? m : n;
Value of r will be 3 Value of r will be 2
Bitwise Operator
Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< Shift left
>> Shift right
Bitwise Operator Examples
8 = 1000 (In Binary)
6 = 0110 (In Binary)
Bitwise & (AND) Bitwise | (OR)
int a=8,b=6,c; int a=8,b=6,c;
c = a & b; c = a | b;
cout<<"Output ="<< c; cout<<"Output ="<< c;
Output = 0 Output = 14
Bitwise << (Shift Left) Bitwise >> (Shift Right)
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 of right shifting is the equivalent
multiplying a by a power of two of dividing a by a power of two
New Operators in C++
It allows to access to the global
version of variable
:: Scope Resolution Declares a pointer to a member of
a class
::* Pointer-to-member declarator
->* Pointer-to-member operator To access pointer to class members
Type Conversion
Implicit Explicit
(Automatically converts (Forcefully converts one
one datatype to another datatype to another
datatype) datatype)
long
double double
float
long int
unsigned
int int
char
Implicit Type Conversion
#include <iostream>
using namespace std;
ans = count * avg
int main()
{ 5 10.01
int count = 5;
float avg = 10.01; double int float
double ans;
5.0 *
ans = count * avg;
float
50.05
cout<<"Answer=:"<<ans; 50.05
return 0; float
double
}
Output:
Answer = 50.05
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()
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 syntax
c = int (19.99) + int (11.99); // new C++ syntax
char ch = 'Z';
cout << "The code for " << ch << " is "; //print as char
cout << int(ch) << endl; //print as int
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;
int &ans = a; declares ans as reference to a
OUTPUT Its necessary to
cout<<"a="<<a<<endl; a=5 initialize the
cout<<"&a="<<&a<<endl; &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
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.
Reference Vs Pointer
References Pointers
int i; int *p = &i;
int &r = i;
p
addr
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.
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
Keyword
Tag
name Integer Values for symbolic constants
• It is a template
• #include<stdio.h>
• #include<conio.h>
• void area(float r);
• void main()
• {
• float x;
• printf("enter radius of circle");
• scanf("%f",&x);
Function with arguments but no return
• area(x);
• getch();
value
• }
• void area(float r)
• {
• float ar;
• ar=3.14*r*r;
• printf("the area of circle is %f",ar);
• }
Function with Arguments and Return
Value
• #include<stdio.h>
• #include<conio.h>
• float area(float r);
• int main()
• {
• float x,ans;
Function with arguments and return
• printf("enter radius of circle");
• scanf("%f",&x);
value.
• ans=area(x);
• printf("%f the area of a circle is",ans);
• return 0; }
• }
• float area(float r)
• {
• float ar;
• ar=3.14*r*r;
• return(ar);
• }
Function with no arguments and
return value
• #include<stdio.h>
• #include<conio.h>
• float area();
• int main()
• { float ans;
•
• ans=area();
• printf("%f the area of a circle is",ans);
• return 0; Function with no arguments and
• }
• float area()
return value
• {
• float x,ar;
• printf("enter radius of circle");
• scanf("%f",&x);
•
• ar=3.14*x*x;
• return(ar);
• }
C++ Programming Recursion
• A function that calls itself is known as recursive function and the process of
calling function itself is known as recursion in C++ programming.
• Example of recursion in C++ programming
• Write a C++ program to find sum of first n natural numbers using recursion.
Note: Positive integers are known as natural number i.e. 1, 2, 3....n
• #include <iostream>
• using namespace std;
• int sum(int n);
• int main()
• {
• int num,add;
• cout<<"Enter a positive integer:\n";
• cin>>num;
• add=sum(num);
• cout<<add;
• }
• int sum(int n){
• if(n==0)
• return n;
• else
• return n+sum(n-1); /*self call to function sum() */
• }
• sum(5)
• =5+sum(4)
• =5+4+sum(3)
• =5+4+3+sum(2)
• =5+4+3+2+sum(1)
• =5+4+3+2+1+sum(0)
• =5+4+3+2+1+0
• =5+4+3+2+1
• =5+4+3+3
• =5+4+6
• =5+10
• =15
• Every recursive function must be provided with a way to end the recursion. In this
example when, n is equal to 0, there is no recursive call and recursion ends.
Recursion of Factorial Number
Call by Value
• The function does not have access to the actual variables in the calling
program and can only work on the copies of values.
#include<iostream>
//#include<conio.h>
uisng namespace std;
int swap(int ,int );
int main()
{ int a,b;
cout<<“enter a,b”;
cin>>a>>b;
swap(a,b);
return 0;
}
• int swap(int a,int b)
•{
• int t=a;
• a=b;
• b=t;
• cout<<a<<“ “<<b;
•}
Call by Reference
This means that when the function is working with its own arguments, it is
actually working on the original data.
#include<iostream>
using namespace std;
int swap(int &,int &);
int main()
{
int a,b;
cout<<“enter a,b”;
cin>>a>>b;
swap(a,b);
cout<<a<<b;
return 0;
}
• int swap(int &a,int &b)
•{
• int t=a;
• a=b;
• b=t;
•}
Inline Function
• We mentioned that functions save memory space because all the calls to the
function cause the same code to be executed
• The function body need not be duplicated in memory
• When compiler sees the function call, it normally generates a jump to the
function.
• At end of the function it jumps back to the instruction following the call.
• While this sequence of events may save memory space, it takes some extra
time.
• Syntax:- inline function-header
• {
• function body
• }
• #include<iostream.h>
• #include<conio.h>
• inline int max(int a,int b,int c)
• { return (a>b ?((a>c)?a:c) : ((b>c? b: c))); }
• int main()
• { int x,y,z;
• cout<<“Enter x,y,z”; Enter x,y,z
• cin>>x>>y>>z; 1 2 3
• cout<< “Max no is”<<max(x,y,z); getch(); }
Max no is3
Function Overloading
• Auto
• Register
• Static
• External
Auto Storage Class
#include<iostream.h>
#include<conio.h>
void test();
int main()
{ int a=5;
cout << a<<endl;
{ int a=10; //print 10
cout<<a<<endl; }
test();
return 0;
}
• void test()
•{
• int b=6; // local variable to test()
• cout<<b<<endl;
• getch();
•}
Output:
5 10 6
Register Storage Class
•register int a;