Data Structures (struct)
In the world of programming, data structures play a pivotal role in how we organize and manipulate data efficiently. They act as the building blocks that enable us to create well-structured and organized programs. In this explanation, we will delve into one fundamental data structure in C++ known as struct.
What is a Data Structure?
Before we explore struct, let’s define what a data structure is. A data structure is a way of organizing and storing data in a computer’s memory to perform operations on that data more efficiently. It provides a blueprint for the storage and retrieval of information.
Introduction to struct in C++
struct in C++ is a user-defined data type that allows you to group together variables of different data types under a single name. Think of it as creating a custom data type tailored to the specific needs of your program. This makes your code more modular, organized, and easier to understand.
Syntax of struct:
The syntax for defining a struct is straightforward:
struct MyStruct
struct MyStruct
Example 1:
#include <iostream>
#include <string>
using namespace std;
struct car
{
string name;
string color;
int maxSpeed;
int model;
};
int main()
{
car x;
x.name = "Rolls-Royce";
x.color = "Red";
x.model = 2019;
x.maxSpeed = 270;
cout << x.name << endl;
return 0;
}
code defines a struct named car that represents information about a car, and it creates an instance of this struct called x to store details about a specific car.
Key points:
- Struct Definition (car):
The struct named car has four member variables: name, color, maxSpeed, and model. These variables store information about a car. - Instance Creation (x):
An instance of the car struct named x is created in the main function. This instance can store information about a specific car. - Assigning Values:
Values are assigned to the member variables of the x instance using the dot (.) notation. For example, x.name = “Rolls-Royce” assigns the name “Rolls-Royce” to the name member variable. - Printing Information:
The program prints the name of the car using cout << x.name << endl;. In this case, it will output “Rolls-Royce” to the console.
Output:
If you run this program, the output will be:
Rolls-Royce
Example 2:
Here is another way of initialization the values based on the previous example:
#include <iostream>
#include <string>
using namespace std;
struct car
{
string name;
string color;
int maxSpeed;
int model;
};
int main()
{
car x = {"BMW","Blue",250,2016};
cout << x.maxSpeed << endl;
return 0;
}
Output:
250
Example 3:
#include <iostream>
#include <string>
using namespace std;
struct car
{
string name;
string color;
int maxSpeed;
int model;
};
int main()
{
car x = {"BMW","Blue",250,2016};
car y = x;
cout << y.name << endl;
return 0;
}
Instance Creation and Initialization (x and y):
- An instance of the car struct named x is created with initial values.
- Another instance named y is created and initialized with the values of x. This is a memberwise copy, and each member of y gets the value of the corresponding member in x.
Example 4:
#include <iostream>
#include <string>
using namespace std;
struct car
{
string name;
string color;
int maxSpeed;
int model;
}y;
int main()
{
car x = {"BMW","Blue",250,2016};
car y = {"Mercedes","Red",300,2016};
if (x.maxSpeed > y.maxSpeed)
cout << "car x is faster than car y";
else
cout << "car y is faster than car x";
return 0;
}
- Global Instance Declaration (y):
An instance of the car struct named y is declared at the global scope. This means it can be accessed throughout the program. - Instance Creation and Initialization (x and Local y):
– A local instance of the car struct named x is created with initial values.
– Another local instance named y is created with different values. This y is local to the main function and shadows the global y within this scope. - Comparison and Output:
The program compares the maximum speeds of cars x and y and prints a message indicating which car is faster.
Output:
If you run this program, the output will depend on the values assigned to maxSpeed in x and y. For the provided values, it will output:
car y is faster than car x
Pass ‘struct’ into function:
Example 5:
#include <iostream>
#include <string>
using namespace std;
struct car
{
string name;
string color;
int maxSpeed;
int model;
};
void f(car f)
{
cout << "Name = " << f.name << endl;
cout << "Color = " << f.color << endl;
}
int main()
{
car v = {"No name","Red",160,2000};
f(v);
return 0;
}
Key points:
- Function Definition (f):
The function f takes a car as a parameter and prints information about the car, specifically the name and color. - Instance Creation and Initialization (v):
An instance of the car struct named v is created with initial values. - Function Call (f(v)):
The program calls the function f with the v instance as an argument.
Output:
If you run this program, the output will be:
Name = No name Color = Red
Example 6:
#include <iostream>
#include <string>
using namespace std;
struct car
{
string name;
string color;
int maxSpeed;
int model;
};
car read_return(car&s)
{
cout << "Enter car name:\n";
cin >> s.name;
cout << "Enter car color:\n";
cin >> s.color;
cout << "Enter car maximum speed:\n";
cin >> s.maxSpeed;
cout << "Enter car model:\n";
cin >> s.model;
return s;
}
int main()
{
car v;
read_return(v);
car h;
h = v;
cout << h.name << endl;
return 0;
}
Key points:
- Function Definition (read_return):
The function read_return takes a car reference as a parameter, reads input to populate its fields, and returns the modified car struct. - Function Call (read_return(v)):
The program calls the function read_return with the v instance as a reference, allowing the function to modify the values of v. - Assignment and Printing:
– The values of v are assigned to h.
– The program prints the name of h.
Output:
If you run this program and enter values when prompted, the output will depend on the input provided. For example:
Enter car name: BMW Enter car color: Blue Enter car maximum speed: 250 Enter car model: 2022 BMW
Example 7:
#include <iostream>
#include <string>
using namespace std;
struct car
{
string name;
string color;
int maxSpeed;
int model;
void fun(car n)
{
cout << n.name << endl;
}
};
int main()
{
car v = {"Kia"};
v.fun(v);
return 0;
}
Key points:
- Member Function Definition (fun):
The member function fun is defined inside the car struct.
It takes a car as a parameter and prints its name.
Output:
If you run this program, the output will be:
Kia
Example 8:
#include <iostream>
#include <string>
using namespace std;
struct car
{
string name;
string color;
int maxSpeed;
int model;
void fun(car n)
{
cout << n.name << endl;
}
};
int main()
{
car*h;
car b = {"Toyota","Red",170,2008};
h = &b;
cout << h->color << endl;
return 0;
}
Key points:
- Pointer Declaration and Initialization (h):
A pointer to a car named h is declared. - Instance Creation and Initialization (b):
An instance of the car struct named b is created and initialized with specific values. - Pointer Assignment (h = &b):
The address of b is assigned to the pointer h. - Pointer Dereferencing (h->color):
The program prints the color of the car pointed to by h using the arrow operator (->).
Output:
If you run this program, the output will be:
Red