User Defined Types The Struct http cs mst

  • Slides: 35
Download presentation
User Defined Types – The Struct http: //cs. mst. edu

User Defined Types – The Struct http: //cs. mst. edu

struct § Intro to Object-Oriented Programming (OOP) § Allows us to abstract at a

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. http: //cs. mst. edu

struct syntax // placed in a header file struct type_name { member_type 1 member_name

struct syntax // placed in a header file struct type_name { member_type 1 member_name 1; member_type 2 member_name 2; . . . member_type. N member_name. N; }; http: //cs. mst. edu

syntax example // placed in a header file struct type_name { member_type 1 member_name

syntax example // placed in a header file struct type_name { member_type 1 member_name 1; member_type 2 member_name 2; . . . member_type. N member_name. N; }; // point. h struct point { float m_Xcoord; float m_Ycoord; }; http: //cs. mst. edu

syntax example // placed in a header file struct type_name { member_type 1 member_name

syntax example // placed in a header file struct type_name { member_type 1 member_name 1; member_type 2 member_name 2; . . . member_type. N member_name. N; }; // point. h struct point { float m_Xcoord; float m_Ycoord; }; http: //cs. mst. edu

syntax example // placed in a header file struct type_name { member_type 1 member_name

syntax example // placed in a header file struct type_name { member_type 1 member_name 1; member_type 2 member_name 2; . . . member_type. N member_name. N; }; // point. h struct point { float m_Xcoord; float m_Ycoord; }; http: //cs. mst. edu

syntax example // placed in a header file struct type_name { member_type 1 member_name

syntax example // placed in a header file struct type_name { member_type 1 member_name 1; member_type 2 member_name 2; . . . member_type. N member_name. N; }; // point. h struct point { float m_Xcoord; float m_Ycoord; }; http: //cs. mst. edu

syntax example // placed in a header file struct type_name { member_type 1 member_name

syntax example // placed in a header file struct type_name { member_type 1 member_name 1; member_type 2 member_name 2; . . . member_type. N member_name. N; }; // point. h struct point { float m_Xcoord; float m_Ycoord; }; http: //cs. mst. edu

syntax example // placed in a header file struct type_name { member_type 1 member_name

syntax example // placed in a header file struct type_name { member_type 1 member_name 1; member_type 2 member_name 2; . . . member_type. N member_name. N; }; // point. h struct point { float m_Xcoord; float m_Ycoord; }; http: //cs. mst. edu

syntax example // placed in a header file struct type_name { member_type 1 member_name

syntax example // placed in a header file struct type_name { member_type 1 member_name 1; member_type 2 member_name 2; . . . member_type. N member_name. N; }; // point. h struct point { float m_Xcoord; float m_Ycoord; }; http: //cs. mst. edu

