YouTip LogoYouTip

Cpp Struct

C/C++ arrays allow you to define variables that store data items of the same type, but a **struct** is another user-defined data type available in C++ that allows you to store data items of different types. A struct is used to represent a record. Suppose you want to track the status of books in a library, you might need to track the following attributes for each book: * Title * Author * Subject * Book ID In C++, the `struct` statement is used to define a structure (struct). A struct is a user-defined data type that allows you to combine data of different types. Similar to a `class`, a struct allows you to define member variables and member functions. To define a structure, you must use the **struct** statement. The struct statement defines a new data type with multiple members. The format of the struct statement is as follows: struct type_name{member_type1 member_name1; member_type2 member_name2; member_type3 member_name3; . . }object_names; **type_name** is the name of the structure type, **member_type1 member_name1** is a standard variable definition, such as **int i;** or **float f;** or any other valid variable definition. At the end of the structure definition, before the final semicolon, you can specify one or more structure variables, which is optional. Below is a declaration of a structure type **Books** with a variable **book**: struct Books{char title; char author; char subject; int book_id; }book; **Advantages of Structs:** * **Simple Data Encapsulation:** Suitable for encapsulating simple data of various types, often used for data storage. * **Lightweight:** Compared to `class`, struct syntax is more concise, suitable for small data objects. * **Object-Oriented Support:** Supports constructors, member functions, and access control, enabling object-oriented design. To access the members of a structure, we use the **member access operator (.)**. The member access operator is a period placed between the structure variable name and the structure member we want to access. The following example demonstrates the usage of a struct: ## Example #include#includeusing namespace std; struct Books{char title; char author; char subject; int book_id; }; int main(){Books Book1; Books Book2; strcpy(Book1.title, "C++ Tutorial"); strcpy(Book1.author, "Tutorial"); strcpy(Book1.subject, "Programming Language"); Book1.book_id = 12345; strcpy(Book2.title, "CSS Tutorial"); strcpy(Book2.author, "Tutorial"); strcpy(Book2.subject, "Frontend Technology"); Book2.book_id = 12346; cout<<"First book title : "<<Book1.title<<endl; cout<<"First book author : "<<Book1.author<<endl; cout<<"First book category : "<<Book1.subject<<endl; cout<<"First book ID : "<<Book1.book_id<<endl; cout<<"Second book title : "<<Book2.title<<endl; cout<<"Second book author : "<<Book2.author<<endl; cout<<"Second book category : "<<Book2.subject<<endl; cout<<"Second book ID : "<<Book2.book_id<<endl; return 0; } The example defines the structure type `Books` and its two variables `Book1` and `Book2`. When the above code is compiled and executed, it will produce the following result: First book title : C++ Tutorial First book author : TutorialFirst book category : Programming LanguageFirst book ID : 12345Second book title : CSS Tutorial Second book author : TutorialSecond book category : Frontend TechnologySecond book ID : 12346 You can pass a struct as a function parameter, similar to how you pass other types of variables or pointers. You can access struct variables using the method shown in the example above: ## Example #include#includeusing namespace std; void printBook(struct Books book); struct Books{char title; char author; char subject; int book_id; }; int main(){Books Book1; Books Book2; strcpy(Book1.title, "C++ Tutorial"); strcpy(Book1.author, "Tutorial"); strcpy(Book1.subject, "Programming Language"); Book1.book_id = 12345; strcpy(Book2.title, "CSS Tutorial"); strcpy(Book2.author, "Tutorial"); strcpy(Book2.subject, "Frontend Technology"); Book2.book_id = 12346; printBook(Book1); printBook(Book2); return 0; }void printBook(struct Books book){cout<<"book title : "<<book.title<<endl; cout<<"book author : "<<book.author<<endl; cout<<"book category : "<<book.subject<<endl; cout<<"book ID : "<<book.book_id<` operator, as shown below: struct_pointer->title; The above code accesses the `title` member of the `Book1` structure through `struct_pointer`. Let's rewrite the example above using a structure pointer. This will help you understand the concept of structure pointers: ## Example #include#includeusing namespace std; struct Books{string title; string author; string subject; int book_id; Books(string t, string a, string s, int id) : title(t), author(a), subject(s), book_id(id){}}; void printBookInfo(const Books* book){cout<<"Book Title: "<title<<endl; cout<<"Book Author: "<author<<endl; cout<<"Book Category: "<subject<<endl; cout<<"Book ID: "<book_id<` operator to access the member variables pointed to by the struct pointer. * **`main` Function:** * Creates two objects of type `Books`, `Book1` and `Book2`. * Uses the `&` operator to get the addresses of these two objects and assigns them to the pointers `ptrBook1` and `ptrBook2`. * When calling the `printBookInfo` function, it passes pointers to the `Books` objects. When the above code is compiled and executed, it will produce the following result: Book Title: C++ Tutorial Book Author: TutorialBook Category: Programming LanguageBook ID: 12345Book Title: CSS Tutorial Book Author: TutorialBook Category: Frontend TechnologyBook ID: 12346 Here is a simpler way to define a structure, where you can create an "alias" for the type. For example: typedef struct Books{ char title; char author; char subject; int book_id;}Books; Now, you can use `Books` directly to define variables of type `Books` without using the `struct` keyword. Here is an example: Books Book1, Book2; You can use the **`typedef`** keyword to define a non-structure type, as shown below: typedef long int *pint32; pint32 x, y, z; `x`, `y`, and `z` are all pointers to `long int`. * * * ## Differences Between Struct and Class In C++, `struct` and `class` are essentially very similar, with the only difference being the default access permissions: * `struct` has `public` members and inheritance by default. * `class` has `private` members and inheritance by default. You can think of `struct` as a simplified form of `class`, suitable for simple data encapsulation without too many complex features. ### Combining Structs with Functions You can initialize a struct using a constructor, and you can also pass a struct by reference to avoid unnecessary copying. ## Example struct Books { string title; string author; string subject; int book_id; // Constructor Books(string t, string a, string s, int id) : title(t), author(a), subject(s), book_id(id){} void printInfo()const{ cout<<"Book Title: "<< title << endl; cout<<"Book Author: "<< author << endl; cout<<"Book Category: "<< subject << endl; cout<<"Book ID: "<< book_id << endl; } }; void printBookByRef(const Books& book){ book.printInfo(); }
← Css CountersCpp Operators β†’