Document Details

ArdentPlateau5380

Uploaded by ArdentPlateau5380

2023

Tags

C++ structures programming structures data structures computer science

Summary

This document explains C++ structures, which are user-defined data types that group elements of potentially different data types. It provides examples on how to create and interact with structures using the struct keyword, emphasizing member declaration and access.

Full Transcript

MODULE 4 STRUCTURES LESSON #1 Structures Upon completion of this subtopic, you will be able to: Understand what is a structure Learn how to declare and use structures in a program A structure is a group of elements which are of different type. Each of the elements is identified by its...

MODULE 4 STRUCTURES LESSON #1 Structures Upon completion of this subtopic, you will be able to: Understand what is a structure Learn how to declare and use structures in a program A structure is a group of elements which are of different type. Each of the elements is identified by its own identifier and they are called member. A structure can be thought of as an object without any member functions. The important property of structures is that the data in a structure can be a collection of data items of diverse types. A structure variable can hold values just like any other variable can. A structure value is a collection of smaller values called member values. The struct keyword defines a structure type followed by an identifier (name of the structure). Then inside the curly braces, you can declare one or more members (declare variables inside curly braces) of that structure For example: struct Person { char name; int age; float salary; }; Here a structure person is defined which has three members: name, age and salary. Once you declare a structure person as above. You can define a structure variable as: Person bill; Here, a structure variable bill is defined which is of type structure Person. When structure variable is defined, only then the required memory is allocated by the compiler. The members of structure variable is accessed using a dot (.) operator. Suppose, you want to access age of structure variable bill and assign it 50 to it. You can perform this task by using following code below: bill.age = 50; #include int main() using namespace std; { struct Rectangle struct Rectangle r; { r.width=8; int width, height; r.height=5; cout

Use Quizgecko on...
Browser
Browser