C++ Structured Data Types: Records in ECE, Exams of Engineering

An introduction to structured data types in c++, focusing on records and hierarchical records. Topics covered include the syntax for declaring and accessing individual components of records, using records, aggregate operations on records, and accessing hierarchical record members. Examples are given to illustrate the concepts.

Typology: Exams

Pre 2010

Uploaded on 07/23/2009

koofers-user-z16-1
koofers-user-z16-1 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Electrical and Computer Engineering 1of 17
UAH CPE 112
Structured Data Types
Unlike a simple data type which has only a single
data item, a structured data type is one in which
each value is a collection of component items.
String is an example of a structured data type.
Individual elements are accessed by index:
myString[3]
C++ has the following structured data types:
struct, union, class, and arrays
Electrical and Computer Engineering 2of 17
UAH CPE 112
Records (C++ structs)
A record is a heterogeneous structured data
type (struct).
Each component of a record is called a field
of the record (member), and each field is
given a name called the field (member)
name.
Electrical and Computer Engineering 3of 17
UAH CPE 112
struct Syntax
StructDeclaration
struct TypeName
{MemberList
};
MemberList
DataType MemberName ;
DataType MemberName ;
.
.
.
Electrical and Computer Engineering 4of 17
UAH CPE 112
struct Example
enum GradeType (A, B, C, D, F);
struct StudentRec
{
string firstName;
string lastName;
float gpa;
int programGrade;
int quizGrade;
int finalExam;
GradeType courseGrade;
};
StudentRec firstStudent;
StudentRec student;
int grade;
Electrical and Computer Engineering 5of 17
UAH CPE 112
Accessing Individual Components
To access an individual member of a struct
variable, you give the name of the variable,
followed by a dot (period), and then the member
name. This expression is called a member selector.
MemberSelector
StructVariable.MemberName
Examples:
firstStudent.gpa
student.finalExam
Electrical and Computer Engineering 6of 17
UAH CPE 112
Using structs
cin >> student.finalExam;
grade = student.finalExam +
student.programGrade + student.quizGrade;
if (grade >= 900)
student.courseGrade = A;
else if (grade >= 800)
student.courseGrade = B;
else if (grade >= 700)
.
.
.
pf3

Partial preview of the text

Download C++ Structured Data Types: Records in ECE and more Exams Engineering in PDF only on Docsity!

Electrical and Computer Engineering

1 of 17

Structured Data Types

  • Unlike a simple data type which has only a single

data item, a structured data type is one in which

each value is a collection of component items.

  • String is an example of a structured data type.

Individual elements are accessed by index:

myString[3]

  • C++ has the following structured data types:

struct, union, class, and arrays

Electrical and Computer Engineering

2 of 17

Records (C++ structs)

  • A record is a heterogeneous structured data

type (struct).

  • Each component of a record is called a field

of the record (member), and each field is

given a name called the field (member)

name.

Electrical and Computer Engineering

3 of 17

UAH CPE 112

struct Syntax

StructDeclaration

struct TypeName

MemberList

MemberList

DataType MemberName ;

DataType MemberName ;

Electrical and Computer Engineering

4 of 17

UAH CPE 112

struct Example

enum GradeType (A, B, C, D, F); struct StudentRec { string firstName; string lastName; float gpa; int programGrade; int quizGrade; int finalExam; GradeType courseGrade; };

StudentRec firstStudent; StudentRec student; int grade;

Electrical and Computer Engineering 5 of 17

UAH CPE 112

Accessing Individual Components

  • To access an individual member of a struct

variable, you give the name of the variable,

followed by a dot (period), and then the member

name. This expression is called a member selector.

MemberSelector

StructVariable.MemberName

  • Examples:

firstStudent.gpa

student.finalExam

Electrical and Computer Engineering 6 of 17

UAH CPE 112

Using structs

cin >> student.finalExam;

grade = student.finalExam +

student.programGrade + student.quizGrade;

if (grade >= 900)

student.courseGrade = A;

else if (grade >= 800)

student.courseGrade = B;

else if (grade >= 700)

Electrical and Computer Engineering

7 of 17

Aggregate Operations on structs

  • An aggregate operation is one that manipulates the

struct as an entire unit.

  • You can assign a variable of a struct type to

another variable of that same type. You can pass a

struct variable to a function and return it as a

function’s value.

  • You cannot input an entire struct variable with

one statement. You cannot perfrom arithmetic or

comparison operations on struct variables.

Electrical and Computer Engineering

8 of 17

More About struct Declarations

  • You can declare variable names within the

struct declaration.

  • Example:
struct StudentRec
string firstName;
string lastName;
} firstStudent, student;

Electrical and Computer Engineering

9 of 17

UAH CPE 112

One More struct Example

#include using namespace std;

struct player { int assists; int points; int rebounds; };

int triple_double (struct player);

int main() { player shaq, the_admiral, kobe, jason; int shaq_td, jason_td; Electrical and Computer Engineering

10 of 17

UAH CPE 112

One More struct Example

shaq.assists = 2; shaq.points = 42; shaq.rebounds = 18;

jason. assists = 12; jason.points = 23; jason.rebounds = 10;

shaq_td = triple_double(shaq); cout << "shaq's triple double is " << shaq_td << endl; jason_td = triple_double(jason); cout << "jason's triple double is " << jason_td << endl; }

Electrical and Computer Engineering 11 of 17

UAH CPE 112

One More struct Example

int triple_double (struct player player_name) { int result;

if (player_name.assists >= 10 && player_name.points >= 10 && player_name.rebounds >= 10) result = 1; else result = 0; return result; }

Electrical and Computer Engineering 12 of 17

UAH CPE 112

Hierarchical Records

  • Records whose components are themselves

records are called hierarchical records.