SlideShare ist ein Scribd-Unternehmen logo
1 von 42
Prepared By: Asst. Prof. Sejal Jadav
Unit-1
Principles of Object Oriented Programming Tokens,
expressions & Control Statements
(14 MARKS)
B.C.A & B.Sc.(IT) – 3
CS-13 C++ and Object Oriented
Programming
Prepared By: Asst. Prof. Sejal Jadav
Operators
• Input/output operators
• << output / insertion operator
• >> input / extraction operator
• Scope resolution operator
• ::
• Memory management operators
• New-memory allocation
operator
• Delete-memory release operator
• Member dereferencing operator
• ::* pointer-to-member declarator
• ->*pointer-to-member operator
• .* pointer-to-member operator
• Manipulators
• Endl-line feed operator
• Setw-field width operator
• Type cast operator
• Definition : An operator is a symbol that tells the compiler to
perform specific mathematical or logical manipulations.
Prepared By: Asst. Prof. Sejal Jadav
Scope Resolution Operator
• We know that the same variable name can be used in
different blocks in C++
• The scope of the variable extends from the point of its
declaration till the end of the block containing the
declaration.
Prepared By: Asst. Prof. Sejal Jadav
• The variable declared inside the block is called local
variable to that block.
• While blocks are nested the declaration in inner block
hides a declaration of the same variable in an outer
block.
Prepared By: Asst. Prof. Sejal Jadav
• Hence in C – language the global variable cannot be
accessed from the inner block.
• C++ resolves this problem by introducing a new
operator : : called scope resolution operator.
Prepared By: Asst. Prof. Sejal Jadav
Syntax :
: : variable_name
• This operator allows global version of the variable.
• Let’s see Example……. 12_scope_operator.cpp
Prepared By: Asst. Prof. Sejal Jadav
Type cast operator
•Type casting is a way to convert a variable
from one data type to another datatype.
•For example, if you want to store a long value
into a simple integer then you can typecast long
to int.
Prepared By: Asst. Prof. Sejal Jadav
•You can convert values from one type to
another explicitly using the cast operator.
•There are two types of type conversion in C++.
1. Implicit Conversion
2. Explicit Conversion (also known as Type
Casting)
Prepared By: Asst. Prof. Sejal Jadav
Implicit Type Conversion
• The type conversion that is automatically done by the
compiler is known as implicit type conversion.
• This type of conversion is also known as automatic
conversion.
• Let’s look at examples: 01_typecast, 02_typecast.cpp
Prepared By: Asst. Prof. Sejal Jadav
int main() {
int num_int = 9;
double num_double;
num_double = num_int;
cout << "num_int = " << num_int << endl;
cout << "num_double = " << num_double << endl;
}
Output
num_int = 9
num_double = 9
Prepared By: Asst. Prof. Sejal Jadav
• In the program, we have assigned an int data to a
double variable.
num_double = num_int;
• Here, the int value is automatically converted to
double by the compiler before it is assigned to the
num_double variable. This is an example of implicit
type conversion.
Prepared By: Asst. Prof. Sejal Jadav
Data Loss During Conversion (Narrowing Conversion)
• As we have seen from the above example, conversion
from one data type to another is to data loss.
• This happens when data of a larger type is converted
to data of a smaller type.
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
C++ Explicit Conversion
• When the user manually changes data from one type
to another, this is known as explicit conversion.
• This type of conversion is also known as type
casting.
Prepared By: Asst. Prof. Sejal Jadav
• There are three major ways in which we can use
explicit conversion in C++. They are:
1. C-style type casting (also known as cast notation)
2. Function notation (also known as old C++ style
type casting)
3. Type conversion operators
Prepared By: Asst. Prof. Sejal Jadav
C-style Type Casting
• As the name suggests, this type of casting is favored
by the C programming language. It is also known as
cast notation.
• The syntax for this style is:
(data_type)expression;
Prepared By: Asst. Prof. Sejal Jadav
For example,
// initializing int variable
int num_int = 26;
// declaring double variable
double num_double;
// converting from int to double
num_double = (double)num_int;
Prepared By: Asst. Prof. Sejal Jadav
Function-style Casting
• We can also use the function like notation to cast data
from one type to another.
• The syntax for this style is:
data_type(expression);
Prepared By: Asst. Prof. Sejal Jadav
For example,
// initializing int variable
int num_int = 26;
// declaring double variable
double num_double;
// converting from int to double
num_double = double(num_int);
Prepared By: Asst. Prof. Sejal Jadav
Manipulators
• Manipulators are operators used in C++ for
formatting output.
• The data is manipulated by the programmer's choice
of display.
• 1. endl Manipulator
• 2. setw Manipulator
Prepared By: Asst. Prof. Sejal Jadav
endl
• endl is the line feed operator in C++.
• It acts as a stream manipulator whose purpose is to
feed the whole line and then point the cursor to the
beginning of the next line.
• We can use n (n is an escape sequence) instead
of endl for the same purpose.
Prepared By: Asst. Prof. Sejal Jadav
setw
• This manipulator sets the minimum field width on
output.
Syntax:
setw(x)
• Example: 04_ manipulator.cpp
Prepared By: Asst. Prof. Sejal Jadav
•main() function is the entry point of any C++
program.
•It is the point at which execution of program is
started.
main function in c++
Prepared By: Asst. Prof. Sejal Jadav
•When a C++ program is executed, the execution
control goes directly to the main() function.
•Every C++ program have a main() function.
Prepared By: Asst. Prof. Sejal Jadav
Syntax
main() [OR int main()]
{
//body of program
return 0; //optional
}
• In the above syntax …..
Prepared By: Asst. Prof. Sejal Jadav
int :
• This is perfectly valid because the main() doesn’t
return any value to the operating system.
• In C++, by default, the main() returns int type
value to the operating system.
Prepared By: Asst. Prof. Sejal Jadav
•Here int keyword in the main() function
declaration is optional.
•If return statement at the end is missing,
most compilers will generate the warning
that the function should return a value.
Prepared By: Asst. Prof. Sejal Jadav
main:
•Main() is a name of function which is
predefined function in C++ library.
Prepared By: Asst. Prof. Sejal Jadav
#include<iostream.h>
int main()
{
cout<< “CCSIT College - Junagadh”;
return 0;
}
Prepared By: Asst. Prof. Sejal Jadav
Functions in c++
• What is Function?
• Definition, Declaration & Call
• Declaration
• Way to define a function
• Formal & Actual Arguments
• Types of formal arguments
• Call by value, call by address and call by reference
Prepared By: Asst. Prof. Sejal Jadav
What is Function?
• Function is block of code performing a unit task.
• Function definition is block of code
• Function is a way to achieve modularization. (no. of small
functions)
• Function are of two types : Library (predefined) function
and user –defined
• Predefined functions are
• declared in header files and
• defined in library files
Prepared By: Asst. Prof. Sejal Jadav
Function Definition, Declaration & Call
#include<iostream>
Using namespace std;
Main()
{
Void fun();
Cout<<“You are in main”;
Fun();
}
Void fun()
{
Cout<<“you are in fun”;
}
Function Definition
Function Call
Function Declaration
Declaration of cout & cin
Prepared By: Asst. Prof. Sejal Jadav
Function Declaration [Function PROTOTYPE]
• Function Declaration is also known as Function
Prototype
• Functions need to be declared before use (just like
variable)
• Function can be declared locally or globally
• Function has a name, return type and arguments.
Syntax:
• ReturnType functionName(argumentList);
Prepared By: Asst. Prof. Sejal Jadav
Way to define a function
• No argument no return value
• With argument no return value
• No argument with return value
• With argument with return value
Prepared By: Asst. Prof. Sejal Jadav
5
6
Formal & Actual Arguments
#include<iostream>
Int sum(int,int);
Main()
{
Int a=5,b=6;
Int s=sum(a,b);
Cout<<“Sum is :
“<<s;
}
Int sum(int x, int y)
{
Return (x+y);
}
a
b
s
a & b are actual argument
Sum() memory
block
main()
memory
block
x & y are formal argument
5
6
11
x
y
Prepared By: Asst. Prof. Sejal Jadav
• Let’s see example :callbyvalue.cpp
Prepared By: Asst. Prof. Sejal Jadav
Types of Formula Argument
• Formal arguments can be of three types
1. Ordinary variables of any type
2. Pointer variables
3. Reference variables
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Call by reference
• When formal arguments are reference variables, it is
function call by reference.
• The call by reference method the called function
does not create its own copy rather it refers to
original value only by different name i.e. reference.
Prepared By: Asst. Prof. Sejal Jadav
• When we pass arguments by reference, the formal
arguments int sum(int &x,int &y) in the called
function becomes aliases to the actual arguments in
the calling function.
• It means that when the called function is working
with its own arguments, it is actually working on the
original data.

