This is part 14 of my series, C++ Taking the Bull by the Horns. We have seen the fundamental object types, which are, int, float, _Bool, char and void. We have seen three derived object types, which are the enum, the pointer and the array. In this part of the series, we shall look at another derived object type, which is called structure.
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
As I said, I present C++ to you in this series the way the inventors see it. I do the presentation in simple terms. I believe that in this way you would understand C++ better. Remember, take things in this series as I give you. Do not try to add or subtract any idea in your mind to or from what I give you; that would be misleading. You can do any subtraction or addition after you complete the series.
Reasons for having the Structure
An array is a set of objects of the same type. There are times when you need a set of objects of different types. The credentials of a man form a set of objects of different types. The credentials of a man can consist of his name, his age, his salary and his highest qualification. His name e.g. "John Smith", is a string. His age can be considered as an integer if you are not interested in fractional years. His salary (per week) is a float, because it may have some decimal digits, e.g. $1425.45. His highest qualification is a string. A structure is abbreviated as struct.
A struct Example
Imagine that John Smith mentioned above is my friend. The following code handles a struct for his credentials (the explanation is given below):
#include
using namespace std;
int main()
{
struct {char *name; int age; float salary; char *HQ;} myFriend;
myFriend.name = "John Smith";
myFriend.age = 30;
myFriend.salary = 1425.45;
myFriend.HQ = "Msc";
cout You can try the above code (do not worry about any error message for now).
The first line in the main function block, creates the struct and makes it an object type for the identifier, myFriend. It begins with the reserved word, struct. After that you have a space and a block. It is in the block that you actually make your derived object type, which is a struct. After the block you have the identifier, myFriend. This identifier is for an object whose nature you have derived in the block.
In the block, you have declarations of basic object types. In the case of an array, everything in the block would have been of the same object type. In our block above, the first object has the identifier, name. It is a pointer to a char. It is for my friend's name. The second object has the identifier, age. It is of type int (we ignore fractions of years). It is for my friend's age. The next object has the identifier, salary. It is of type, float. It is for my friend's salary. The last object has the identifier, HQ for highest qualification. It is a pointer (memory address) to char. Remember, if a string in double quotes is assigned to a pointer to char, the pointer becomes a "string" pointer.
The next code segment in the code assigns values to those basic objects. To assign a value, you begin with the identifier of the struct, which in this case is myFriend, followed by a dot and then the identifier of the basic object in the struct. In the case of string it is the identifier of a pointer to char. The combination of the struct identifier, the dot and an internal struct object identifier refers to the internal struct object and would return the internal object content or have assigned an internal (basic) object value. If the internal object is a char pointer, then a char pointer will be returned from the combination. The next code segment displays the assigned values of the internal struct objects. Remember, the cout object would take as operand a string pointer and obtain the value (string) from the pointer and return it (send).
Note that for the assignment of the salary value, the currency ($) symbol was not included. This is because the value has to be float (made up of only digits and a decimal point) and $ is not a float digit.
The Derived Object Type of struct
Struct is an object type; precisely, a derived object type. It is an object type that you the programmer actually create. You can create a struct of credentials. Consider the following table, which gives the credentials of four employees in a company:
EmployeeID, Name, Age, Salary, HQ
Emp1 Mary Jones 35 $3856.42 Phd
Emp2 John Carlson 36 $2124.32 Msc
Emp3 James Taylor 32 $1523.42 Bsc
Emp4 Suzan White 33 $1424.32 Bsc
You can create a struct for these credentials. Here a credential refers to the name, age, salary, and highest qualification of an employee.
Creating a Structure
In simple terms, the syntax to create a struct is:
struct {Type IdentA; Type IdentB; Type IdentC; ...} structIdent1, structIdent2, structIdent3,...;
You begin with the reserved word (keyword), struct. This is followed by a space and then a block. Inside the block you have statements. The statements in the block are separated by semicolons. Each statement is the declaration of an object. The last statement in the block also has a semicolon. After the block you have identifiers for objects that will have the particular struct type. These identifiers are separated by commas. At the end of the struct creation statement, you still have a semicolon that marks the end of the struct statement. Each object in the block is called a member of the struct.
More than one Identifier for a struct
In the code below, a struct is created for the credentials for four employees in a company. Well, to save time and space, only the data for two of the employees are assigned; the age of one of the employees is displayed. The data for the code is taken from the above employee table.
#include
using namespace std;
int main()
{
struct {char *name; int age; float salary; char *HQ;} Emp1, Emp2, Emp3, Emp4;
Emp1.name = "Mary Jones";
Emp1.age = 35;
Emp1.salary = 3856.42;
Emp1.HQ = "Phd";
Emp2.name = "John Carlson";
Emp2.age = 36;
Emp2.salary = 2124.32;
Emp2.HQ = "Msc";
cout
return 0;
}
In the line of the code where the struct is created, four identifiers are declared. The content type of what will be in each derived object of the four identifiers is what (the internal objects) is in the struct block. For the second code segment in the block of the main function, the values for the internal objects of the identifier, Emp1 are assigned. By the end of this code segment, the derived object identified by Emp1 has all its content. Its content is made of objects whose values have been assigned. That is, its content is made of objects that are not empty at the end of the code segment. Values can be assigned to other struct objects (Emp2, Emp3, Emp4) in the same way. To get the value of one of the objects of a struct object, type the identifier of the struct object first, then the dot and then the identifier of the corresponding internal object. That is what is done with the cout object.
Object Type from Struct
Hey, you can make an object type from struct. We know fundamental objects types that are int, float, _Bool, char and void. We know derived object types that are enum, pointer, array and now struct. Note that you are the one who decides on what objects will be in the block of a struct. There are so many possibilities. Now that there are so many possibilities, possibilities full into sets, similar to the idea that you can integers as a set or floats as a set. So far as credentials are concerned (above), you can have credentials for employees, you can have credentials for friends, you can have credentials for family members, and so on.
Let us talk about fruits. If you go to a shop, you may see pears. To buy pears, you may need to know the weight and price. You may also see oranges. To buy oranges you may also need to know the weight and price. You can create your own object type, which you would call, fruits. Each fruit is a struct. The struct has the declaration for weight and price. In simple terms the syntax to create an object type from struct is:
struct typeName {Type IdentA; Type IdentB; Type IdentC; ...} structIdent1, structIdent2, structIdent3,...;
The syntax is the same as before except that you have the object type name in front of the block.
The following code would create the object type, fruits for you:
struct fruits {float weight; float price;} pear, orange;
You now have an object type, fruits. From it you can make the object, pear with its own weight and price; you can also make the object, orange with its own weight and price. Here, pear and orange are object identifiers, while fruits is object type you have created.
The following procedure will produce the same effects as the above statement:
struct fruits {float weight; float price;};
fruits pear;
fruits orange;
The last two statements clearly illustrate that you can create your own object types, from structs. The two statements are similar to
int myInt;
int hisInt;
Read and try the following code that illustrates this:
#include
using namespace std;
int main()
{
struct fruits {float weight; float price;};
fruits pear;
fruits orange;
orange.weight = 1.2; //weight in kg
orange.price = 3.00; //price in $
pear.weight = 0.8; //weight in kg
pear.price = 3.25; //price in $
cout
return 0;
}
You output should be 0.8.
Note: structs are actually called DATA structures.
Time to take a break. Let us stop here and continue in the next part of the series.
Chrys
To arrive at any of the parts of this series, just type the corresponding title below in the Search Box of this page and click Search (you can also use any available links):
C++ Taking the Bull by the Horns - Part 1
C++ Taking the Bull by the Horns - Part 2
C++ Taking the Bull by the Horns - Part 3
C++ Taking the Bull by the Horns - Part 4
C++ Taking the Bull by the Horns - Part 5
C++ Taking the Bull by the Horns - Part 6
C++ Taking the Bull by the Horns - Part 7
C++ Taking the Bull by the Horns - Part 8
C++ Taking the Bull by the Horns - Part 9
C++ Taking the Bull by the Horns - Part 10
C++ Taking the Bull by the Horns - Part 11
C++ Taking the Bull by the Horns - Part 12
C++ Taking the Bull by the Horns - Part 13
C++ Taking the Bull by the Horns - Part 14
C++ Taking the Bull by the Horns - Part 15
C++ Taking the Bull by the Horns - Part 16
C++ Taking the Bull by the Horns - Part 17
C++ Taking the Bull by the Horns - Part 18
C++ Taking the Bull by the Horns - Part 19
C++ Taking the Bull by the Horns - Part 20
C++ Taking the Bull by the Horns - Part 21
C++ Taking the Bull by the Horns - Part 22
C++ Taking the Bull by the Horns - Part 23
C++ Taking the Bull by the Horns - Part 24
C++ Taking the Bull by the Horns - Part 25
Published by Chrys Forcha
I have more than 10 years experience in computer programming, software, electronics and telecommunications. I have a First Degree in Electronics and a Master's Degree in Technical Education. As well a... View profile
- Different Structures of OrganizationsA brief explanation of five different structures an organization can follow: functional, divisional, matrix, network and virtual.
- Biofilms: The Newly Discovered LIving Structures of Bacteria!This is a report on recent studies of bacterial structures called Biofilms, and the organization within the structure is described as well.
Creative Homeownership Through Recycling Abandoned StructuresAbandoned structures are everywhere, just look around nearly any community. And often they are in stable enough condition to actually be viable candidates for recycling.- Data Structures: An Explanation of Data StructuresA basic explanation of various data structures involved in database construction.
Pawn Structures in ChessWhat do you do when you have doubled pawns, or the position is open? How is my play different in closed positions? This article summarises the ways to play in different pawn str...
- C Object Identifiers
- C++ Object Identifiers
- C++ Basic Object Types
- C Basic Object Types
- Advantages of Object-Oriented Programming
- The Nature and Uses of Dish and Pillar Structures
- Features of Object Oriented Programming



