SlideShare a Scribd company logo
OOPS USING
C++(UNIT 2)
BY:MS SURBHI SAROHA
Syllabus
 Program Control Statements
 Sequential Constructs
 Decision Making Construct
 Iteration/Loop Construct
 Arrays
 Functions(User defined Functions, Inline Function, Function overloading)
 User Defined Data Types(Structure, Union and Enumeration)
Program Control Statements
 The conditional statements (also known as decision control structures)
such as if, if else, switch, etc. are used for decision-making purposes in
C/C++ programs.
 They are also known as Decision-Making Statements and are used to
evaluate one or more conditions and make the decision whether to
execute a set of statements or not.
 These decision-making statements in programming languages decide the
direction of the flow of program execution.
Following are the decision-making
statements available in C or C++:
 if Statement
 if-else Statement
 Nested if Statement
 if-else-if Ladder
 switch Statement
 Conditional Operator
 Jump Statements:
 break
 continue
 goto
 return
If-Else
 If-Else statements allow the program to execute different blocks of code
depending on conditionals. All If statements have the following form:
 if ( condition ) {
 //body
 }
For Loop
 A for loop allows for a block of code to be executed until a conditional
becomes false. For loops are usually used when a block of code needs to
executed a fixed number of times.
 Each loop consists of 3 parts, an initialization step, the conditional, and an
iteration step.
 The initialization is run before entering the loop, the condition is checked at
the beginning of each run through the loop ( including the first run ), and the
iteration step executes at the end of each pass through the loop, but before
the condition is rechecked.
 for( initialization ; conditional ; iteration ) {
 // loop body
 }
While Loop
 A while loop is a simple loop that will run the same code over and over as
long as a given conditional is true. The condition is checked at the
beginning of each run through the loop ( including the first one ). If the
conditional is false for the beginning, the while loop will be skipped all
together.
 while ( conditional ) {
 // loop body
 }
Do-while Loop
 A do-while loop acts just like a while loop, except the condition is checked
at the end of each pass through the loop body. This means a do-while
loop will execute at least once.
 do {
 // loop body
 } while ( condition );
Break
 Break is a useful keyword that allows the program to exit a loop or switch
statement before the expected end of a that code block. This is useful in
error checking or if the outcome of a loop is not certain. For example, the
following code will break out of the for loop if a user asks to leave.
 string inputs[10];
 string input;
 for (int i = 0; i < 10; i++ ) {
 cin >> input;
Cont…
 if ( input == "quit" ) {
 break;
 }
 inputs[i] = input;
 }
Arrays
 Arrays are used to store multiple values in a single variable, instead of declaring separate
variables for each value.
 To declare an array, define the variable type, specify the name of the array followed
by square brackets and specify the number of elements it should store.
 #include <iostream>
 #include <string>
 using namespace std;
 int main() {
 string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
 cout << cars[0];
 return 0;
 }
Loop Through an Array
 #include <iostream>
 #include <string>
 using namespace std;
 int main() {
 string cars[5] = {"Volvo", "BMW", "Ford", "Mazda", "Tesla"};
 for (int i = 0; i < 5; i++) {
 cout << cars[i] << "n";
 }
 return 0;
 }
User defined Functions
 #include <iostream>
 using namespace std;
 // Function declaration
 void myFunction();
 // The main method
 int main() {
Cont…..
 myFunction(); // call the function
 return 0;
 }
 // Function definition
 void myFunction() {
 cout << "I just got executed!";
 }
Inline Function
 C++ provides inline functions to reduce the function call overhead.
 An inline function is a function that is expanded in line when it is called.
 When the inline function is called whole code of the inline function gets inserted or
substituted at the point of the inline function call.
 This substitution is performed by the C++ compiler at compile time.
 An inline function may increase efficiency if it is small.
 Syntax:
 inline return-type function-name(parameters)
 {
 // function code
 }
OOPS USING C++(UNIT 2)
Inline functions Advantages:
 Function call overhead doesn’t occur.
 It also saves the overhead of push/pop variables on the stack when a
function is called.
 It also saves the overhead of a return call from a function.
 When you inline a function, you may enable the compiler to perform
