Structures

User defined data type that groups of one or more variables, of potentially different data types, under a single identifier name, terminated with a semi-colon ;

 

Declared using the struct keyword like so:

 

struct identifier {

data type member-identifier ;
data type member-identifier ;

} ;

 

e.g.

struct bike {

int speed ;

float price ;

char *model ;

} ;

 

Once declared the structure can be used the same as any other data type:

 

e.g.

bike ducati ;

bike yamaha, honda ;

 

Alternatively a structure can be declared with objects after the closing } and before the ; as follows:

 

e.g.

struct bike {

int speed ;

float price ;

char *model ;

} ducati, yamaha, honda ; //three bikes structs declared

 

Values are assigned in a similar manner to arrays by use of the curly braces:

 

e.g.

ducati = { 145, 7655, "Monster" }

 

and can also be assigned within the struct declaration:

 

e.g.

struct bike {

int speed ;

float price ;

char *model ;

} ducati, yamaha = { 128, 5699, "FZR1" }, honda ; //three bikes structs declared, one of which is also initialised with values

 

 

Individual members are referred to by using the . dot member operator, which connects the structure name with the member name:

 

e.g.

ducati.speed

 

This allows individual structure members to be assigned values or return their values:

 

e.g.

honda.speed = 144 ;  //sets the speed member of the honda struct

cout << honda.speed ;  //gets the speed of the honda struct

 

 

#include <iostream>
using namespace std;

//declare a structure (user defined data type) called vehicle, with 2 objects called: mine, yours
struct vehicle {
	string make;
	int speed;
	float price;
} mine, yours, hailwood = {"Ducati", 185, 27999} ;

//function prototype called details, declaring a variable called bike of the data type vehicle
void details (vehicle bike);

int main () {
	mine.make = "Fantic"; //assign values to the members of the 'mine' structure
	mine.speed = 65;
	mine.price = 320;

	vehicle agostini = {"MV Augusta", 179, 33864 } ;

	cout << "Enter make: "; //assign values to the members of the 'yours' structure
	cin >> yours.make;
	cout << "Enter speed: ";
	cin >> yours.speed;
	cout << "Enter price: ";
	cin >> yours.price;

	cout << "\nMy bike is a "; 	//print the variable’s values
	details (mine);				//passing the 'mine' structure to the details function
	cout << "Your bike is a "; 	//and again for ‘yours’
	details (yours);
	cout << "And let's not forget Hailwood's "; 	//and again for ‘yours’
	details (hailwood);
	cout << "Or Agostini's "; 	//and again for ‘yours’
	details (agostini);

	return 0;
}

void details (vehicle bike) {
	cout << bike.make;
	cout << " with a top speed of " << bike.speed << "mph";
	cout << " costing £" << bike.price << "\n\n";
}

Compile & Run:

Enter make: honda
Enter speed: 120
Enter price: 5699 

 

My bike is a Fantic with a top speed of 65mph costing £320

 

Your bike is a honda with a top speed of 120mph costing £5699

 

And let's not forget Hailwood's Ducati with a top speed of 185mph costing £27999

 

Or Agostini's MV Augusta with a top speed of 179mph costing £33864

 

 

 

(*Note: Unlike C, the keyword struct does not have to be used in the declaration of a new struct, as per line 19)

 


 

In C++ structures are quite similar to classes and can incorporate member functions. However, the default visibility of members within a structure is public, whereas they are private in a class.

 

Example of a structure with methods:

Leave a Reply