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