context-specific optimization on the body of the function. Such
optimizations are not possible for normal function calls. Other
optimizations can be obtained by considering the flows of the calling
context and the called context.
 An inline function may be useful (if it is small) for embedded systems
because inline can yield less code than the function called preamble and
return.
Example
 #include <iostream>
 using namespace std;
 inline int cube(int s) { return s * s * s; }
 int main()
 {
 cout << "The cube of 3 is: " << cube(3) << "n";
 return 0;
 }
Function overloading
 With function overloading, multiple functions can have the same name with
different parameters.
 Example
 int myFunction(int x)
float myFunction(float x)
double myFunction(double x, double y)
 #include <iostream>
 using namespace std;
 int plusFuncInt(int x, int y) {
 return x + y;
 }
Cont…..
 double plusFuncDouble(double x, double y) {
 return x + y;
 }
 int main() {
 int myNum1 = plusFuncInt(8, 5);
 double myNum2 = plusFuncDouble(4.3, 6.26);
 cout << "Int: " << myNum1 << "n";
 cout << "Double: " << myNum2;
 return 0;
 }
Structure
 Structures (also called structs) are a way to group several related variables
into one place. Each variable in the structure is known as a member of the
structure.
 Unlike an array, a structure can contain many different data types (int,
string, bool, etc.).
 Create a Structure
 struct { // Structure declaration
int myNum; // Member (int variable)
string myString; // Member (string variable)
} myStructure; // Structure variable
Access Structure Members
 #include <iostream>
 #include <string>
 using namespace std;
 int main() {
 struct {
 int myNum;
 string myString;
 } myStructure;
Cont….
 myStructure.myNum = 1;
 myStructure.myString = "Hello World!";
 cout << myStructure.myNum << "n";
 cout << myStructure.myString << "n";
 return 0;
 }
Advantages of Structure
 Structure stores more than one data type of the same object together.
 It is helpful when you want to store data of different or similar data types
such as name, address, phone, etc., of the same object.
 It makes it very easy to maintain the entire record as we represent
complete records using a single name.
 The structure allows passing a whole set of records to any function with
the help of a single parameter.
 An array of structures can also be created to store multiple data of similar
data types.
Disadvantages of Structure
 If the complexity of the project goes increases, it becomes hard to manage
all your data members.
 Making changes to one data structure in a program makes it necessary to
change at several other places. So it becomes difficult to track all changes.
 The structure requires more storage space as it allocates memory to all the
data members, and even it is slower.
 The structure takes more storage space as it gives memory to all the
different data members, whereas union takes only the memory size
required by the largest data size parameters, and the same memory is
shared with other data members.
C++ program to demonstrate the
making of structure
 #include <iostream>
 using namespace std;

 // Define structure
 struct GFG {
 int G1;
 char G2;
 float G3;
 };

Cont….
 // Driver Code
 int main()
 {
 // Declaring a Structure
 struct GFG Geek;
 Geek.G1 = 85;

 Geek.G2 = 'G';
 Geek.G3 = 989.45;
 cout << "The value is : "
Cont….
 << Geek.G1 << endl;
 cout << "The value is : "
 << Geek.G2 << endl;
 cout << "The value is : "
 << Geek.G3 << endl;

 return 0;
 }
Union
 In "c++," programming union is a user-defined data type that is used to
store the different data type's values.
 However, in the union, one member will occupy the memory at once.
 In other words, we can say that the size of the union is equal to the size of
its largest data member size.
 Union offers an effective way to use the same memory location several
times by each data member.
Cont….
 #include <iostream>
 using namespace std;

 // Defining a Union
 union GFG {
 int Geek1;
 char Geek2;
 float Geek3;
 };

Cont…..
 int main()
 {
 Union GFG G1;
 G1.Geek1=34;
 cout<<“The first value at”<<“the allocated memory “<<G1.Geek1<<endl;
 G1.Geek2 = 34;

Cont….
 cout << "The next value stored "
 << "after removing the "
 << "previous value : " << G1.Geek2 << endl;
 G1.Geek3 = 34.34;
 cout << "The Final value value "
 << "at the same allocated "
 << "memory space : " << G1.Geek3 << endl;
 return 0;
 }
