You are on page 1of 11

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

PROGRAMMING
FUNDAMENTALS

Experiment 10
C++ Structures

CLO 2: Analyze advanced C++ programming concepts to examine problems


and derive its solution

1
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Purpose:

This experiment provides an introduction of different programs using different “C++

Structures”. Students will run and check different programs consisting of implementation

of library functions.

Objectives:

At the end of this experiment you will:

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.

Equipment and Components:

4) Microsoft Visual Studio 16

2
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

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.

Syntax for declaring structure

struct structure-name
{
datatype var1;
datatype var2;
- - - - - - - - - -
- - - - - - - - - -
datatype varN;
};

Example for declaring structure

struct Employee
{
int Id;
char Name[25];
int Age;
long Salary;
};
3
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Accessing the structure members


We have to create an object of structure to access its members. Object is a variable of type
structure. Structure members are accessed using the dot operator(.) between structure's
object and structure's member name.

Syntax for creating object

structure-name obj;

Example for creating object & accessing structure members

4
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

#include <iostream>
using namespace std;
struct Employee
{
string Id;
string Name;
int Age;
long Salary;
};

int main()
{

Employee E; //Statement 1

cout << "\nEnter Employee Age : ";


cin >> E.Age;

cout << "\nEnter Employee Salary : ";


cin >> E.Salary;

cout << "\nEnter Employee Id : ";


cin >> E.Id;

cout << "\nEnter Employee Name : ";


cin >> E.Name;

cout << "\n\nEmployee Id : " << E.Id;


cout << "\nEmployee Name : " << E.Name;
cout << "\nEmployee Age : " << E.Age;
cout << "\nEmployee Salary : " << E.Salary;
}

5
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

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.

Example for declaring & initializing structure at same time

#include <iostream>
using namespace std;
struct Employee
{
int Id;
char Name[25];
int Age;
long Salary;
};

int main()
{

Employee E = {2,"Suresh",35,35000};

cout << "\n\nEmployee Id : " << E.Id;


cout << "\nEmployee Name : " << E.Name;
cout << "\nEmployee Age : " << E.Age;
cout << "\nEmployee Salary : " << E.Salary;

Nested Structure in C++


When a structure contains another structure, it is called nested structure. For example, we have two
structures named Address and Employee. To make Address nested to Employee, we have to define
Address structure before and outside Employee structure and create an object of Address structure
inside Employee structure.

6
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Syntax for structure within structure or nested structure


struct structure1
{
- - - - - - - - - -
- - - - - - - - - -
};

struct structure2
{
- - - - - - - - - -
- - - - - - - - - -
structure1 obj;
};

Example for structure within structure or nested structure

//C++ Program to assign data to members of a structure variable and display it.

#include <iostream>

using namespace std;

struct Address

{ char HouseNo[25];

char City[25];

char PinCode[25];

};

struct Employee

{ char Id[25];

char Name[25];

char Salary[25];

Address Add; //Address Object is defined as member of Employee Structure so it can


access all other members of Address structure.

};

int main() {

int i;

7
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Employee E;

cout << "\n\tEnter Employee Id : ";

cin >> E.Id;

cout << "\n\tEnter Employee Name : ";

cin >> E.Name;

cout << "\n\tEnter Employee Salary : ";

cin >> E.Salary;

cout << "\n\tEnter Employee House No : ";

cin >> E.Add.HouseNo;

cout << "\n\tEnter Employee City : ";

cin >> E.Add.City;

cout << "\n\tEnter Employee PinCode: ";

cin >> E.Add.PinCode;

cout << "\nDetails of Employees";

cout << "\n\tEmployee Id : " << E.Id;

cout << "\n\tEmployee Name : " << E.Name;

cout << "\n\tEmployee Salary : " << E.Salary;

cout << "\n\tEmployee House No : " << E.Add.HouseNo;

cout << "\n\tEmployee City : " << E.Add.City;

cout << "\n\tEmployee PinCode: " << E.Add.PinCode;

return 0;}

8
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Example 2 Nested Structure


#include <iostream>
using namespace std;

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

SOFTWARE ENGINEERING DEPARTMENT

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.

Output of the code should look like this:

10
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

11

You might also like