SlideShare a Scribd company logo
1 of 39
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
 }
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 

More Related Content

Similar to OOPS USING C++(UNIT 2)

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 loopingaprilyyy
 
control statements
control statementscontrol statements
control statementsAzeem Sultan
 
Inline function
Inline functionInline function
Inline functionTech_MX
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2rohassanie
 
OpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpen Gurukul
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and loopingChaAstillas
 
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 ShahzadFaisal Shehzad
 
Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfCh-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfesuEthopi
 
Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++ppd1961
 
chapter-8-function-overloading.pdf
chapter-8-function-overloading.pdfchapter-8-function-overloading.pdf
chapter-8-function-overloading.pdfstudy material
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 FunctionDeepak Singh
 

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

My programming final proj. (1)
My programming final proj. (1)My programming final proj. (1)
My programming final proj. (1)
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
 
control statements
control statementscontrol statements
control statements
 
Inline function
Inline functionInline function
Inline function
 
C++ Functions
C++ FunctionsC++ Functions
C++ Functions
 
My final requirement
My final requirementMy final requirement
My final requirement
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
OpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ Programming
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
 
14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
 
Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!
 
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
 
Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfCh-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdf
 
Function in C++
Function in C++Function in C++
Function in C++
 
Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++
 
chapter-8-function-overloading.pdf
chapter-8-function-overloading.pdfchapter-8-function-overloading.pdf
chapter-8-function-overloading.pdf
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
 

More from 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 2SURBHI SAROHA
 
Management Information System(Unit 2).pptx
Management Information System(Unit 2).pptxManagement Information System(Unit 2).pptx
Management Information System(Unit 2).pptxSURBHI 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)SURBHI SAROHA
 
Management Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptxManagement Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptxSURBHI 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).pptxSURBHI SAROHA
 
Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)SURBHI SAROHA
 
Database Management System(UNIT 1)
Database Management System(UNIT 1)Database Management System(UNIT 1)
Database Management System(UNIT 1)SURBHI SAROHA
 
Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1SURBHI SAROHA
 
Database Management System(UNIT 1)
Database Management System(UNIT 1)Database Management System(UNIT 1)
Database Management System(UNIT 1)SURBHI SAROHA
 

More from SURBHI SAROHA (20)

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
 
Management Information System(Unit 2).pptx
Management Information System(Unit 2).pptxManagement Information System(Unit 2).pptx
Management Information System(Unit 2).pptx
 
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)
 
Management Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptxManagement Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptx
 
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
 
JAVA (UNIT 5)
JAVA (UNIT 5)JAVA (UNIT 5)
JAVA (UNIT 5)
 
DBMS (UNIT 5)
DBMS (UNIT 5)DBMS (UNIT 5)
DBMS (UNIT 5)
 
DBMS UNIT 4
DBMS UNIT 4DBMS UNIT 4
DBMS UNIT 4
 
JAVA(UNIT 4)
JAVA(UNIT 4)JAVA(UNIT 4)
JAVA(UNIT 4)
 
OOPs & C++(UNIT 5)
OOPs & C++(UNIT 5)OOPs & C++(UNIT 5)
OOPs & C++(UNIT 5)
 
OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)
 
DBMS UNIT 3
DBMS UNIT 3DBMS UNIT 3
DBMS UNIT 3
 
JAVA (UNIT 3)
JAVA (UNIT 3)JAVA (UNIT 3)
JAVA (UNIT 3)
 
Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)
 
DBMS (UNIT 2)
DBMS (UNIT 2)DBMS (UNIT 2)
DBMS (UNIT 2)
 
JAVA UNIT 2
JAVA UNIT 2JAVA UNIT 2
JAVA UNIT 2
 
Database Management System(UNIT 1)
Database Management System(UNIT 1)Database Management System(UNIT 1)
Database Management System(UNIT 1)
 
Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1
 
Database Management System(UNIT 1)
Database Management System(UNIT 1)Database Management System(UNIT 1)
Database Management System(UNIT 1)
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 

Recently uploaded

internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 

Recently uploaded (20)

internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 

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  }
  • 16.
  • 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