SlideShare una empresa de Scribd logo
1 de 12
Computer Programming -II
Instructor:
Dr. Satyavir Singh
Assistant Professor
C++ Structures (struct)
• C++ Structures
• 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
• To create a structure, use the struct keyword and
declare each of its members inside curly braces.
• struct name { // 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;
myStructure.myNum = 1;
myStructure.myString = "Hello World!";
cout << myStructure.myNum << "n";
cout << myStructure.myString << "n";
return 0;
}
One Structure in Multiple Variables
• struct {
int myNum;
string myString;
} myStruct1, myStruct2, myStruct3; // Multiple
structure variables separated with commas
#include <iostream>
#include <string>
using namespace std;
int main() {
struct book {
string name;
float price;
int pages;
} b1, b2;
b1.name="C";
b1.price=130.00;
b1.pages=550;
b2.name="C++";
b2.price=140.00;
b2.pages=650;
cout<<"book b1 detailsn"<<b1.name<<"t"<<b1.price<<"t"<<b1.pages<<"n";
cout<<"book b2 detailsn"<<b2.name<<"t"<<b2.price<<"t"<<b2.pages<<"n";
return 0;
}
Struct book
#include <iostream>
#include <string>
using namespace std;
int main() {
struct myCar{
string brand;
string model;
int year;
} myCar1, myCar2; // We can add variables by separating them with a comma here
// Put data into the first structure
myCar1.brand = "BMW";
myCar1.model = "X5";
myCar1.year = 1999;
// Put data into the second structure
myCar2.brand = "Ford";
myCar2.model = “Escort";
myCar2.year = 1969;
// Print the structure members
cout << myCar1.brand << " " << myCar1.model << " " << myCar1.year << "n";
cout << myCar2.brand << " " << myCar2.model << " " << myCar2.year << "n";
return 0;
}
Struct myCar
Array of Structures
#include <iostream>
#include <string>
using namespace std;
int main() {
struct book {
string name;
float price;
int pages;
} ;
book b[100];
for(int i=0;i<100;i++)
{
cout<<“Enter the books name, price and pages:n”;
cin>>b[i].name>>b[i].price>>b[i].pages;
}
cout<<“Book details are:”;
for(int i=0;i<100;i++)
{
cout<<b[i].name<<“n”<<b[i].price<<“n”<<b[i].pages<<“n”;
}
return 0;
}
C++ References
Creating References
• A reference variable is a "reference" to an existing variable, and it is created with
the & operator:
string food = "Pizza"; // food variable
string &meal = food;
Now, we can use either the variable name food or the reference name meal to refer to
the food variable:
#include <iostream>
#include <string>
using namespace std;
int main() {
string food = "Pizza";
string &meal = food;
cout << food << "n";
cout << meal << "n";
return 0;
}
Output:
Pizza
Pizza
C++ References
• A reference variable provide an alias (alternative
name) for a previously defined variable.
Example
float total=100;
float &sum=total;
cout<<total; Output: 100
cout<<sum ; Output: 100
total=total+10;
cout<<total; Output: 110
cout<<sum; Output: 110
#include<iostream>
using namespace std;
void swap(int ,int );
int main()
{
int a,b;
cout<<"Enter the two numbers to swap";
cin>>a>>b;
cout<<"nAfter swapping of two numbers:";
swap(a,b);
cout<<a<<"t"<<b;
return 0;
}
void swap(int a, int b)
{
int temp;
temp=a;
a=b;
b=temp;
}
Swapping of two numbers using call by value
Swapping of two numbers using call by reference
#include <iostream>
using namespace std;
int main() {
int a=5, b=10;
cout << “Before swapping:t” <<a<<“t”<<b;
swap(a,b);
cout << “After swapping:t” <<a<<“t”<<b;
return 0;
}
void swap(int &num1, int &num2)
{ int temp;
temp=num1;
num1=num2;
num2=temp;
}
#include<iostream>
using namespace std;
void swap(int ,int );
int main()
{
int a,b;
cout<<"Enter the Two Numbers to Swap: ";
cin>>a>>b;
swap(a,b);
return 0;
}
void swap(int a, int b)
{
int temp;
temp=a;
a=b;
b=temp;
cout<<"nAfter Swapping of Two Numbers:";
cout<<a<<"t"<<b;
}
Swapping of two numbers using call by value

Más contenido relacionado

Similar a Computer Programming -II (Lec. 10).pptx

Objective-C Crash Course for Web Developers
Objective-C Crash Course for Web DevelopersObjective-C Crash Course for Web Developers
Objective-C Crash Course for Web DevelopersJoris Verbogt
 
