Structures Overview
A struct is used to represent a complex object with internal
structure.
struct Student
{
string ssn;
string name;
int age;
};
Student A, B;
A and B each have their own properties
A -- has its own ssn, name, and age
B -- has its own ssn, name, and age
Like every other variable, each can be manipulated independently
A.name = "Bob";
A.ssn = "234";
A.age = 25;
B.name = "Jim";
B.ssn = "678";
B.age = 27;
Structure Assignment
You can use assignment with most structures (exceptions will be
pointed out later in the course)
In the above example:
A = B;
Each field of B is assigned to the CORRESPONDING field of A
It is equivalent to writing:
A.ssn = B.ssn; A.name = B.name; A.age = B.age;
No Equality == Operator for Structures
Unlike other types, you MAY NOT use "==" or "!=" with structures, as
you can with built in types like int, short, char, string, etc.
Student A, B;
You MAY NOT do the following:
if (A == B) cout << "They are the same." << endl;
Objects may not be compared for equality (or inequality)
You can compare the individual fields (which are integer or
string or some other built in types)
if (A.name == B.name) ... // is legal
if (A.name == "Bob") ... // is also legal
These operators must be defined through a process called "operator
overloading", which we probably will not have time to get into in this
course.
Passing structures as parameters
struct Student
{
string ssn;
string name;
int age;
};
// passing struct by value
void display ( Student x)
{
cout << "Student ssn: " << ssn << endl
<< "\tName: " << x.name << endl
<< "\tAge: " << x.age << endl;
}
// passing struct by reference
void change_name ( Student & x, string s)
{
if (isalpha(s[0]) && (name.size() >= 2))
x.name = s;
else
cout << "Name is not valid\n" << endl;
}
you can call the function "change_name" as below:
Student foo;
change_name(foo, "Bill");
example30.cpp passing structures as parameters (by value or reference)
How can we create a collection of objects such as students?
One of the common representations of a collection is to use
an array, of course. So, we create an array of objects:
Student club[15]; // creates a collection of 15 objects
Indexed from 0 to 14 as you might expect
Each entry is an identical "Student" object with
3 internal attributes/fields:
ssn, name, age
You can still use the "." notation with each array element:
club[0].name = "Bob";
club[0].ssn = "123";
club[0].age = 25;
change_name(club[0], "Jane");
With strucures, you are able to program many different kinds of
problems. Often you hear about "DATABASES". Many databases are
essentially filled with tables. Each table can be thought of as a
structure:
Table of Available Cars:
Manufacturer Model Type Seats Price
Toyota Camry Sedan 5 25000
Honda Civic Hatchback 4 17000
Nissan Altima Sedan 5 22000
Ford Taurus Sedan 5 21000
Ford Explorer SUV 6 28000
You can model the above table as follows:
enum Car_Type { Sedan, Hatchback, SUV, Truck };
struct Car
{
string manufacturer;
string model;
Car_Type type;
int seats;
int price;
};
example31.cpp Reading in and processing an array structures
The University of Southern California does not screen or control the content on this website and thus does not guarantee the accuracy, integrity, or quality of such content. All content on this website is provided by and is the sole responsibility of the person from which such content originated, and such content does not necessarily reflect the opinions of the University administration or the Board of Trustees