Learning Outcomes of the Week: WEEK 9
Computer Programming II C++
C++ CLASSES
-(Structures)
a) Refer to pdf: Chap_06
b) C++ How to program chapter 9
4/24/2024 © 2022. Cavendish University. Rights Reserved 0
Introduction
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, float, char, etc.).
Introduction
STRUCTURES:
Structures are used to represent a record,
suppose you want to keep track of your books
in a library. Or students records in class:
You might want to track the following
attributes about each book −
Introduction
STRUCTURES:
Structures are used to represent a record,
suppose you want to keep track of your books in
a library. Or students records in class:
You might want to track the following attributes
about each book −
Structures
Remember: Arrays allow you to define the type of variables
that can hold several data items of the same type / kind
But
structure is another user defined data type available in C ++
programming, which allows you to combine data items of
different kinds.
Suppose you want to store some information about a person:
his/her name, age, and salary.
You can easily create different variables—name, age, salary
—to store this information separately.
However, in the future, you might want to store information
about multiple people.
Now, you'd need to create different variables for each
information per person: name1, age1, salary1, name2, age2,
salary2,
(N.B: Arrays will be tedious to use)
Example: A student can have
• Names (data type ???)
• Age (data type ???)
• Address (data type ???)
• Phone Number (data type ???)
• Amount (data type ???)
etc etc all these attributes/items will have different
data types
(N.B: Arrays will be tedious to use)
Creating a Structure
• You can create a structure by using the struct
keyword and declare each of its members inside
curly braces
To access the structure, you must create a variable
of it.
• Use the struct keyword inside the main() method,
followed by the name of the structure and then
the name of the structure variable: eg-----→
Creating a Structure : example
• // Create a structure called myStructure
struct myStructure
• {
int myNum;
char myLetter;
};
int main() {
// Create a structure variable of myStructure called s1
struct myStructure s1;
// Assign values to members of s1
s1.myNum = 13;
s1.myLetter = 'B';
Example: Creating a Structure
Use one structure to represent two cars:
//Method 1:
struct {
string brand;
string model;
int year;
} myCar1, myCar2;
Example
Use one structure to represent two cars:
// 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;
Example
Use one structure to represent two cars:
// Print the structure members
cout << myCar1.brand << " " << myCar1.model << " " <<
myCar1.year << "\n";
cout << myCar2.brand << " " << myCar2.model << " " <<
myCar2.year << "\n";
Example
Use one structure to represent two cars:
Method 2:
// Declare a structure named "car"
struct car {
string brand;
string model;
int year;
};
Example
Use one structure to represent two cars:
int main() {
// Create a car structure and store it in myCar1;
car myCar1;
myCar1.brand = "BMW";
myCar1.model = "X5";
myCar1.year = 1999;
Example
Use one structure to represent two cars:
// Create another car structure and store it in myCar2;
car myCar2;
myCar2.brand = "Ford";
myCar2.model = "Mustang";
myCar2.year = 1969;
Example
Use one structure to represent two cars:
// Print the structure members
cout << myCar1.brand << " " << myCar1.model << " " <<
myCar1.year << "\n";
cout << myCar2.brand << " " << myCar2.model << " " <<
myCar2.year << "\n";
return 0;
N.B(What is the problem with this program????Static)
Defining a Structure( e.g book)
To define a structure, you must use the struct
statement.
The struct statement defines a new data type, with
more than one member for your program.
The format of the struct statement is this:
See below
Structures
struct [structure tag]
{
member definition;
member definition;
... member definition; }
[one or more structure variables];
(structure tag is used to identify a particular kind of structure
& help in creating a variable inside main function)
Defining a Structure”Books”
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
}
Accessing Structure Members
To access any member of a structure, we use the
member access operator (.)(DOT OPERATOR).
Use the member access operator as a period between
the structure variable name and the structure
member that we wish to access.
strcpy( Book1.title, "C++ Programming");
Examples
See the codes and run it!!!!!!!!!!!!
Example1: Structures-Books
WHATS THE PROBLEM with the Example????
1:
2:
Examples
See the codes and run it!!!!!!!!!!!!
Example1: Structures-Books
WHATS THE PROBLEM with the Example????
N.B: assume you need to print 10 books
Structures as a function
You can pass a structure as a function
argument in very similar way as you pass any
other variable :
See the codes and run it!!!!!!!!!!!!
Example2: Structures-Books( as a function)
Example 3: Dynamic
struct Person
string name;
int age;
float salary;
};
See codes person and run
Tasks 1:
Write a C++ program to create a structure student,
containing
struct student {
char name[64];
char course[128];
int age;
int year;
}
1: Static with 3 records
Tasks 2:
Convert program above from static to dynamic
Prompt the user to enter the details: minimum of 3
records
Self study
2: Pointers to Structures.
You can define pointers to structures in very similar way
as you define pointer to any other variable (Research)