Advantages of Union
 Union takes less memory space as compared to the structure.
 Only the largest size data member can be directly accessed while using a
union.
 It is used when you want to use less (same) memory for different data
members.
 It allocates memory size to all its data members to the size of its largest
data member.
Disadvantages of Union
 It allows access to only one data member at a time.
 Union allocates one single common memory space to all its data
members, which are shared between all of them.
 Not all the union data members are initialized, and they are used by
interchanging values at a time.
Enumeration
 Enumeration (Enumerated type) is a user-defined data type that can be
assigned some limited values.
 These values are defined by the programmer at the time of declaring the
enumerated type.
 If we assign a float value to a character value, then the compiler generates
an error.
 In the same way, if we try to assign any other value to the enumerated data
types, the compiler generates an error.
 Enumerator types of values are also known as enumerators. It is also
assigned by zero the same as the array. It can also be used with switch
statements.
Cont….
 Syntax:
 enum enumerated-type-name
 {
 value1, value2, value3…..valueN
 };
 An enumeration is a user-defined data type that consists of integral constants.
To define an enumeration, keyword enum is used.
 enum season { spring, summer, autumn, winter };
 Here, the name of the enumeration is season.
 And, spring, summer and winter are values of type season.
Example
 #include <iostream>
 using namespace std;
 enum week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,
Saturday };
 int main()
 {
 week today;

Cont….
 today = Wednesday;
 cout << "Day " << today+1;
 return 0;
 }
 Output
 Day 4
Thank you 
Ad

More Related Content

Similar to OOPS USING C++(UNIT 2) (20)

Switch case and looping jam
Switch case and looping jamSwitch case and looping jam
Switch case and looping jam
JamaicaAubreyUnite
 
My programming final proj. (1)
My programming final proj. (1)My programming final proj. (1)
My programming final proj. (1)
aeden_brines
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
aprilyyy
 
control statements
control statementscontrol statements
control statements
Azeem Sultan
 
Inline function
Inline functionInline function
Inline function
Tech_MX
 
C++ Functions
C++ FunctionsC++ Functions
C++ Functions
Jari Abbas
 
My final requirement
My final requirementMy final requirement
My final requirement
katrinaguevarra29
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
rohassanie
 
OpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ Programming
Open Gurukul
 
C++ Topic 1.pdf from Yangon Technological University
C++ Topic 1.pdf  from Yangon Technological UniversityC++ Topic 1.pdf  from Yangon Technological University
C++ Topic 1.pdf from Yangon Technological University
ShweEainLinn2
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
ChaAstillas
 
complier design unit 5 for helping students
complier design unit 5 for helping studentscomplier design unit 5 for helping students
complier design unit 5 for helping students
aniketsugandhi1
 
Introduction& Overview-to-C++_programming.pptx
Introduction& Overview-to-C++_programming.pptxIntroduction& Overview-to-C++_programming.pptx
Introduction& Overview-to-C++_programming.pptx
divyadhanwani67
 
Introduction to C++ programming language
Introduction to C++ programming languageIntroduction to C++ programming language
Introduction to C++ programming language
divyadhanwani67
 
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
22 scheme  OOPs with C++ BCS306B_module2.pdfmodule2.pdf22 scheme  OOPs with C++ BCS306B_module2.pdfmodule2.pdf
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
sindhus795217
 
6. Functions in C ++ programming object oriented programming
6. Functions in C ++ programming object oriented programming6. Functions in C ++ programming object oriented programming
6. Functions in C ++ programming object oriented programming
Ahmad177077
 
14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
Docent Education
 
Fundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programmingFundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programming
LidetAdmassu
 
Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!
olracoatalub
 
Function Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
Function Overloading,Inline Function and Recursion in C++ By Faisal ShahzadFunction Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
Function Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
Faisal Shehzad
 
My programming final proj. (1)
My programming final proj. (1)My programming final proj. (1)
My programming final proj. (1)
aeden_brines
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
aprilyyy
 
control statements
control statementscontrol statements
control statements
Azeem Sultan
 
Inline function
Inline functionInline function
Inline function
Tech_MX
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
rohassanie
 
OpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ Programming
Open Gurukul
 
C++ Topic 1.pdf from Yangon Technological University
C++ Topic 1.pdf  from Yangon Technological UniversityC++ Topic 1.pdf  from Yangon Technological University
C++ Topic 1.pdf from Yangon Technological University
ShweEainLinn2
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
ChaAstillas
 
complier design unit 5 for helping students
complier design unit 5 for helping studentscomplier design unit 5 for helping students
complier design unit 5 for helping students
aniketsugandhi1
 
Introduction& Overview-to-C++_programming.pptx
Introduction& Overview-to-C++_programming.pptxIntroduction& Overview-to-C++_programming.pptx
Introduction& Overview-to-C++_programming.pptx
divyadhanwani67
 
Introduction to C++ programming language
Introduction to C++ programming languageIntroduction to C++ programming language
Introduction to C++ programming language
divyadhanwani67
 
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
22 scheme  OOPs with C++ BCS306B_module2.pdfmodule2.pdf22 scheme  OOPs with C++ BCS306B_module2.pdfmodule2.pdf
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
sindhus795217
 
6. Functions in C ++ programming object oriented programming
6. Functions in C ++ programming object oriented programming6. Functions in C ++ programming object oriented programming
6. Functions in C ++ programming object oriented programming
Ahmad177077
 
Fundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programmingFundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programming
LidetAdmassu
 
Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!
olracoatalub
 
Function Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
Function Overloading,Inline Function and Recursion in C++ By Faisal ShahzadFunction Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
Function Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
Faisal Shehzad
 

More from Dr. SURBHI SAROHA (20)

Deep learning(UNIT 3) BY Ms SURBHI SAROHA
Deep learning(UNIT 3) BY Ms SURBHI SAROHADeep learning(UNIT 3) BY Ms SURBHI SAROHA
Deep learning(UNIT 3) BY Ms SURBHI SAROHA
Dr. SURBHI SAROHA
 
MOBILE COMPUTING UNIT 2 by surbhi saroha
MOBILE COMPUTING UNIT 2 by surbhi sarohaMOBILE COMPUTING UNIT 2 by surbhi saroha
MOBILE COMPUTING UNIT 2 by surbhi saroha
Dr. SURBHI SAROHA
 
Mobile Computing UNIT 1 by surbhi saroha
Mobile Computing UNIT 1 by surbhi sarohaMobile Computing UNIT 1 by surbhi saroha
Mobile Computing UNIT 1 by surbhi saroha
Dr. SURBHI SAROHA
 
DEEP LEARNING (UNIT 2 ) by surbhi saroha
DEEP LEARNING (UNIT 2 ) by surbhi sarohaDEEP LEARNING (UNIT 2 ) by surbhi saroha
DEEP LEARNING (UNIT 2 ) by surbhi saroha
Dr. SURBHI SAROHA
 
Introduction to Deep Leaning(UNIT 1).pptx
Introduction to Deep Leaning(UNIT 1).pptxIntroduction to Deep Leaning(UNIT 1).pptx
Introduction to Deep Leaning(UNIT 1).pptx
Dr. SURBHI SAROHA
 
Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2
Dr. SURBHI SAROHA
 
Management Information System(Unit 2).pptx
Management Information System(Unit 2).pptxManagement Information System(Unit 2).pptx
Management Information System(Unit 2).pptx
Dr. SURBHI SAROHA
 
Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)
Dr. SURBHI SAROHA
 
Management Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptxManagement Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptx
Dr. SURBHI SAROHA
 
Introduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptxIntroduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptx
Dr. SURBHI SAROHA
 
JAVA (UNIT 5)
JAVA (UNIT 5)JAVA (UNIT 5)
JAVA (UNIT 5)
Dr. SURBHI SAROHA
 
DBMS (UNIT 5)
DBMS (UNIT 5)DBMS (UNIT 5)
DBMS (UNIT 5)
Dr. SURBHI SAROHA
 
DBMS UNIT 4
DBMS UNIT 4DBMS UNIT 4
DBMS UNIT 4
Dr. SURBHI SAROHA
 