C++ Secure Programming
C++ Secure ProgrammingC++ Secure Programming
C++ Secure ProgrammingMarian Marinov
 
Module 5-Structure and Union
Module 5-Structure and UnionModule 5-Structure and Union
Module 5-Structure and Unionnikshaikh786
 
Oop lec 3(structures)
Oop lec 3(structures)Oop lec 3(structures)
Oop lec 3(structures)Asfand Hassan
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCEVenugopalavarma Raja
 
Webinar: Build an Application Series - Session 2 - Getting Started
Webinar: Build an Application Series - Session 2 - Getting StartedWebinar: Build an Application Series - Session 2 - Getting Started
Webinar: Build an Application Series - Session 2 - Getting StartedMongoDB
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Abu Saleh
 
OOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptxOOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptxSirRafiLectures
 
slideset 7 structure and union (1).pdf
slideset 7 structure and union (1).pdfslideset 7 structure and union (1).pdf
slideset 7 structure and union (1).pdfHimanshuKansal22
 
How to create an Angular builder
How to create an Angular builderHow to create an Angular builder
How to create an Angular builderMaurizio Vitale
 
So I already have most of the code and now I have to1. create an .pdf
So I already have most of the code and now I have to1. create an .pdfSo I already have most of the code and now I have to1. create an .pdf
So I already have most of the code and now I have to1. create an .pdfarjuncollection
 
Beginning direct3d gameprogrammingcpp02_20160324_jintaeks
Beginning direct3d gameprogrammingcpp02_20160324_jintaeksBeginning direct3d gameprogrammingcpp02_20160324_jintaeks
Beginning direct3d gameprogrammingcpp02_20160324_jintaeksJinTaek Seo
 
前端MVC之BackboneJS
前端MVC之BackboneJS前端MVC之BackboneJS
前端MVC之BackboneJSZhang Xiaoxue
 

Similar a Computer Programming -II (Lec. 10).pptx (20)

Structures_Final_KLE (2).pptx
Structures_Final_KLE (2).pptxStructures_Final_KLE (2).pptx
Structures_Final_KLE (2).pptx
 
Ch7 structures
Ch7 structuresCh7 structures
Ch7 structures
 
U5 SPC.pptx
U5 SPC.pptxU5 SPC.pptx
U5 SPC.pptx
 
U5 SPC.pptx
U5 SPC.pptxU5 SPC.pptx
U5 SPC.pptx
 
Structures
StructuresStructures
Structures
 
Objective-C Crash Course for Web Developers
Objective-C Crash Course for Web DevelopersObjective-C Crash Course for Web Developers
Objective-C Crash Course for Web Developers
 
structure .pptx
structure .pptxstructure .pptx
structure .pptx
 
C++ Secure Programming
C++ Secure ProgrammingC++ Secure Programming
C++ Secure Programming
 
Module 5-Structure and Union
Module 5-Structure and UnionModule 5-Structure and Union
Module 5-Structure and Union
 
Oop lec 3(structures)
Oop lec 3(structures)Oop lec 3(structures)
Oop lec 3(structures)
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 
Webinar: Build an Application Series - Session 2 - Getting Started
Webinar: Build an Application Series - Session 2 - Getting StartedWebinar: Build an Application Series - Session 2 - Getting Started
Webinar: Build an Application Series - Session 2 - Getting Started
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
 
OOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptxOOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptx
 
slideset 7 structure and union (1).pdf
slideset 7 structure and union (1).pdfslideset 7 structure and union (1).pdf
slideset 7 structure and union (1).pdf
 
How to create an Angular builder
How to create an Angular builderHow to create an Angular builder
How to create an Angular builder
 
Unit4 C
Unit4 C Unit4 C
Unit4 C
 
So I already have most of the code and now I have to1. create an .pdf
So I already have most of the code and now I have to1. create an .pdfSo I already have most of the code and now I have to1. create an .pdf
So I already have most of the code and now I have to1. create an .pdf
 
Beginning direct3d gameprogrammingcpp02_20160324_jintaeks
Beginning direct3d gameprogrammingcpp02_20160324_jintaeksBeginning direct3d gameprogrammingcpp02_20160324_jintaeks
Beginning direct3d gameprogrammingcpp02_20160324_jintaeks
 
前端MVC之BackboneJS
前端MVC之BackboneJS前端MVC之BackboneJS
前端MVC之BackboneJS
 

