0% found this document useful (0 votes)
21 views10 pages

Oop Lab 1

Uploaded by

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

Oop Lab 1

Uploaded by

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

AIR UNIVERSITY

DEPARTMENT OF ELECTRICAL
AND COMPUTER
ENGINEERING
EXPERIMENT NO 1

Lab Title: Introduction to Structures


Student Name: Reg. No: Objectiv

LAB ASSESSMENT:

Attributes Excelle Goo Avera Satisfactor Unsatisfactor


nt(5) d ge y (2) y (1)
(4) (3)
Ability to
Conduct
Experiment
Ability to
assimilate the
results
Effective use of
lab equipment
and follows
the lab safety rules

Total Marks: Obtained Marks:

LAB REPORT ASSESSMENT:

Excelle Goo Avera Satisfactor Unsatisfactor


Attributes nt(5) d ge y (2) y (1)
(4) (3)

Data presentation

Experimental results

Conclusion

Total Marks: Obtained Marks:

Date: Signature:
OOP Lab: 01

EXPERIMENT NO
1
Introduction to Structures

Objectives

 Familiarization with Structures in C++.


 Learning the difference between structural and functional programming

Equipment required
 Desktop/PC
 Visual Studio/ Dev C++ on Windows

Review: Functions in C++

We often come around situations where we need to store a group of data


whether of similar data types or different data types. We have seen Arrays
in C++ which are used to store set of data of similar data types at
contiguous memory locations.
Unlike Arrays, Structures in C++ are user-defined data types which are
used to store group of items of different data types.

What is a structure?
Structures in C++ are like containers that store variables of different data types
in them. Structures are user-defined data types, i.e., this data type is not in-built
in C++. Suppose the container name is Employee, i.e., we’re having a
structure named Employee. This container will store information about
Employees like name, age, and salary.
Defining a structure
A structure is created by using a keyword struct. Structures in C++ can
contain two types of members:
 Data Members: These are normal C++ variables. (a structure has
variables of different data types).
 Member Functions: These are normal C++ functions. Along with
variables, we can also include functions inside a structure
declaration.

struct Employee
{
string name;
int age;
float salary;

};

Important: Always end structure definition with a semicolon.


member functions inside structures in C++,

struct Employee
{
// Data Members
string name;
int age;
float salary;

// Member functions
void display( )
{
cout << “The employee age is: ” << age;
}
};

After defining a structure, you define its variable to store the information of an
employee.
You can define as many structure variables as you want. Suppose you want
to store information of 5 Employees, then you can make five structure
variables. The syntax for defining a structure variable is given below:

Int main (){


Employee
e1;
}

Access Members of a Structure?


The dot operator(.) is used in C++ to access structure members i.e the data
members and member functions. Suppose you want to access the age of an
employee, then you can write e1.age;
You can also assign a value using a dot operator like e1.age = 30;
OOP Lab: 01

Example 2
Defining a Structure in C/C++?
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 variablesinside curly braces) of that structure.
For example:

Here a structure named ‘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 datatype. When you define an integer as below:

int x;
The int specifies that, variable x can hold integer elements only. Similarly,
structure definition only specifies that, it can hold different data types that
have been defined in struct. Once you declare a structure “Person” as
above, it allocates memory accordingly.
You can define a structure variable in the main function by creating its
object as follow:

Person obj1;
Here, a structure variable obj1 is defined which is of type structure Person.
When structure variable is defined, only then the required memory is
allocated by the compiler. The members of structure variable are accessed
using a dot (.) operator. Suppose you want to access structure variable
obj1 and assign it a value
67.You can perform this task by using following code below in the
main function: obj1.weight = 67;

How to not access structure elements?

Structure members are accessed using dot (.)

operator only. struct Point


{
int x = 0; // COMPILER ERROR: cannot initialize members
here int y = 0; // COMPILER ERROR: cannot initialize
members here };
#include // Print members of myStructure
<iostream> cout<<myStructure.my_name
#include<string> <<end l;
using namespace cout <<
std; myStructure.my_rollnum << "\
// Create a structure variable called myStructure n";
struct }
myStructure {
string
my_name; int
my_rollnum;
};

int main()

Example 2: C++ Structure


OOP Lab: 01

Passing Structure to Function in C++


The value of structure variables can be passed to a function in similar way
as normal argument. Consider the example below:

Example: C++ Structure and Function

In this program, the structure variable p is passed to a function. Then the


members of structure p are displayed from this function. The return type of
displayData() is void and a single argument of type structure Person is
passed.

Example: Returning structure from function in C++


The output of this program is the same as the program above. In this
program, we have created two structure variables p and temp of type
Person under the main() function. The structure variable p is passed to
getData() function which takes input from the user which is then stored in
the temp variable. We then assign the value of temp to p. Then the
structure variable p is passed to displayData() function, which displays
the information.

One Structure in Multiple Variables

If you want to make multiple structures of same template, then you can use a
comma (,) to separate them.

struct {

string my_name;

int my_rollnum;
} myStruct1, myStruct2, myStruct3; // Multiple structure variables
separated

with commas

LAB TASKS
Q1. Find the area and perimeter of a triangle using structures and
function.Formula to find area and perimeter of triangle is given
below:

Create a struct Triangle. Declare necessary variables and functions


in the structure definition. There should be four functions
1. Set: To pass the values (a,b,c) to calculate area and perimeter.
2. Print: To print the calculated Area and Perimeter.
Call these functions in the main function to find the Area and Perimeter
3. Area: It takes values of different necessary variables and
calculates area
4. Perimeter: It takes values of different necessary
variables and calculates area
OOP Lab: 01

Q2. Create a struct Student where attributes associated with each


student are his name, registration number, father name, degree and
department. One can set and display the details of any student.

Home Tasks
Q1. Make a struct “Cylinder”. Choose appropriate attributes. The struct
should include input methods.
Class should be able to calculate:
Surface Area of Cylinder (formula A = 2πr2 + 2πrh =
2πr(r + h)) Volume of cylinder (formula V = πr2h )

Q2. Create a struct Electrical and Mechatronics. Consider both the


departments have thesame 2 subjects in this semester.
1. Computer Programming
2. Object Oriented Programming

Now your job is to take the names and marks of 3 students of


electrical department and 3students of mechatronics department.
Then find the average of CP and OOP in both the departments and
compare which department performed better.

Note: No marks will be given if you don’t use structures and functions

Questions:
Q1: What is the need of structures in C++?
Q2: Can we create an array of structures in code?
Q3: After defining a structure, does any memory get allocated to the structure?

You might also like