Download presentation
Presentation is loading. Please wait.
1
User Defined Types – The Struct
2
struct Intro to Object-Oriented Programming (OOP)
Allows us to abstract at a higher level to build entities more complex than short, long, int, float, double, char, and bool With it programmers can create their own types to define what should make up a student, a class, a department, a university, etc.
3
struct syntax // placed in a header file struct type_name { member_type1 member_name1; member_type2 member_name2; . . member_typeN member_nameN; };
4
syntax example // placed in a header file struct type_name { member_type1 member_name1; member_type2 member_name2; . . member_typeN member_nameN; }; // point.h struct point { float m_Xcoord; float m_Ycoord; };
5
syntax example // placed in a header file struct type_name { member_type1 member_name1; member_type2 member_name2; . . member_typeN member_nameN; }; // point.h struct point { float m_Xcoord; float m_Ycoord; };
6
syntax example // placed in a header file struct type_name { member_type1 member_name1; member_type2 member_name2; . . member_typeN member_nameN; }; // point.h struct point { float m_Xcoord; float m_Ycoord; };
7
syntax example // placed in a header file struct type_name { member_type1 member_name1; member_type2 member_name2; . . member_typeN member_nameN; }; // point.h struct point { float m_Xcoord; float m_Ycoord; };
8
syntax example // placed in a header file struct type_name { member_type1 member_name1; member_type2 member_name2; . . member_typeN member_nameN; }; // point.h struct point { float m_Xcoord; float m_Ycoord; };
9
syntax example // placed in a header file struct type_name { member_type1 member_name1; member_type2 member_name2; . . member_typeN member_nameN; }; // point.h struct point { float m_Xcoord; float m_Ycoord; };
10
syntax example // placed in a header file struct type_name { member_type1 member_name1; member_type2 member_name2; . . member_typeN member_nameN; }; // point.h struct point { float m_Xcoord; float m_Ycoord; };
11
struct in use int main() { point p1, p2; // 2 points with 2 floats in each p1.m_Xcoord = 4; p1.m_Ycoord = 6; cout << "enter p2’s x: "; cin >> p2.m_Xcoord; cout << "and the y: "; cin >> p2.m_Ycoord; cout << "the x coordinate of p1 is “ << p1.m_Xcoord; ...
12
struct in use int main() { point p1, p2; // 2 points with 2 floats in each p1.m_Xcoord = 4; p1.m_Ycoord = 6; cout << "enter p2’s x: "; cin >> p2.m_Xcoord; cout << "and the y: "; cin >> p2.m_Ycoord; cout << "the x coordinate of p1 is “ << p1.m_Xcoord; ...
13
struct in use int main() { point p1, p2; // 2 points with 2 floats in each p1.m_Xcoord = 4; p1.m_Ycoord = 6; cout << "enter p2’s x: "; cin >> p2.m_Xcoord; cout << "and the y: "; cin >> p2.m_Ycoord; cout << "the x coordinate of p1 is “ << p1.m_Xcoord; ...
14
struct in use int main() { point p1, p2; // 2 points with 2 floats in each p1.m_Xcoord = 4; p1.m_Ycoord = 6; cout << "enter p2’s x: "; cin >> p2.m_Xcoord; cout << "and the y: "; cin >> p2.m_Ycoord; cout << "the x coordinate of p1 is “ << p1.m_Xcoord; ...
15
struct in use int main() { point p1, p2; // 2 points with 2 floats in each p1.m_Xcoord = 4; p1.m_Ycoord = 6; cout << "enter p2’s x: "; cin >> p2.m_Xcoord; cout << "and the y: "; cin >> p2.m_Ycoord; cout << "the x coordinate of p1 is “ << p1.m_Xcoord; ...
16
struct in use int main() { point p1, p2; // 2 points with 2 floats in each p1.m_Xcoord = 4; p1.m_Ycoord = 6; cout << "enter p2’s x: "; cin >> p2.m_Xcoord; cout << "and the y: "; cin >> p2.m_Ycoord; cout << "the x coordinate of p1 is “ << p1.m_Xcoord; ...
17
structs within structs
struct point { float m_Xcoord; float m_Ycoord; }; struct line point m_Left; point m_Right; int main() line my_line; my_line.m_Left.m_Xcoord = 5; my_line.m_Left.m_Ycoord = 8; ...
18
structs within structs
struct point { float m_Xcoord; float m_Ycoord; }; struct line point m_Left; point m_Right; int main() line my_line; my_line.m_Left.m_Xcoord = 5; my_line.m_Left.m_Ycoord = 8; ...
19
structs within structs
struct point { float m_Xcoord; float m_Ycoord; }; struct line point m_Left; point m_Right; int main() line my_line; my_line.m_Left.m_Xcoord = 5; my_line.m_Left.m_Ycoord = 8; ... line my_line
20
structs within structs
struct point { float m_Xcoord; float m_Ycoord; }; struct line point m_Left; point m_Right; int main() line my_line; my_line.m_Left.m_Xcoord = 5; my_line.m_Left.m_Ycoord = 8; ... line my_line point m_Left point m_Right
21
structs within structs
struct point { float m_Xcoord; float m_Ycoord; }; struct line point m_Left; point m_Right; int main() line my_line; my_line.m_Left.m_Xcoord = 5; my_line.m_Left.m_Ycoord = 8; ... line my_line point m_Left float m_Xcoord float m_Ycoord point m_Right float m_Xcoord float m_Ycoord
22
structs within structs
struct point { float m_Xcoord; float m_Ycoord; }; struct line point m_Left; point m_Right; int main() line my_line; my_line.m_Left.m_Xcoord = 5; my_line.m_Left.m_Ycoord = 8; ... line my_line point m_Left float m_Xcoord float m_Ycoord point m_Right float m_Xcoord float m_Ycoord
23
structs within structs
struct point { float m_Xcoord; float m_Ycoord; }; struct line point m_Left; point m_Right; int main() line my_line; my_line.m_Left.m_Xcoord = 5; my_line.m_Left.m_Ycoord = 8; ... line my_line point m_Left float m_Xcoord float m_Ycoord point m_Right float m_Xcoord float m_Ycoord
24
structs within structs
struct point { float m_Xcoord; float m_Ycoord; }; struct line point m_Left; point m_Right; int main() line my_line; my_line.m_Left.m_Xcoord = 5; my_line.m_Left.m_Ycoord = 8; ... line my_line point m_Left float m_Xcoord float m_Ycoord point m_Right float m_Xcoord float m_Ycoord
25
structs within structs
struct point { float m_Xcoord; float m_Ycoord; }; struct line point m_Left; point m_Right; int main() line my_line; my_line.m_Left.m_Xcoord = 5; my_line.m_Left.m_Ycoord = 8; ... line my_line point m_Left float m_Xcoord float m_Ycoord point m_Right float m_Xcoord float m_Ycoord
26
structs within structs
struct point { float m_Xcoord; float m_Ycoord; }; struct line point m_Left; point m_Right; int main() line my_line; my_line.m_Left.m_Xcoord = 5; my_line.m_Left.m_Ycoord = 8; ... line my_line point m_Left float m_Xcoord float m_Ycoord 5 point m_Right float m_Xcoord float m_Ycoord
27
structs within structs
struct point { float m_Xcoord; float m_Ycoord; }; struct line point m_Left; point m_Right; int main() line my_line; my_line.m_Left.m_Xcoord = 5; my_line.m_Left.m_Ycoord = 8; ... line my_line point m_Left float m_Xcoord float m_Ycoord 5 8 point m_Right float m_Xcoord float m_Ycoord
28
Another Example struct car_part { string m_description; long m_partNumber; float m_wholesalePrice; float m_retailPrice; string m_color; etc };
29
Forward Declarations struct point; struct line { point m_Left; ... }; struct point
30
Forward Declarations struct point; struct line { point m_Left; ... }; struct point
31
Pro Tips struct complex_number { float m_RealPart; float m_ImaginaryPart; string m_name; }
32
Pro Tips struct complex_number { float m_RealPart; float m_ImaginaryPart; string m_name; }
33
Pro Tips struct complex_number { float m_RealPart; float m_ImaginaryPart; string m_name; }
34
Pro Tips struct complex_number { float m_RealPart; float m_ImaginaryPart; };
35
End of Session
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.