Último

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
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
 
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
 
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
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
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
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
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
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxAmita Gupta
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
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
 
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
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 

Último (20)

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
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
 
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
 
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
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
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
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
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
 
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
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
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)
 
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
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 

Computer Programming -II (Lec. 10).pptx

  • 1. Computer Programming -II Instructor: Dr. Satyavir Singh Assistant Professor
  • 2. C++ Structures (struct) • C++ Structures • 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 • To create a structure, use the struct keyword and declare each of its members inside curly braces. • struct name { // Structure declaration int myNum; // Member (int variable) string myString; // Member (string variable) } myStructure; // Structure variable
  • 3. Access Structure Members #include <iostream> #include <string> using namespace std; int main() { struct { int myNum; string myString; } myStructure; myStructure.myNum = 1; myStructure.myString = "Hello World!"; cout << myStructure.myNum << "n"; cout << myStructure.myString << "n"; return 0; }
  • 4. One Structure in Multiple Variables • struct { int myNum; string myString; } myStruct1, myStruct2, myStruct3; // Multiple structure variables separated with commas
  • 5. #include <iostream> #include <string> using namespace std; int main() { struct book { string name; float price; int pages; } b1, b2; b1.name="C"; b1.price=130.00; b1.pages=550; b2.name="C++"; b2.price=140.00; b2.pages=650; cout<<"book b1 detailsn"<<b1.name<<"t"<<b1.price<<"t"<<b1.pages<<"n"; cout<<"book b2 detailsn"<<b2.name<<"t"<<b2.price<<"t"<<b2.pages<<"n"; return 0; } Struct book
  • 6. #include <iostream> #include <string> using namespace std; int main() { struct myCar{ string brand; string model; int year; } myCar1, myCar2; // We can add variables by separating them with a comma here // Put data into the first structure myCar1.brand = "BMW"; myCar1.model = "X5"; myCar1.year = 1999; // Put data into the second structure myCar2.brand = "Ford"; myCar2.model = “Escort"; myCar2.year = 1969; // Print the structure members cout << myCar1.brand << " " << myCar1.model << " " << myCar1.year << "n"; cout << myCar2.brand << " " << myCar2.model << " " << myCar2.year << "n"; return 0; } Struct myCar
  • 7. Array of Structures #include <iostream> #include <string> using namespace std; int main() { struct book { string name; float price; int pages; } ; book b[100]; for(int i=0;i<100;i++) { cout<<“Enter the books name, price and pages:n”; cin>>b[i].name>>b[i].price>>b[i].pages; } cout<<“Book details are:”; for(int i=0;i<100;i++) { cout<<b[i].name<<“n”<<b[i].price<<“n”<<b[i].pages<<“n”; } return 0; }
  • 8. C++ References Creating References • A reference variable is a "reference" to an existing variable, and it is created with the & operator: string food = "Pizza"; // food variable string &meal = food; Now, we can use either the variable name food or the reference name meal to refer to the food variable: #include <iostream> #include <string> using namespace std; int main() { string food = "Pizza"; string &meal = food; cout << food << "n"; cout << meal << "n"; return 0; } Output: Pizza Pizza
  • 9. C++ References • A reference variable provide an alias (alternative name) for a previously defined variable. Example float total=100; float &sum=total; cout<<total; Output: 100 cout<<sum ; Output: 100 total=total+10; cout<<total; Output: 110 cout<<sum; Output: 110
  • 10. #include<iostream> using namespace std; void swap(int ,int ); int main() { int a,b; cout<<"Enter the two numbers to swap"; cin>>a>>b; cout<<"nAfter swapping of two numbers:"; swap(a,b); cout<<a<<"t"<<b; return 0; } void swap(int a, int b) { int temp; temp=a; a=b; b=temp; } Swapping of two numbers using call by value
  • 11. Swapping of two numbers using call by reference #include <iostream> using namespace std; int main() { int a=5, b=10; cout << “Before swapping:t” <<a<<“t”<<b; swap(a,b); cout << “After swapping:t” <<a<<“t”<<b; return 0; } void swap(int &num1, int &num2) { int temp; temp=num1; num1=num2; num2=temp; }
  • 12. #include<iostream> using namespace std; void swap(int ,int ); int main() { int a,b; cout<<"Enter the Two Numbers to Swap: "; cin>>a>>b; swap(a,b); return 0; } void swap(int a, int b) { int temp; temp=a; a=b; b=temp; cout<<"nAfter Swapping of Two Numbers:"; cout<<a<<"t"<<b; } Swapping of two numbers using call by value