Weitere ähnliche Inhalte

Was ist angesagt?

CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - ReferenceMohammed Sikander
 
Inline function
Inline functionInline function
Inline functionTech_MX
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default argumentsNikhil Pandit
 
C++ Function
C++ FunctionC++ Function
C++ FunctionHajar
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)Ritika Sharma
 
C++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIAC++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIADheeraj Kataria
 
Learning C++ - Functions in C++ 3
Learning C++ - Functions  in C++ 3Learning C++ - Functions  in C++ 3
Learning C++ - Functions in C++ 3Ali Aminian
 
Functions in C++
Functions in C++Functions in C++
Functions in C++home
 
Function in c++
Function in c++Function in c++
Function in c++Kumar
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT03062679929
 

Was ist angesagt? (19)

CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
C++ functions
C++ functionsC++ functions
C++ functions
 
functions of C++
functions of C++functions of C++
functions of C++
 
Inline function
Inline functionInline function
Inline function
 
Parameter passing to_functions_in_c
Parameter passing to_functions_in_cParameter passing to_functions_in_c
Parameter passing to_functions_in_c
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Function C++
Function C++ Function C++
Function C++
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)
 
C++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIAC++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIA
 
Learning C++ - Functions in C++ 3
Learning C++ - Functions  in C++ 3Learning C++ - Functions  in C++ 3
Learning C++ - Functions in C++ 3
 
