Oop Lab 1
Oop Lab 1
DEPARTMENT OF ELECTRICAL
AND COMPUTER
ENGINEERING
EXPERIMENT NO 1
LAB ASSESSMENT:
Data presentation
Experimental results
Conclusion
Date: Signature:
OOP Lab: 01
EXPERIMENT NO
1
Introduction to Structures
Objectives
Equipment required
Desktop/PC
Visual Studio/ Dev C++ on Windows
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;
};
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:
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;
int main()
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:
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 )
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?