JAVA(UNIT 4)
JAVA(UNIT 4)JAVA(UNIT 4)
JAVA(UNIT 4)
Dr. SURBHI SAROHA
 
OOPs & C++(UNIT 5)
OOPs & C++(UNIT 5)OOPs & C++(UNIT 5)
OOPs & C++(UNIT 5)
Dr. SURBHI SAROHA
 
OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)
Dr. SURBHI SAROHA
 
DBMS UNIT 3
DBMS UNIT 3DBMS UNIT 3
DBMS UNIT 3
Dr. SURBHI SAROHA
 
JAVA (UNIT 3)
JAVA (UNIT 3)JAVA (UNIT 3)
JAVA (UNIT 3)
Dr. SURBHI SAROHA
 
Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)
Dr. SURBHI SAROHA
 
DBMS (UNIT 2)
DBMS (UNIT 2)DBMS (UNIT 2)
DBMS (UNIT 2)
Dr. SURBHI SAROHA
 
Deep learning(UNIT 3) BY Ms SURBHI SAROHA
Deep learning(UNIT 3) BY Ms SURBHI SAROHADeep learning(UNIT 3) BY Ms SURBHI SAROHA
Deep learning(UNIT 3) BY Ms SURBHI SAROHA
Dr. SURBHI SAROHA
 
MOBILE COMPUTING UNIT 2 by surbhi saroha
MOBILE COMPUTING UNIT 2 by surbhi sarohaMOBILE COMPUTING UNIT 2 by surbhi saroha
MOBILE COMPUTING UNIT 2 by surbhi saroha
Dr. SURBHI SAROHA
 
Mobile Computing UNIT 1 by surbhi saroha
Mobile Computing UNIT 1 by surbhi sarohaMobile Computing UNIT 1 by surbhi saroha
Mobile Computing UNIT 1 by surbhi saroha
Dr. SURBHI SAROHA
 
DEEP LEARNING (UNIT 2 ) by surbhi saroha
DEEP LEARNING (UNIT 2 ) by surbhi sarohaDEEP LEARNING (UNIT 2 ) by surbhi saroha
DEEP LEARNING (UNIT 2 ) by surbhi saroha
Dr. SURBHI SAROHA
 
Introduction to Deep Leaning(UNIT 1).pptx
Introduction to Deep Leaning(UNIT 1).pptxIntroduction to Deep Leaning(UNIT 1).pptx
Introduction to Deep Leaning(UNIT 1).pptx
Dr. SURBHI SAROHA
 
Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2
Dr. SURBHI SAROHA
 
Management Information System(Unit 2).pptx
Management Information System(Unit 2).pptxManagement Information System(Unit 2).pptx
Management Information System(Unit 2).pptx
Dr. SURBHI SAROHA
 
Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)
Dr. SURBHI SAROHA
 
Management Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptxManagement Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptx
Dr. SURBHI SAROHA
 
Introduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptxIntroduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptx
Dr. SURBHI SAROHA
 
Ad

Recently uploaded (20)

Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
SPRING FESTIVITIES - UK AND USA -
SPRING FESTIVITIES - UK AND USA            -SPRING FESTIVITIES - UK AND USA            -
SPRING FESTIVITIES - UK AND USA -
Colégio Santa Teresinha
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
Ad

