Programming Fundamentals Chapter 04: Structures, Slides of Programming Languages

The document introduces the concept of structures in C++ programming. It explains how structures can be used to organize simple variables into more complex entities. examples of how to use structures to display structure members and place a person's information. useful for students learning C++ programming and want to understand how to use structures to organize data.

Typology: Slides

2021/2022

Available from 11/16/2022

razaroghani
razaroghani 🇵🇰

4.5

(4)

151 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Programming Fundamentals
Chapter 04: Structures
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Programming Fundamentals Chapter 04: Structures and more Slides Programming Languages in PDF only on Docsity!

Programming Fundamentals

Chapter 04: Structures

Structures

❑ We have seen variables of simple data types,

such as float, char, and int.

❑ Variables of such types represent one item of

information: a height, an amount, a count, and

so on.

❑ But just as groceries are organized into bags,

employees into departments, and words into

sentences, it’s often convenient to organize

simple variables into more complex entities.

❑ The C++ construction called the structure is

one way to do this.

❑ A structure is a collection of simple variab-

les (can be of different types).

❑ The data items in a structure are called the

members of the structure.

Program to use a structure (1/2) // uses parts inventory to demonstrate structures #include using namespace std; struct part{ // declare a structure int modelnumber; // ID number of widget int partnumber; // ID number of widget part float cost; // cost of part }; int main(){ part part1; // define a structure variable // give values to structure members part1.modelnumber = 6244; part1.partnumber = 373; part1.cost = 217.55F;

Program to use a structure (2/2) // display structure members cout << "Model " << part1.modelnumber; cout << ", part " << part1.partnumber; cout << ", costs $" << part1.cost << endl; system("PAUSE"); return 0; }

Initializing Structure Members (1/2) // uses parts inventory to demonstrate structures #include using namespace std; struct part{ // declare a structure int modelnumber; // ID number of widget int partnumber; // ID number of widget part float cost; // cost of part }; int main(){ // initialize variable part part1 = { 6244, 373, 217.55F }; part part2; // define variable // display first variable cout << "Model " << part1.modelnumber; cout << ", part " << part1.partnumber; cout << ", costs $" << part1.cost << endl;

Placing A Person’s Information (1/3) #include #include <stdio.h> #include <stdlib.h> using namespace std; struct date{ int month; int day; int year; }; struct student{ char name[80]; char mobile[15]; int age; char email[30]; date birthday; };

Placing A Person’s Information (2/3) int main(){ student sd1; cout << "Please Enter Your Information " << endl; cout << "Name: "; gets(sd1.name); cout << "mobile Number: "; gets(sd1.mobile); cout << "Your Age: "; cin >> sd1.age; cout << "Please Enter Your Birthday \n"; cout << "Year: "; cin >> sd1.birthday.year; cout << "month(1-12): "; cin >> sd1.birthday.month; cout << "Day(1-31): "; cin >> sd1.birthday.day;