struct in use int main() { point p 1, p 2; // 2 points

struct in use int main() { point p 1, p 2; // 2 points with 2 floats in each p 1. m_Xcoord = 4; p 1. m_Ycoord = 6; cout << "enter p 2’s x: "; cin >> p 2. m_Xcoord; cout << "and the y: "; cin >> p 2. m_Ycoord; cout << "the x coordinate of p 1 is “ << p 1. m_Xcoord; . . . http: //cs. mst. edu

struct in use int main() { point p 1, p 2; // 2 points

struct in use int main() { point p 1, p 2; // 2 points with 2 floats in each p 1. m_Xcoord = 4; p 1. m_Ycoord = 6; cout << "enter p 2’s x: "; cin >> p 2. m_Xcoord; cout << "and the y: "; cin >> p 2. m_Ycoord; cout << "the x coordinate of p 1 is “ << p 1. m_Xcoord; . . . http: //cs. mst. edu

struct in use int main() { point p 1, p 2; // 2 points

struct in use int main() { point p 1, p 2; // 2 points with 2 floats in each p 1. m_Xcoord = 4; p 1. m_Ycoord = 6; cout << "enter p 2’s x: "; cin >> p 2. m_Xcoord; cout << "and the y: "; cin >> p 2. m_Ycoord; cout << "the x coordinate of p 1 is “ << p 1. m_Xcoord; . . . http: //cs. mst. edu

struct in use int main() { point p 1, p 2; // 2 points

struct in use int main() { point p 1, p 2; // 2 points with 2 floats in each p 1. m_Xcoord = 4; p 1. m_Ycoord = 6; cout << "enter p 2’s x: "; cin >> p 2. m_Xcoord; cout << "and the y: "; cin >> p 2. m_Ycoord; cout << "the x coordinate of p 1 is “ << p 1. m_Xcoord; . . . http: //cs. mst. edu

struct in use int main() { point p 1, p 2; // 2 points

struct in use int main() { point p 1, p 2; // 2 points with 2 floats in each p 1. m_Xcoord = 4; p 1. m_Ycoord = 6; cout << "enter p 2’s x: "; cin >> p 2. m_Xcoord; cout << "and the y: "; cin >> p 2. m_Ycoord; cout << "the x coordinate of p 1 is “ << p 1. m_Xcoord; . . . http: //cs. mst. edu

struct in use int main() { point p 1, p 2; // 2 points

struct in use int main() { point p 1, p 2; // 2 points with 2 floats in each p 1. m_Xcoord = 4; p 1. m_Ycoord = 6; cout << "enter p 2’s x: "; cin >> p 2. m_Xcoord; cout << "and the y: "; cin >> p 2. m_Ycoord; cout << "the x coordinate of p 1 is “ << p 1. m_Xcoord; . . . http: //cs. mst. edu

structs within structs struct point { float m_Xcoord; float m_Ycoord; }; struct line {

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; . . . http: //cs. mst. edu

structs within structs struct point { float m_Xcoord; float m_Ycoord; }; struct line {

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; . . . http: //cs. mst. edu

structs within structs struct point { float m_Xcoord; float m_Ycoord; }; line my_line struct

structs within structs struct point { float m_Xcoord; float m_Ycoord; }; line my_line 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; . . . http: //cs. mst. edu

structs within structs struct point { float m_Xcoord; float m_Ycoord; }; line my_line point

structs within structs struct point { float m_Xcoord; float m_Ycoord; }; line my_line point m_Left struct line { point m_Left; point m_Right; }; point m_Right int main() { line my_line; my_line. m_Left. m_Xcoord = 5; my_line. m_Left. m_Ycoord = 8; . . . http: //cs. mst. edu

structs within structs struct point { float m_Xcoord; float m_Ycoord; }; line my_line point

structs within structs struct point { float m_Xcoord; float m_Ycoord; }; line my_line point m_Left float m_Xcoord struct line { point m_Left; point m_Right; }; float m_Ycoord point m_Right int main() { line my_line; my_line. m_Left. m_Xcoord = 5; my_line. m_Left. m_Ycoord = 8; . . . float m_Xcoord http: //cs. mst. edu float m_Ycoord

structs within structs struct point { float m_Xcoord; float m_Ycoord; }; line my_line point

structs within structs struct point { float m_Xcoord; float m_Ycoord; }; line my_line point m_Left float m_Xcoord struct line { point m_Left; point m_Right; }; float m_Ycoord point m_Right int main() { line my_line; my_line. m_Left. m_Xcoord = 5; my_line. m_Left. m_Ycoord = 8; . . . float m_Xcoord http: //cs. mst. edu float m_Ycoord

structs within structs struct point { float m_Xcoord; float m_Ycoord; }; line my_line point

structs within structs struct point { float m_Xcoord; float m_Ycoord; }; line my_line point m_Left float m_Xcoord struct line { point m_Left; point m_Right; }; float m_Ycoord point m_Right int main() { line my_line; my_line. m_Left. m_Xcoord = 5; my_line. m_Left. m_Ycoord = 8; . . . float m_Xcoord http: //cs. mst. edu float m_Ycoord

structs within structs struct point { float m_Xcoord; float m_Ycoord; }; line my_line point

structs within structs struct point { float m_Xcoord; float m_Ycoord; }; line my_line point m_Left float m_Xcoord struct line { point m_Left; point m_Right; }; float m_Ycoord point m_Right int main() { line my_line; my_line. m_Left. m_Xcoord = 5; my_line. m_Left. m_Ycoord = 8; . . . float m_Xcoord http: //cs. mst. edu float m_Ycoord

structs within structs struct point { float m_Xcoord; float m_Ycoord; }; line my_line point

structs within structs struct point { float m_Xcoord; float m_Ycoord; }; line my_line point m_Left float m_Xcoord struct line { point m_Left; point m_Right; }; float m_Ycoord point m_Right int main() { line my_line; my_line. m_Left. m_Xcoord = 5; my_line. m_Left. m_Ycoord = 8; . . . float m_Xcoord http: //cs. mst. edu float m_Ycoord

structs within structs struct point { float m_Xcoord; float m_Ycoord; }; line my_line point

structs within structs struct point { float m_Xcoord; float m_Ycoord; }; line my_line point m_Left float m_Xcoord struct line { point m_Left; point m_Right; }; float m_Ycoord 5 point m_Right int main() { line my_line; my_line. m_Left. m_Xcoord = 5; my_line. m_Left. m_Ycoord = 8; . . . float m_Xcoord http: //cs. mst. edu float m_Ycoord

structs within structs struct point { float m_Xcoord; float m_Ycoord; }; line my_line point

structs within structs struct point { float m_Xcoord; float m_Ycoord; }; line my_line point m_Left struct line { point m_Left; point m_Right; }; float m_Xcoord float m_Ycoord 5 8 point m_Right int main() { line my_line; my_line. m_Left. m_Xcoord = 5; my_line. m_Left. m_Ycoord = 8; . . . float m_Xcoord http: //cs. mst. edu float m_Ycoord

Another Example struct car_part { string m_description; long m_part. Number; float m_wholesale. Price; float

Another Example struct car_part { string m_description; long m_part. Number; float m_wholesale. Price; float m_retail. Price; string m_color; etc }; http: //cs. mst. edu

Forward Declarations struct point; struct line { point m_Left; . . . }; struct

Forward Declarations struct point; struct line { point m_Left; . . . }; struct point {. . . }; http: //cs. mst. edu

Forward Declarations struct point; struct line { point m_Left; . . . }; struct

Forward Declarations struct point; struct line { point m_Left; . . . }; struct point {. . . }; http: //cs. mst. edu

Pro Tips struct complex_number { float m_Real. Part; float m_Imaginary. Part; string m_name; }

Pro Tips struct complex_number { float m_Real. Part; float m_Imaginary. Part; string m_name; } http: //cs. mst. edu

Pro Tips struct complex_number { float m_Real. Part; float m_Imaginary. Part; string m_name; }

Pro Tips struct complex_number { float m_Real. Part; float m_Imaginary. Part; string m_name; } http: //cs. mst. edu

Pro Tips struct complex_number { float m_Real. Part; float m_Imaginary. Part; string m_name; }

Pro Tips struct complex_number { float m_Real. Part; float m_Imaginary. Part; string m_name; } http: //cs. mst. edu

Pro Tips struct complex_number { float m_Real. Part; float m_Imaginary. Part; }; http: //cs.

Pro Tips struct complex_number { float m_Real. Part; float m_Imaginary. Part; }; http: //cs. mst. edu

End of Session http: //cs. mst. edu

End of Session http: //cs. mst. edu