SlideShare a Scribd company logo
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

More Related Content

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

Structures_Final_KLE (2).pptx
Structures_Final_KLE (2).pptxStructures_Final_KLE (2).pptx
Structures_Final_KLE (2).pptx
ShreevatsaAlawandi
 
U5 SPC.pptx
U5 SPC.pptxU5 SPC.pptx
U5 SPC.pptx
thenmozhip8
 
U5 SPC.pptx
U5 SPC.pptxU5 SPC.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 Developers
Joris Verbogt
 
structure .pptx
structure .pptxstructure .pptx
structure .pptx
MohamedFekry53
 
C++ Secure Programming
C++ Secure ProgrammingC++ Secure Programming
C++ Secure Programming
Marian Marinov
 
Module 5-Structure and Union
Module 5-Structure and UnionModule 5-Structure and Union
Module 5-Structure and Union
nikshaikh786
 
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 SCIENCE
Venugopalavarma 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 Started
MongoDB
 
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
Abu Saleh
 
OOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptxOOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptx
SirRafiLectures
 
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
HimanshuKansal22
 
How to create an Angular builder
How to create an Angular builderHow to create an Angular builder
How to create an Angular builder
Maurizio 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 .pdf
arjuncollection
 
Beginning direct3d gameprogrammingcpp02_20160324_jintaeks
Beginning direct3d gameprogrammingcpp02_20160324_jintaeksBeginning direct3d gameprogrammingcpp02_20160324_jintaeks
Beginning direct3d gameprogrammingcpp02_20160324_jintaeks
JinTaek Seo
 
前端MVC之BackboneJS
前端MVC之BackboneJS前端MVC之BackboneJS
前端MVC之BackboneJS
Zhang Xiaoxue
 

Similar to 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
 

Recently uploaded

The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
Kartik Tiwari
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
gb193092
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 

Recently uploaded (20)

The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 

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