0% found this document useful (0 votes)
48 views8 pages

Understanding Structures in C++ Programming

Struc

Uploaded by

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

Understanding Structures in C++ Programming

Struc

Uploaded by

aliabeed323
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

FACULTY OF ELECTONICS

Subject: CP II TECHNOLOGY SEMESTER 4th


Structure in C++

Structure : is the collection of variables of different types under a single name for better visualization of
problem. Arrays is also collection of data but arrays can hold data of only one type whereas structure can hold
data of one or more types.
Structure: is collection of related data items stored in one place and can be referenced by more
than one names. Usually these data items are different basic data types. therefore, the number of
bytes required to store them may also vary.
How to define a structure in C++ programming?
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[50];
int age;
float salary;
};
Here a structure person is defined which has three members: name, age and salary. When a
structure is created, no memory is allocated. The structure definition is only the blueprint for
the creating of variables. You can imagine it as a data type. When you define an integer as
below:
int x;
The int specifies that, variable x can hold integer element only. Similarly, structure definition
only specifies that, what property a structure variable holds when it is defined.
Declaring structure variables can be done in any off the following ways
1. struct student{
char id_num[5];
char name [10] ;
char gender;
}studno_1,studno_2;
2. struct {
char id_num[5];
char name [10] ;
char gender;
}studno_1,studno_2;
3. struct student{
char id_num[5];
char name [10] ;
char gender;
}; /notag
1 Eng:Alhadi
FACULTY OF ELECTONICS
Subject: CP II TECHNOLOGY SEMESTER 4th
Structure in C++
In the above three cases two structure variables studno_1,studno_2 are declared.
each structure variable has 4 elements that 3 character variables, and an integer variable.
In 1 and 2the structure variables are declared immediately after closing brace in the
structure whereas in 3 they are declared as student. Also in 2 there is no structure
tag this means we cannot declare structure variables of this type elsewhere in the
program instead we have to use structure variables studno_1,and studno_2 directly.
The most widely used may be no(1) and (3) where we put the declaration of the
struct in header file and use it anywhere.
Accessing the structure element :
1-Astructure element can be accessed and assigned a value by using the structure variable
name,the dot operator(.)and the element’s [Link] example the following statement:
Studno_1.name=”Jasmine”;
Example
#include<iostream>
using namespace std;
struct student{
char id_num[5];
char name[10];
char gender;
int age;
float mony;
};
int main()
{
struct student studno_1;
cout<<"Enter ID Num(5 max) ";
cin>>studno_1.id_num;
cout<<"Enter student name (10 max) ";
cin>>studno_1.name;
cout<<"Enter student gender ";
cin>>studno_1.gender;
cout<<"Enter student age ";
cin>>studno_1.age;
cout<<"Enter student givets ";
cin>>studno_1.mony;
cout<<endl;
system("pause");
return 0;
}

2 Eng:Alhadi
FACULTY OF ELECTONICS
Subject: CP II TECHNOLOGY SEMESTER 4th
Structure in C++
The structure pointer operator(->),consisting of a minus(-)sign and a greater than(>)sign
with no intervening spaces, accesses a structure member via a pointer to the structure.
The following example
#include <iostream>
#include <stdlib.h>
using namespace std;
struct card{
char *face;
char *suit;
};
main()
{
struct card p;
struct card *sptr; [Link]="Ace"; [Link]="spades";
sptr=&p;
cout<<" accessing structure element styles "<<endl;
cout<<"Style #1 -use [Link] "<<[Link]<<"of "<<[Link]<<endl;
cout<<"style #2 -use (*sptr).face "<<(*sptr).face<<" of "<<(*sptr).suit<<endl;
system("PAUSE");
return 0;
}

How to define a structure variable?

Once you declare a structure person as above. You can define a structure variable as:
person bill;
a structure variable bill is defined which is of type structure person. When structure variable is
defined, then only the required memory is allocated by the compiler. Considering you have
either 32-bit or 64-bit system, the memory of float is 4 bytes, memory of int is 4 bytes and
memory of char is 1 byte. Hence, 18 bytes of memory is allocated for structure variable bill
How to access members of a structure?
The members of structure variable is accessed using 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:
[Link] = 50;

3 Eng:Alhadi
FACULTY OF ELECTONICS
Subject: CP II TECHNOLOGY SEMESTER 4th
Structure in C++
Example 1:C++ Program to assign data to members of a structure variable and display it.

#include <iostream>
using namespace std;
struct person {
char name[50];
int age;
float salary;
};

int main() {
person p1;
cout << "Enter Full name: ";
[Link]([Link], 50);
cout << "Enter age: ";
cin >> [Link];
cout << "Enter salary: ";
cin >> [Link];
cout << "\nDisplaying Information." << endl;
cout << "Name: " << [Link] << endl;
cout <<"Age: " << [Link] << endl;
cout << "Salary: " << [Link];

return 0;
}

