0% found this document useful (0 votes)
24 views

Chapter 7 - Structures

Uploaded by

s1116035
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Chapter 7 - Structures

Uploaded by

s1116035
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

YUAN ZE UNIVERSITY – Fundamental Computer Programming- C++ Lab(I)

Chapter 7: Structures
C++ Structures
Structures (also called structs) are a way to group several related variables into one place. Each variable
in the structure is known as a member of the structure.

Unlike an array, a structure can contain many different data types (int, string, bool, etc.).

Create a Structure
To create a structure, use the struct keyword and declare each of its members inside curly braces.
After the declaration, specify the name of the structure variable (myStructure in the example below):
struct { // Structure declaration
int myNum; // Member (int variable)
string myString; // Member (string variable)
} myStructure; // Structure variable

Access Structure Members


To access members of a structure, use the dot syntax (.):
// Create a structure variable called myStructure
struct {
int myNum;
string myString;
} myStructure;

// Assign values to members of myStructure


myStructure.myNum = 1;
myStructure.myString = "Hello World!";

// Print members of myStructure


cout << myStructure.myNum << "\n";
cout << myStructure.myString << "\n";

One Structure in Multiple Variables


You can use a comma (,) to use one structure in many variables:
struct {
int myNum;
string myString;
} myStruct1, myStruct2, myStruct3;

// Multiple structure variables separated with commas

Lecturer: Quang-Thai Ho
YUAN ZE UNIVERSITY – Fundamental Computer Programming- C++ Lab(I)

This example shows how to use a structure in two different variables:


struct {
string brand;
string model;
int year;
} myCar1, myCar2; // We can add variables by separating them with a comma here

// Put data into the first structure


myCar1.brand = "BMW";
myCar1.model = "X5";
myCar1.year = 1999;

// Put data into the second structure


myCar2.brand = "Ford";
myCar2.model = "Mustang";
myCar2.year = 1969;

// Print the structure members


cout << myCar1.brand << " " << myCar1.model << " " << myCar1.year << "\n";
cout << myCar2.brand << " " << myCar2.model << " " << myCar2.year << "\n";

Named Structures
By giving a name to the structure, you can treat it as a data type. This means that you can create
variables with this structure anywhere in the program at any time.
To create a named structure, put the name of the structure right after the struct keyword:
struct myDataType { // This structure is named "myDataType"
int myNum;
string myString;
};

To declare a variable that uses the structure, use the name of the structure as the data type of the
variable:

myDataType myVar;

Example
// Declare a structure named "car"
struct car {
string brand;
string model;
int year;
};

int main() {
// Create a car structure and store it in myCar1;
car myCar1;
myCar1.brand = "BMW";
myCar1.model = "X5";
myCar1.year = 1999;

// Create another car structure and store it in myCar2;

Lecturer: Quang-Thai Ho
YUAN ZE UNIVERSITY – Fundamental Computer Programming- C++ Lab(I)

car myCar2;
myCar2.brand = "Ford";
myCar2.model = "Mustang";
myCar2.year = 1969;

// Print the structure members


cout << myCar1.brand << " " << myCar1.model << " " << myCar1.year << "\n";
cout << myCar2.brand << " " << myCar2.model << " " << myCar2.year << "\n";

return 0;
}

Lecturer: Quang-Thai Ho
YUAN ZE UNIVERSITY – Fundamental Computer Programming- C++ Lab(I)

Problem 1: Define a structure:


struct profile{
char fullname[40];
double score;
char grade[10];
};

Write a program to input the name and score of n students. Letter Grade is calculated by the
following table:

Score Letter Grade


>= 9.0 A
>= 8.0 && < 9.0 B+
>= 7.0 && < 8.0 B
>= 6.0 && < 7.0 C+
>= 5.0 && < 6.0 C
>= 4.5 && < 5.0 D+
>= 4.0 && < 4.5 D
< 4.0 F

Calculate letter grade for n students and print the list to the screen

Output:

FULL NAME SCORE GRADE

Lin Jia-Hao 7.5 B

Huang Mei-Ling 4.7 D+

Chen Zhi-Yong 9.2 A

Problem 2: Consider a fraction as a structure with two fields, the numerator, and the denominator.
Write a program that performs addition, subtraction, multiplication, and division of two fractions.
(Results must be minimal).
struct fraction{
int num, den;
};

Input: Please input fraction A: 2 15

Please input fraction B: 3 15

Please input the operator (+,-,*,/): +

Output: Result: 1/3

Lecturer: Quang-Thai Ho
YUAN ZE UNIVERSITY – Fundamental Computer Programming- C++ Lab(I)

Problem 3: Create a list of employees, each employee is considered as a structure including the fields
First Name, Last Name, Age, Address, and Salary. Enter some employees in the list, sort the names in
lexicographical order by given name, print the sorted list to the screen.
struct employee{
char firstname[20], lastname[10], address[30];
int age;
double salary;
};
Input: Please in put n: 3

Input employee #1:

First name: Jia-Hao

Last name: Lin

Age: 21

Address: 23 Yuan Dong Rd

Salary: 26400

Output:

FULL NAME Age Address Salary

Lin Jia-Hao 21 23 Yuan Dong Rd 26400

Huang Mei-Ling 20 81 Ming Guang Rd 25800

Chen Zhi-Yong 23 121 Zhong Shan Rd 32000

Lecturer: Quang-Thai Ho

You might also like