C++-Lesson 5 Structures
C++-Lesson 5 Structures
C++ CLASSES
-(Structures)
a) Refer to pdf: Chap_06
b) C++ How to program chapter 9
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:
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:
But
Method 2:
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:
return 0;
See below
Structures
Example1: Structures-Books
1:
2:
Examples
Example1: Structures-Books
string name;
int age;
float salary;
};