Struct
Struct
Struct
Structs in C++
Instructor: Ali AlSbou
Records (structs)
1
7/31/2018
Records (structs)
Declaring a Structure:
2
7/31/2018
Example (1):
• Example:
struct Date { The “Date” structure
int day;
int month; has 3 members,
int year; day, month & year.
} ;
3
7/31/2018
Examples
• Example:
struct StudentInfo{
int Id; The “StudentInfo”
int age; structure has 4 members
char Gender;
double CGA; of different types.
};
• Example:
struct StudentGrade{
char Name[15]; The “StudentGrade”
char Course[9]; structure has 5
int Lab[5];
int Homework[3]; members of
int Exam[2]; different array types.
}; 7
4
7/31/2018
Example
10
5
7/31/2018
11
Assigning Structures
12
6
7/31/2018
Assignment (continued)
The assignment statement:
student = newStd;
13
14
7
7/31/2018
Input/Output
15
Input/Output (continued)
16
8
7/31/2018
Example
#include <iostream> int main()
using namespace std; {
Person p1;
struct Person cout << "Enter Full name: ";
{ cin.get(p1.name, 50);
char name[50]; cout << "Enter age: ";
cin >> p1.age;
int age;
cout << "Enter salary: ";
float salary; cin >> p1.salary;
};
cout << "\nDisplaying Information." <<
endl;
cout << "Name: " << p1.name << endl;
cout <<"Age: " << p1.age << endl;
cout << "Salary: " << p1.salary;
return 0;
}
17
Example
#include <iostream> int main ()
#include <string> {
using namespace std; Student stu;
int i;
struct Student
cout << "Your name, please: ";
{ getline(cin, stu.name);
string name; cout << "\nYour id is: ";
int id; cin >> stu.id;
cout << "Enter your marks for three tests." << endl;
int mark[3]; for (i = 0; i < 3; i++)
}; {
cout << "Test " << i+1 << ": ";
cin >> stu.mark[i];
}
cout << endl;
cout << "Hello, " << stu.name << endl;
cout << "Your Student ID is " << stu.id << endl;
cout << "Your marks are: " << endl;
for (i = 0; i < 3; i++)
{ cout << "Test " << i+1 << ": " << stu.mark[i] << " " << endl;
}}
18
9
7/31/2018
Nesting structures
Structures can also be nested in such a way
that an element of a structure is itself another
structure:
struct movies_t {
string title;
int year; };
struct friends_t {
string name;
string email;
movies_t favorite_movie;
} charlie, maria;
19
Example:Nesting structures
#include<iostream.h>
struct distance
{
int feet;
float inches;
}
struct room
{
distance length; Void main ()
distance width; {
}; room dining;
dining.length.feet=13;
dining.length.inches=6.5;
dining.width.feet=10;
dining.width.inches=0.0;
float L=dining.length.feet+dining.length.inches/12;
float W=dining.width.feet+dining.width.inches/12;
cout<<”\n Dining room area is”<<L*W<<”Square feet”;
}
20
10
7/31/2018
21
22
11
7/31/2018
23
For example
The following program declares a structure called sample, and then
a function called f1( ) that takes a parameter of type sample
#include <iostream>
using namespace std;
arg.a = 1000;
struct sample {
int a;
arg.ch = 'X';
char ch; } ; f1(arg);
return 0; }
void f1(sample parm); void f1(sample parm)
int main() {
{ cout<<parm.a<<" "<<parm.ch<<
struct sample arg; "\n";
}
24
12
7/31/2018
Example
• struct fred
{ char x;
int y;
float z;
char s[10];
} mike;
Example
26
13
7/31/2018
Example
#include <iostream> int main()
struct Employee {
{ Employee joe = { 14, 32, 24.15 };
short id; Employee frank = { 15, 28, 18.27 };
int age; printInformation(joe);
double wage; cout << "\n";
}; printInformation(frank);
void printInformation(Employee employee) return 0;
{
}
cout << "ID: " << employee.id << "\n";
cout << "Age: " << employee.age << "\n";
cout << "Wage: " << employee.wage << "\n";
}
27
Structure Pointers
28
14
7/31/2018
Structure Pointers
• To find the address of a structure variable, you must place the &
operator before the structure variable's name.
• For example, given the following fragment,
struct bal {
float balance;
char name[80];
} person;
bal *p; // declare a structure pointer
then
p = &person;
puts the address of person into the pointer p.
29
Structure Pointers
30
15