C++ Functions
C++ FunctionsC++ Functions
C++ Functions
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Function in c++
Function in c++Function in c++
Function in c++
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 

Ähnlich wie C++ unit-1-part-11

Ähnlich wie C++ unit-1-part-11 (20)

C++ unit-1-part-9
C++ unit-1-part-9C++ unit-1-part-9
C++ unit-1-part-9
 
C++ unit-1-part-10
C++ unit-1-part-10C++ unit-1-part-10
C++ unit-1-part-10
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Functions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptxFunctions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptx
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
 
Ppt of c vs c#
Ppt of c vs c#Ppt of c vs c#
Ppt of c vs c#
 
Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++
 
C++ unit-1-part-13
C++ unit-1-part-13C++ unit-1-part-13
C++ unit-1-part-13
 
Esoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingEsoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programming
 
C_plus_plus
C_plus_plusC_plus_plus
C_plus_plus
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
 
C++
C++C++
C++
 
Lec-1c.pdf
Lec-1c.pdfLec-1c.pdf
Lec-1c.pdf
 
Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented Technologies
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
 

Mehr von Jadavsejal

Programming with Java Concept: Java TimeZone Class
Programming with Java Concept: Java TimeZone ClassProgramming with Java Concept: Java TimeZone Class
Programming with Java Concept: Java TimeZone ClassJadavsejal
 
Programming Java Concept of Event Handling
Programming Java Concept of Event HandlingProgramming Java Concept of Event Handling
Programming Java Concept of Event HandlingJadavsejal
 
Programming Java GUI using SWING, Event Handling
Programming Java GUI using SWING, Event HandlingProgramming Java GUI using SWING, Event Handling
Programming Java GUI using SWING, Event HandlingJadavsejal
 
concept of applet and concept of Layout Managers
concept of applet and concept of Layout Managersconcept of applet and concept of Layout Managers
concept of applet and concept of Layout ManagersJadavsejal
 
C++ unit-2-part-3
C++ unit-2-part-3C++ unit-2-part-3
C++ unit-2-part-3Jadavsejal
 
C++ unit-2-part-2
C++ unit-2-part-2C++ unit-2-part-2
C++ unit-2-part-2Jadavsejal
 
C++ unit-2-part-1
C++ unit-2-part-1C++ unit-2-part-1
C++ unit-2-part-1Jadavsejal
 
C++ unit-1-part-15
C++ unit-1-part-15C++ unit-1-part-15
C++ unit-1-part-15Jadavsejal
 
C++ unit-1-part-14
C++ unit-1-part-14C++ unit-1-part-14
C++ unit-1-part-14Jadavsejal
 
C++ unit-1-part-8
C++ unit-1-part-8C++ unit-1-part-8
C++ unit-1-part-8Jadavsejal
 
