C++ Session 9
C++ Session 9
(or Structures)
Java has no structure
Data Structures
• A data structure is a group of data elements
put together under one name.
• These data elements, known as members, can
have different types and different lengths.
• Data structures can be declared in C++ using
the following syntax:
Declaring Structures
• struct type_name {
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
} object_names ; //can be ignored
• There should be a semi column at the end
Explanation
• Where type_name is a name for the structure
type
• object_name can be a set of valid identifiers
for objects that have the type of this structure.
• Within braces {}, there is a list with the data
members,
• Each one is specified with a type and a valid
identifier as its name
Example
struct product {
int weight;
double price;
} apple, banana, melon; //are object variable
//The object variables can be ignored
Example
struct product {
int weight;
double price;
} ; //no object variable declared
Explanation
This declares a structure
struct product {
type called product.
int weight;
It defines product as having
double price; two members: weight and
} apple, banana, melon; price each of different data
type.
The declaration creates a
new type (product) which is
used to declare three object
(variables) namely apple,
banana, and melon.
Another way of declaring the objects if
ignored in the structure definition
struct product { This declares a structure
int weight; type called product.
double price; It defines product as having
}; two members: weight and
price each of different data
product apple; type.
product banana, melon; The declaration creates a
//they can all be on a new type (product) which is
straight line used to declare three object
(variables) called apple,
banana, and melon.
Example
struct Automobile struct Automobile
{ {
int year; int year, doors; //list
int doors;
double horsePower;
double horsePower;
char model;
char model;
};
};
// you can combine
member names of the
same type into a single
list separated by comers
Variables of a structure type can be declared the
same way as variables of other data types
struct Automobile
{
int year, doors; //list
double horsePower;
char model;
};
Automobile my_car, your_car;
//variables of type Automobile
Accessing members
• Once the three objects of a structure type are
declared (e.g. apple, banana, and melon) its
members can be accessed directly
• The syntax for that is simply to insert a dot (.)
between the object name and the member
name
Example
struct product { apple.weight
int weight; apple.price
double price; banana.weight
}; banana.price
product apple; melon.weight
product banana, melon; melon.price
//they can all be on a
straight ling
Initializing an object (order is important)
#include <iostream>
int main()
#include <string> {
using namespace std; person pers ={"Jeff Kent", 25};
//person pers ={25, “Amanda Boro”}
struct person //won’t work
{ cout<<"Outputting person data "<<endl;
cout<<"===================="<<endl;
string name;
int height; cout<<"Your name is "<<pers.name<<"
and height is "
}; <<pers.height<<endl<<endl;
Outputting person data
====================
} Your name is Jeff Kent and height is 25
Given the structure and structure variable
declaration
struct Person
{
string name;
int height;
Person()
{
name = "Adams Smith";
height = 45;
}
};
int main()
{
Person p;
cout<<"The person's name is "
<<p.name<<" his height is "<<p.height;
#include<iostream>
#include <string> Example
using namespace std;
const int man = 3;
The person’s name is
height is 2003767650
struct Person Default is called because p
{
string name;
was not initialized
int height;
};
int main()
{
Person p;//default constructor is
called
cout<<"The person's name is "
<<p.name<<" his height is
"<<p.height;
#include<iostream>
#include <string>
using namespace std;
const int man = 3; The person’s name is Ait student his height is 24
struct Person
{
string name;
int height;
};
int main()
{
Person p;
p.height = 24;
p.name = "Ait Student";
cout<<"The person's name is "
<<p.name<<" his height is "<<p.height;
#include<iostream>
#include <string>
using namespace std;
const int man = 3;
The person’s name is February
height is 25
struct Person
{
string name;
int height;
};
int main()
{
Person p= {"February",
25};//initialization