0% found this document useful (0 votes)
10 views

Ch04 Structures

The document discusses structures in C++ programming. It explains that structures allow grouping of simple variables into more complex entities, similar to how items are organized in real life. It provides an example structure to store parts inventory data, demonstrating how to define a structure, assign values to its members, and access those values. The document also shows how to initialize structure members and how structures can be used to store details about a person.

Uploaded by

inzamam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Ch04 Structures

The document discusses structures in C++ programming. It explains that structures allow grouping of simple variables into more complex entities, similar to how items are organized in real life. It provides an example structure to store parts inventory data, demonstrating how to define a structure, assign values to its members, and access those values. The document also shows how to initialize structure members and how structures can be used to store details about a person.

Uploaded by

inzamam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 12

Introduction to

Programming
Engr. Rashid Farid Chishti
[email protected]
Chapter 04: Structures
International Islamic University H-10, Islamabad, Pakistan
http://www.iiu.edu.pk
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.
A Simple Structure
 Let’s start off with a structure that contains three variables:
two integers and a floating-point number.
 This structure represents an item in a widget company’s parts
inventory.
 The structure is a kind of blueprint specifying what informat-
ion is necessary for a single part.
 The company makes several kinds of widgets, so the widget
model number is the first member of the structure.
 The number of the part itself is the next member, and the
final member is the part’s cost.
 The next program defines the structure part, defines a
structure variable of that type called part1, assigns values to
its members, and then displays these values.
Program to use a structure (1/2)
// uses parts inventory to demonstrate structures
#include <iostream>
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 <iostream>
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;
Initializing Structure Members (2/2)

part2 = part1; //assign first variable to second


// both variable must be of same data type

//display second variable


cout << "Model " << part2.modelnumber;
cout << ", part " << part2.partnumber;
cout << ", costs $" << part2.cost << endl;
system("PAUSE");
return 0;
}
Placing A Person’s Information (1/3)
#include <iostream>
#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;
Placing A Person’s Information (3/3)
cout << endl << "Here is Your Information"<< endl;
cout << "Name : " << sd1.name << endl;
cout << "Mobile : " << sd1.mobile << endl;
cout << "Age : " << sd1.age <<" years old \n";
cout << "Birthday: "
<< sd1.birthday.day <<"-"
<< sd1.birthday.month <<"-"
<< sd1.birthday.year << endl;
system( "PAUSE" );
return 0;
}

You might also like