C++ unit-1-part-7
C++ unit-1-part-7C++ unit-1-part-7
C++ unit-1-part-7Jadavsejal
 
C++ unit-1-part-6
C++ unit-1-part-6C++ unit-1-part-6
C++ unit-1-part-6Jadavsejal
 
C++ unit-1-part-5
C++ unit-1-part-5C++ unit-1-part-5
C++ unit-1-part-5Jadavsejal
 
C++ unit-1-part-4
C++ unit-1-part-4C++ unit-1-part-4
C++ unit-1-part-4Jadavsejal
 
C++ unit-1-part-2
C++ unit-1-part-2C++ unit-1-part-2
C++ unit-1-part-2Jadavsejal
 
C++ unit-1-part-3
C++ unit-1-part-3C++ unit-1-part-3
C++ unit-1-part-3Jadavsejal
 
C++-Unit-1-Part-1
C++-Unit-1-Part-1C++-Unit-1-Part-1
C++-Unit-1-Part-1Jadavsejal
 
05_system architecture
05_system architecture05_system architecture
05_system architectureJadavsejal
 
04 data flow architecture
04 data flow architecture 04 data flow architecture
04 data flow architecture Jadavsejal
 

Mehr von Jadavsejal (20)

Programming with Java Concept: Java TimeZone Class
Programming with Java Concept: Java TimeZone ClassProgramming with Java Concept: Java TimeZone Class
Programming with Java Concept: Java TimeZone Class
 
Programming Java Concept of Event Handling
Programming Java Concept of Event HandlingProgramming Java Concept of Event Handling
Programming Java Concept of Event Handling
 
Programming Java GUI using SWING, Event Handling
Programming Java GUI using SWING, Event HandlingProgramming Java GUI using SWING, Event Handling
Programming Java GUI using SWING, Event Handling
 
concept of applet and concept of Layout Managers
concept of applet and concept of Layout Managersconcept of applet and concept of Layout Managers
concept of applet and concept of Layout Managers
 
C++ unit-2-part-3
C++ unit-2-part-3C++ unit-2-part-3
C++ unit-2-part-3
 
C++ unit-2-part-2
C++ unit-2-part-2C++ unit-2-part-2
C++ unit-2-part-2
 
C++ unit-2-part-1
C++ unit-2-part-1C++ unit-2-part-1
C++ unit-2-part-1
 
C++ unit-1-part-15
C++ unit-1-part-15C++ unit-1-part-15
C++ unit-1-part-15
 
C++ unit-1-part-14
C++ unit-1-part-14C++ unit-1-part-14
C++ unit-1-part-14
 
C++ unit-1-part-8
C++ unit-1-part-8C++ unit-1-part-8
C++ unit-1-part-8
 
C++ unit-1-part-7
C++ unit-1-part-7C++ unit-1-part-7
C++ unit-1-part-7
 
C++ unit-1-part-6
C++ unit-1-part-6C++ unit-1-part-6
C++ unit-1-part-6
 
C++ unit-1-part-5
C++ unit-1-part-5C++ unit-1-part-5
C++ unit-1-part-5
 
C++ unit-1-part-4
C++ unit-1-part-4C++ unit-1-part-4
C++ unit-1-part-4
 
C++ unit-1-part-2
C++ unit-1-part-2C++ unit-1-part-2
C++ unit-1-part-2
 
C++ unit-1-part-3
C++ unit-1-part-3C++ unit-1-part-3
C++ unit-1-part-3
 
C++-Unit-1-Part-1
C++-Unit-1-Part-1C++-Unit-1-Part-1
C++-Unit-1-Part-1
 
05_system architecture
05_system architecture05_system architecture
05_system architecture
 
04 data flow architecture
04 data flow architecture 04 data flow architecture
04 data flow architecture
 
Vpn protocols
Vpn protocolsVpn protocols
Vpn protocols
 

Kürzlich hochgeladen

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxdhanalakshmis0310
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 

Kürzlich hochgeladen (20)

Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 