OOPS USING C++(UNIT 2)

  • 2. Syllabus  Program Control Statements  Sequential Constructs  Decision Making Construct  Iteration/Loop Construct  Arrays  Functions(User defined Functions, Inline Function, Function overloading)  User Defined Data Types(Structure, Union and Enumeration)
  • 3. Program Control Statements  The conditional statements (also known as decision control structures) such as if, if else, switch, etc. are used for decision-making purposes in C/C++ programs.  They are also known as Decision-Making Statements and are used to evaluate one or more conditions and make the decision whether to execute a set of statements or not.  These decision-making statements in programming languages decide the direction of the flow of program execution.
  • 4. Following are the decision-making statements available in C or C++:  if Statement  if-else Statement  Nested if Statement  if-else-if Ladder  switch Statement  Conditional Operator  Jump Statements:  break  continue  goto  return
  • 5. If-Else  If-Else statements allow the program to execute different blocks of code depending on conditionals. All If statements have the following form:  if ( condition ) {  //body  }
  • 6. For Loop  A for loop allows for a block of code to be executed until a conditional becomes false. For loops are usually used when a block of code needs to executed a fixed number of times.  Each loop consists of 3 parts, an initialization step, the conditional, and an iteration step.  The initialization is run before entering the loop, the condition is checked at the beginning of each run through the loop ( including the first run ), and the iteration step executes at the end of each pass through the loop, but before the condition is rechecked.  for( initialization ; conditional ; iteration ) {  // loop body  }
  • 7. While Loop  A while loop is a simple loop that will run the same code over and over as long as a given conditional is true. The condition is checked at the beginning of each run through the loop ( including the first one ). If the conditional is false for the beginning, the while loop will be skipped all together.  while ( conditional ) {  // loop body  }
  • 8. Do-while Loop  A do-while loop acts just like a while loop, except the condition is checked at the end of each pass through the loop body. This means a do-while loop will execute at least once.  do {  // loop body  } while ( condition );
  • 9. Break  Break is a useful keyword that allows the program to exit a loop or switch statement before the expected end of a that code block. This is useful in error checking or if the outcome of a loop is not certain. For example, the following code will break out of the for loop if a user asks to leave.  string inputs[10];  string input;  for (int i = 0; i < 10; i++ ) {  cin >> input;
  • 10. Cont…  if ( input == "quit" ) {  break;  }  inputs[i] = input;  }
  • 11. Arrays  Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.  To declare an array, define the variable type, specify the name of the array followed by square brackets and specify the number of elements it should store.  #include <iostream>  #include <string>  using namespace std;  int main() {  string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};  cout << cars[0];  return 0;  }
  • 12. Loop Through an Array  #include <iostream>  #include <string>  using namespace std;  int main() {  string cars[5] = {"Volvo", "BMW", "Ford", "Mazda", "Tesla"};  for (int i = 0; i < 5; i++) {  cout << cars[i] << "n";  }  return 0;  }
  • 13. User defined Functions  #include <iostream>  using namespace std;  // Function declaration  void myFunction();  // The main method  int main() {
  • 14. Cont…..  myFunction(); // call the function  return 0;  }  // Function definition  void myFunction() {  cout << "I just got executed!";  }
  • 15. Inline Function  C++ provides inline functions to reduce the function call overhead.  An inline function is a function that is expanded in line when it is called.  When the inline function is called whole code of the inline function gets inserted or substituted at the point of the inline function call.  This substitution is performed by the C++ compiler at compile time.  An inline function may increase efficiency if it is small.  Syntax:  inline return-type function-name(parameters)  {  // function code  }
  • 17. Inline functions Advantages:  Function call overhead doesn’t occur.  It also saves the overhead of push/pop variables on the stack when a function is called.  It also saves the overhead of a return call from a function.  When you inline a function, you may enable the compiler to perform context-specific optimization on the body of the function. Such optimizations are not possible for normal function calls. Other optimizations can be obtained by considering the flows of the calling context and the called context.  An inline function may be useful (if it is small) for embedded systems because inline can yield less code than the function called preamble and return.
  • 18. Example  #include <iostream>  using namespace std;  inline int cube(int s) { return s * s * s; }  int main()  {  cout << "The cube of 3 is: " << cube(3) << "n";  return 0;  }
  • 19. Function overloading  With function overloading, multiple functions can have the same name with different parameters.  Example  int myFunction(int x) float myFunction(float x) double myFunction(double x, double y)  #include <iostream>  using namespace std;  int plusFuncInt(int x, int y) {  return x + y;  }
  • 20. Cont…..  double plusFuncDouble(double x, double y) {  return x + y;  }  int main() {  int myNum1 = plusFuncInt(8, 5);  double myNum2 = plusFuncDouble(4.3, 6.26);  cout << "Int: " << myNum1 << "n";  cout << "Double: " << myNum2;  return 0;  }
  • 21. Structure  Structures (also called structs) are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure.  Unlike an array, a structure can contain many different data types (int, string, bool, etc.).  Create a Structure  struct { // Structure declaration int myNum; // Member (int variable) string myString; // Member (string variable) } myStructure; // Structure variable
  • 22. Access Structure Members  #include <iostream>  #include <string>  using namespace std;  int main() {  struct {  int myNum;  string myString;  } myStructure;
  • 23. Cont….  myStructure.myNum = 1;  myStructure.myString = "Hello World!";  cout << myStructure.myNum << "n";  cout << myStructure.myString << "n";  return 0;  }
  • 24. Advantages of Structure  Structure stores more than one data type of the same object together.  It is helpful when you want to store data of different or similar data types such as name, address, phone, etc., of the same object.  It makes it very easy to maintain the entire record as we represent complete records using a single name.  The structure allows passing a whole set of records to any function with the help of a single parameter.  An array of structures can also be created to store multiple data of similar data types.
  • 25. Disadvantages of Structure  If the complexity of the project goes increases, it becomes hard to manage all your data members.  Making changes to one data structure in a program makes it necessary to change at several other places. So it becomes difficult to track all changes.  The structure requires more storage space as it allocates memory to all the data members, and even it is slower.  The structure takes more storage space as it gives memory to all the different data members, whereas union takes only the memory size required by the largest data size parameters, and the same memory is shared with other data members.
  • 26. C++ program to demonstrate the making of structure  #include <iostream>  using namespace std;   // Define structure  struct GFG {  int G1;  char G2;  float G3;  }; 
  • 27. Cont….  // Driver Code  int main()  {  // Declaring a Structure  struct GFG Geek;  Geek.G1 = 85;   Geek.G2 = 'G';  Geek.G3 = 989.45;  cout << "The value is : "
  • 28. Cont….  << Geek.G1 << endl;  cout << "The value is : "  << Geek.G2 << endl;  cout << "The value is : "  << Geek.G3 << endl;   return 0;  }
  • 29. Union  In "c++," programming union is a user-defined data type that is used to store the different data type's values.  However, in the union, one member will occupy the memory at once.  In other words, we can say that the size of the union is equal to the size of its largest data member size.  Union offers an effective way to use the same memory location several times by each data member.
  • 30. Cont….  #include <iostream>  using namespace std;   // Defining a Union  union GFG {  int Geek1;  char Geek2;  float Geek3;  }; 
  • 31. Cont…..  int main()  {  Union GFG G1;  G1.Geek1=34;  cout<<“The first value at”<<“the allocated memory “<<G1.Geek1<<endl;  G1.Geek2 = 34; 
  • 32. Cont….  cout << "The next value stored "  << "after removing the "  << "previous value : " << G1.Geek2 << endl;  G1.Geek3 = 34.34;  cout << "The Final value value "  << "at the same allocated "  << "memory space : " << G1.Geek3 << endl;  return 0;  }
  • 33. Advantages of Union  Union takes less memory space as compared to the structure.  Only the largest size data member can be directly accessed while using a union.  It is used when you want to use less (same) memory for different data members.  It allocates memory size to all its data members to the size of its largest data member.
  • 34. Disadvantages of Union  It allows access to only one data member at a time.  Union allocates one single common memory space to all its data members, which are shared between all of them.  Not all the union data members are initialized, and they are used by interchanging values at a time.
  • 35. Enumeration  Enumeration (Enumerated type) is a user-defined data type that can be assigned some limited values.  These values are defined by the programmer at the time of declaring the enumerated type.  If we assign a float value to a character value, then the compiler generates an error.  In the same way, if we try to assign any other value to the enumerated data types, the compiler generates an error.  Enumerator types of values are also known as enumerators. It is also assigned by zero the same as the array. It can also be used with switch statements.
  • 36. Cont….  Syntax:  enum enumerated-type-name  {  value1, value2, value3…..valueN  };  An enumeration is a user-defined data type that consists of integral constants. To define an enumeration, keyword enum is used.  enum season { spring, summer, autumn, winter };  Here, the name of the enumeration is season.  And, spring, summer and winter are values of type season.
  • 37. Example  #include <iostream>  using namespace std;  enum week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };  int main()  {  week today; 
  • 38. Cont….  today = Wednesday;  cout << "Day " << today+1;  return 0;  }  Output  Day 4