Array of structure :
Suppose you would like to to store the information of 100 [Link] would be tedious and
unproductive to create 100 different student array variables and work with them
[Link] would be easier to create an array of student structures.
For example to store and manipulate the information contained in 100 student records we use
the following statement:
Struct student{
char id[5];
char name[80];
char gender;
}stud[100];

Example of initializing all the student names to blanks and their information using loop
4 Eng:Alhadi
FACULTY OF ELECTONICS
Subject: CP II TECHNOLOGY SEMESTER 4th
Structure in C++
For(i=0;i<100;i++)
stud[i].name=” “;
stud[i].age=” “;

Example: complete program to store seven students information in records


#include <iostream>
#include <stdlib.h>
using namespace std;
struct student {
char id[6]; //sudent id number max 5 integer number
char name[50]; //student name max49 characters
char gender; //student geneder male or female
int age; //student age
};
int main()
{struct student stud[10]; //decaring array of 10 element of structure type
//and some of the element also arearrays
int i=0;
cout<<"keying in student data and then display"<<endl;
cout<<"Enter Sudent data";
for(i=0;i<2;i++)
{ //storing the data
cout<<"\n Id number(4 integer number) student # "<<i<<" :";
cin>>stud[i].id;
cout<< "First name student #"<<i<<" :";
cin>>stud[i].name;
cout<< "Gender (M or F) student #"<<i<<" :";
cin>>stud[i].gender;
cout<< "Age student #"<<i<<" :";
cin>>stud[i].age;
}
cout<<"-----------------------------------";
cout<<" Display The data ";
cout<<"------------------------------------";
for(i=0;i<2;i++)
{ //storing the data
cout<<" Id number(4 integer number) student # "<<i<<" :"<<stud[i].id<<endl;
cout<< "First name student #"<<i<<" :"<<stud[i].name<<endl;
cout<< "Gender (M or F) student #"<<i<<" :"<<stud[i].gender<<endl;

5 Eng:Alhadi
FACULTY OF ELECTONICS
Subject: CP II TECHNOLOGY SEMESTER 4th
Structure in C++
cout<< "Age student #"<<i<<" :"<<stud[i].age<<endl;
}

return 0;
}

Structure and function :


Individual structure elements or even an entire structure can be passed to functions as
arguments
Here complete example of function return structure ?
#include <iostream>
#include <stdlib.h>
using namespace std;
struct vegetable
{
char name[30];
float price;
};

int main()
{
// decalare 2 structure variables struct vegetable veg1,veg2
struct vegetable veg1,veg2;
//function prototype of type struct
struct vegetable addname();
//another function prototype
int list_func(vegetable);
//function call for user input
veg1=addname();
veg2=addname();
cout<<"vegetable for sale "<<"\n";
//function callfor data display
list_func(veg1);
list_func(veg2);
cout<<endl;

return 0;
}
// this function returns structure
struct vegetable addname()
{char tmp[20];

6 Eng:Alhadi
FACULTY OF ELECTONICS
Subject: CP II TECHNOLOGY SEMESTER 4th
Structure in C++
struct vegetable vege;
cout<<"Enter Name of vegetable "<<endl;
gets([Link]);
cout<<"Enter price vegetable "<<endl;
gets(tmp);
//converts astring to float
[Link]=atof(tmp);
return(vege);
}

// structure passed from main()


int list_func(vegetable list)
{
cout<<"vegetable name :"<<[Link]<<endl;
cout<<"vegetable price $ :"<<[Link]<<endl;
return 0;
}

Example: in this example declare movies struct has two tags mine and yours, inserts data to
structure and display the contents of structures using function
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct movies_t {
string title;
int year;
} mine, yours;

void printmovie (movies_t movie);

int main ()
{
string mystr;
[Link] = "2001 A Space Odyssey";
[Link] = 1968;
cout << "Enter title: ";
getline (cin,[Link]);
cout << "Enter year: ";
getline (cin,mystr);
stringstream(mystr) >> [Link];

7 Eng:Alhadi
FACULTY OF ELECTONICS
Subject: CP II TECHNOLOGY SEMESTER 4th
Structure in C++
cout << "My favorite movie is:\n ";
printmovie (mine);
cout << "And yours is:\n ";
printmovie (yours);
return 0;
}
void printmovie (movies_t movie)
{
cout << [Link];
cout << " (" << [Link] << ")\n";
}

Assigment :
Q1 write program to insert the structure of 5 student results that contents
stdname[20],subj[6],marks[1].and display student information?

Q1 write program to insert the structure of 5 employee salaries that contents


name[20],bdate[10], net_salary,Alghad_tax and display employee information using function
called it dislay_data?

8 Eng:Alhadi

You might also like