C++ unit-1-part-11

  • 1. Prepared By: Asst. Prof. Sejal Jadav Unit-1 Principles of Object Oriented Programming Tokens, expressions & Control Statements (14 MARKS) B.C.A & B.Sc.(IT) – 3 CS-13 C++ and Object Oriented Programming
  • 2. Prepared By: Asst. Prof. Sejal Jadav Operators • Input/output operators • << output / insertion operator • >> input / extraction operator • Scope resolution operator • :: • Memory management operators • New-memory allocation operator • Delete-memory release operator • Member dereferencing operator • ::* pointer-to-member declarator • ->*pointer-to-member operator • .* pointer-to-member operator • Manipulators • Endl-line feed operator • Setw-field width operator • Type cast operator • Definition : An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.
  • 3. Prepared By: Asst. Prof. Sejal Jadav Scope Resolution Operator • We know that the same variable name can be used in different blocks in C++ • The scope of the variable extends from the point of its declaration till the end of the block containing the declaration.
  • 4. Prepared By: Asst. Prof. Sejal Jadav • The variable declared inside the block is called local variable to that block. • While blocks are nested the declaration in inner block hides a declaration of the same variable in an outer block.
  • 5. Prepared By: Asst. Prof. Sejal Jadav • Hence in C – language the global variable cannot be accessed from the inner block. • C++ resolves this problem by introducing a new operator : : called scope resolution operator.
  • 6. Prepared By: Asst. Prof. Sejal Jadav Syntax : : : variable_name • This operator allows global version of the variable. • Let’s see Example……. 12_scope_operator.cpp
  • 7. Prepared By: Asst. Prof. Sejal Jadav Type cast operator •Type casting is a way to convert a variable from one data type to another datatype. •For example, if you want to store a long value into a simple integer then you can typecast long to int.
  • 8. Prepared By: Asst. Prof. Sejal Jadav •You can convert values from one type to another explicitly using the cast operator. •There are two types of type conversion in C++. 1. Implicit Conversion 2. Explicit Conversion (also known as Type Casting)
  • 9. Prepared By: Asst. Prof. Sejal Jadav Implicit Type Conversion • The type conversion that is automatically done by the compiler is known as implicit type conversion. • This type of conversion is also known as automatic conversion. • Let’s look at examples: 01_typecast, 02_typecast.cpp
  • 10. Prepared By: Asst. Prof. Sejal Jadav int main() { int num_int = 9; double num_double; num_double = num_int; cout << "num_int = " << num_int << endl; cout << "num_double = " << num_double << endl; } Output num_int = 9 num_double = 9
  • 11. Prepared By: Asst. Prof. Sejal Jadav • In the program, we have assigned an int data to a double variable. num_double = num_int; • Here, the int value is automatically converted to double by the compiler before it is assigned to the num_double variable. This is an example of implicit type conversion.
  • 12. Prepared By: Asst. Prof. Sejal Jadav Data Loss During Conversion (Narrowing Conversion) • As we have seen from the above example, conversion from one data type to another is to data loss. • This happens when data of a larger type is converted to data of a smaller type.
  • 13. Prepared By: Asst. Prof. Sejal Jadav
  • 14. Prepared By: Asst. Prof. Sejal Jadav C++ Explicit Conversion • When the user manually changes data from one type to another, this is known as explicit conversion. • This type of conversion is also known as type casting.
  • 15. Prepared By: Asst. Prof. Sejal Jadav • There are three major ways in which we can use explicit conversion in C++. They are: 1. C-style type casting (also known as cast notation) 2. Function notation (also known as old C++ style type casting) 3. Type conversion operators
  • 16. Prepared By: Asst. Prof. Sejal Jadav C-style Type Casting • As the name suggests, this type of casting is favored by the C programming language. It is also known as cast notation. • The syntax for this style is: (data_type)expression;
  • 17. Prepared By: Asst. Prof. Sejal Jadav For example, // initializing int variable int num_int = 26; // declaring double variable double num_double; // converting from int to double num_double = (double)num_int;
  • 18. Prepared By: Asst. Prof. Sejal Jadav Function-style Casting • We can also use the function like notation to cast data from one type to another. • The syntax for this style is: data_type(expression);
  • 19. Prepared By: Asst. Prof. Sejal Jadav For example, // initializing int variable int num_int = 26; // declaring double variable double num_double; // converting from int to double num_double = double(num_int);
  • 20. Prepared By: Asst. Prof. Sejal Jadav Manipulators • Manipulators are operators used in C++ for formatting output. • The data is manipulated by the programmer's choice of display. • 1. endl Manipulator • 2. setw Manipulator
  • 21. Prepared By: Asst. Prof. Sejal Jadav endl • endl is the line feed operator in C++. • It acts as a stream manipulator whose purpose is to feed the whole line and then point the cursor to the beginning of the next line. • We can use n (n is an escape sequence) instead of endl for the same purpose.
  • 22. Prepared By: Asst. Prof. Sejal Jadav setw • This manipulator sets the minimum field width on output. Syntax: setw(x) • Example: 04_ manipulator.cpp
  • 23. Prepared By: Asst. Prof. Sejal Jadav •main() function is the entry point of any C++ program. •It is the point at which execution of program is started. main function in c++
  • 24. Prepared By: Asst. Prof. Sejal Jadav •When a C++ program is executed, the execution control goes directly to the main() function. •Every C++ program have a main() function.
  • 25. Prepared By: Asst. Prof. Sejal Jadav Syntax main() [OR int main()] { //body of program return 0; //optional } • In the above syntax …..
  • 26. Prepared By: Asst. Prof. Sejal Jadav int : • This is perfectly valid because the main() doesn’t return any value to the operating system. • In C++, by default, the main() returns int type value to the operating system.
  • 27. Prepared By: Asst. Prof. Sejal Jadav •Here int keyword in the main() function declaration is optional. •If return statement at the end is missing, most compilers will generate the warning that the function should return a value.
  • 28. Prepared By: Asst. Prof. Sejal Jadav main: •Main() is a name of function which is predefined function in C++ library.
  • 29. Prepared By: Asst. Prof. Sejal Jadav #include<iostream.h> int main() { cout<< “CCSIT College - Junagadh”; return 0; }
  • 30. Prepared By: Asst. Prof. Sejal Jadav Functions in c++ • What is Function? • Definition, Declaration & Call • Declaration • Way to define a function • Formal & Actual Arguments • Types of formal arguments • Call by value, call by address and call by reference
  • 31. Prepared By: Asst. Prof. Sejal Jadav What is Function? • Function is block of code performing a unit task. • Function definition is block of code • Function is a way to achieve modularization. (no. of small functions) • Function are of two types : Library (predefined) function and user –defined • Predefined functions are • declared in header files and • defined in library files
  • 32. Prepared By: Asst. Prof. Sejal Jadav Function Definition, Declaration & Call #include<iostream> Using namespace std; Main() { Void fun(); Cout<<“You are in main”; Fun(); } Void fun() { Cout<<“you are in fun”; } Function Definition Function Call Function Declaration Declaration of cout & cin
  • 33. Prepared By: Asst. Prof. Sejal Jadav Function Declaration [Function PROTOTYPE] • Function Declaration is also known as Function Prototype • Functions need to be declared before use (just like variable) • Function can be declared locally or globally • Function has a name, return type and arguments. Syntax: • ReturnType functionName(argumentList);
  • 34. Prepared By: Asst. Prof. Sejal Jadav Way to define a function • No argument no return value • With argument no return value • No argument with return value • With argument with return value
  • 35. Prepared By: Asst. Prof. Sejal Jadav 5 6 Formal & Actual Arguments #include<iostream> Int sum(int,int); Main() { Int a=5,b=6; Int s=sum(a,b); Cout<<“Sum is : “<<s; } Int sum(int x, int y) { Return (x+y); } a b s a & b are actual argument Sum() memory block main() memory block x & y are formal argument 5 6 11 x y
  • 36. Prepared By: Asst. Prof. Sejal Jadav • Let’s see example :callbyvalue.cpp
  • 37. Prepared By: Asst. Prof. Sejal Jadav Types of Formula Argument • Formal arguments can be of three types 1. Ordinary variables of any type 2. Pointer variables 3. Reference variables
  • 38. Prepared By: Asst. Prof. Sejal Jadav
  • 39. Prepared By: Asst. Prof. Sejal Jadav
  • 40. Prepared By: Asst. Prof. Sejal Jadav
  • 41. Prepared By: Asst. Prof. Sejal Jadav Call by reference • When formal arguments are reference variables, it is function call by reference. • The call by reference method the called function does not create its own copy rather it refers to original value only by different name i.e. reference.
  • 42. Prepared By: Asst. Prof. Sejal Jadav • When we pass arguments by reference, the formal arguments int sum(int &x,int &y) in the called function becomes aliases to the actual arguments in the calling function. • It means that when the called function is working with its own arguments, it is actually working on the original data.