Professional Documents
Culture Documents
PROGRAMMING
FUNDAMENTALS
Experiment 10
C++ Structures
1
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Purpose:
Structures”. Students will run and check different programs consisting of implementation
of library functions.
Objectives:
1) Know the basic understanding of a C++ structure and their usability: how to use them
in a C++ program.
2) Run and check different but related programs consisting of C++ Structures.
3) Implementation of C++ program using the right Structure according to the required
logic.
2
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Structure in C++
Structure is commonly referred as user-defined data type. Structure is similar to an array
but the only difference between array and structure is that array is collection of similar
data type on the other hand structure is collection of different data type. A structure can
contain any data type including array and another structure as well. Each variable
declared inside structure is called member of structure.
Structure declaration
Declaration of structure must start with the keyword struct followed by the structure
name and structure's member variables are declared within braces.
struct structure-name
{
datatype var1;
datatype var2;
- - - - - - - - - -
- - - - - - - - - -
datatype varN;
};
struct Employee
{
int Id;
char Name[25];
int Age;
long Salary;
};
3
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
structure-name obj;
4
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
#include <iostream>
using namespace std;
struct Employee
{
string Id;
string Name;
int Age;
long Salary;
};
int main()
{
Employee E; //Statement 1
5
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Initialization of structure
Like normal variable structures can be initialized at the time of declaration. Initialization
of structure is almost similar to initializing array. The structure object is followed by equal
sign and the list of values enclosed in braces and each value is separated with comma.
#include <iostream>
using namespace std;
struct Employee
{
int Id;
char Name[25];
int Age;
long Salary;
};
int main()
{
Employee E = {2,"Suresh",35,35000};
6
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
struct structure2
{
- - - - - - - - - -
- - - - - - - - - -
structure1 obj;
};
//C++ Program to assign data to members of a structure variable and display it.
#include <iostream>
struct Address
{ char HouseNo[25];
char City[25];
char PinCode[25];
};
struct Employee
{ char Id[25];
char Name[25];
char Salary[25];
};
int main() {
int i;
7
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Employee E;
return 0;}
8
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
struct GradeRec
{
float percent;
char grade;
};
struct StudentRec
{
string lastName;
string firstName;
int age;
GradeRec courseGrade;
};
main()
{
StudentRec student;
cout << "Enter first name: ";
cin >> student.firstName;
cout << "Enter last name: ";
cin >> student.lastName;
cout << "Enter age: ";
cin >> student.age;
cout << "Enter overall percent: ";
cin >> student.courseGrade.percent;
if(student.courseGrade.percent >= 90)
{
student.courseGrade.grade = 'A';
}
else if(student.courseGrade.percent >= 75)
{
student.courseGrade.grade = 'B';
}
else
{
student.courseGrade.grade = 'F';
}
9
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
cout << "\n\nHello " << student.firstName << ' ' << student.lastName
<< ". How are you?\n";
cout << "\nCongratulations on reaching the age of " << student.age
<< ".\n";
cout << "Your overall percent score is "
<< student.courseGrade.percent << " for a grade of "
<< student.courseGrade.grade;
}
Task 01
Make a program that uses a structure named student and contains two members namely
Roll number and PF Score. Roll number should be initialized at the start of program. User
can enter PF Score of at most 3 students. Make use of switch statement, do while loop or
goto to repeatedly display the main menu.
10
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
11