Data Structures
(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 CDAccount What is the type of each of the
{ following?
double balance; account.balance
double interest_rate; account.interest_rate
int term; account.term
char initial1; account.initial2
char initial2; CDAccount.term
}; Saving.initial1
CDAccount account; account
Given the structure and structure variable
declaration
struct CDAccount What is the type of each of the
{ following?
double balance; account.balance//double
double interest_rate; account.interest_rate//double
int term; account.term // int
char initial1; account.initial2 //char
char initial2; CDAccount.term//illigal
}; Saving.initial1//undeclared
CDAccount account; account//CDAccount
Find the output produced by the following code.
struct shoeType {
char style;
double price; A ¢ 99
}; A ¢ 11
Int main(){
shoeType shoe1, shoe2;
shoe1.style = ‘A’;
shoe1.price = 99;
cout<<shoe1.style<<“ ¢”<<shoe1.price<<endl;
shoe2.style = shoe1.style;
Shoe2.price= shoe1.price/9;
Example
#include <iostream> person pers;//object
#include <string> cout<<"Enter person's name: ";
getline(cin,pers.name);
using namespace std;
cout<<"Enter height: ";
cin>>pers.height;
struct person
{ cout<<"Outputting person data
string name; "<<endl;
cout<<"==============="<<endl;
int height;
}; cout<<"Your name is "<<pers.name<<"
and height is "
int main() <<pers.height<<endl<<endl;
}
#include <iostream>
#include <string> Example
using namespace std;
cout<<"Outputting person data "<<endl;
const int MAX = 3; cout<<"==================="<<endl;
struct person
{
for(int x=0;x<MAX;x++)
string name;
cout<<"Person no. "<<x+1<<"s name is "
int height;
};
<<pers[x].name<<" and height is "
<<pers[x].height<<endl<<endl;
int main() }
{
person pers[MAX];//object is array
for(int x=0;x<MAX; ++x)
{
cout<<"Enter person's name: ";
cin>>pers[x].name;
cout<<"Enter height: ";
cin>>pers[x].height;
// example about structures Example
#include <iostream>
#include <string>
cout << "Enter title: ";
using namespace std;
getline (cin,yours.title);
cout << "Enter year: ";
struct movies{ cin>>yours.year;
string title; mystr = yours.title;
int year; yr = yours.year;
} mine, yours; cout<<endl<<endl;
int main () cout << "My favorite movie is:\n ";
{
cout<<mine.title<<endl;
string mystr;
cout<<“ year is ”<<mine.year;
int yr;
cout << "And yours is:\n ";
mine.title = "2001 A Space Odyssey";
cout<<yours.title;<<“ and
mine.year = 1968; ”<<yours.year;
return 0;
NB
struct movies{ struct movies{
string title; string title;
int year; int year;
} movies mine, } mine, yours;
your; //not allowed // allowed
struct movies{ struct movies{
string title; string title;
int year;
int year;
};
}; movies mine;
movies mine, yours; movies yours;
// allowed // allowed
Constructor
• This is a function that is automatically called
when you attempt to create an instance of an
object.
• Default constructor
• You need not write a constructor. If you don’t,
the default constructor is called
#include<iostream>
#include <string>
using namespace std; The person's name is Adams Smith his
const int man = 3; height is 45
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
cout<<"The person's name is "
Trial
• The code int val[]={13,27,39,110};
• cout << val[4] << endl; was embeded in a correct c++
program, explain why it would not run.
•
• Write a code using val[x] to display the value 110 in “a”
above. Please note that x is an integer
• Explain the term “flow control” in c++. List the types of flow
controls available
• The code “ if (Chelsea = 17) ; ” was embedded in a correct c+
+ programme. Explain why it would not run and correct it to
run. NB: do not write a programme, just correct the code.