LAB 1 12 02 25 - Structures and Function Overloading 12022025 104546am
LAB 1 12 02 25 - Structures and Function Overloading 12022025 104546am
STRUCTURES
Introduction
A structure is a collection of related elements or data items. A structure is a collection of simple
variables. The data items in a structure are called the “members” of the structure. We’ve seen
variables of simple data types, such as float, char, and int.
Variables of such types represent one item of information: a height, an amount, a count, and so
on. But just as groceries are organized into bags, employees into departments, and words into
sentences, it’s often convenient to organize simple variables into more complex entities. The C ++
construction called the structure is one way to do this.
Objectives
· Structure declarations definitions
· Accessing structure members
· Nested structures
· Structures as objects and data types
Preparation
STRUCTURES
Specifying the names, types and number of data items within a structure called defining of the
structure. The syntax for defining a structure is:
struct st_nmae
{
type l;
type m;
};
struct: Keyword
#include <iostream.h>
};
int main()
{
part1.partnumber = 373;
part1.price = 217.55;
//display structure members
return 0;
Program # 1:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct movies_t {
string title;
int year;
} mine, yours;
int main ()
{
string mystr;
Structure Variables
A structure is a collection of related elements or data items. It is defined to declare its variables.
A variable of structure type represents the member of its structure. To declare a structure
variable, the structure is first defined, e.g:
struct part
int modelnum;
int partnum;
float price;
};
In the above example, modelnum, partnum and price are the members of structure car. The
variable part1 is declared as structure variable.
The dot operator is used to access the members of a structure. To access a member of a specific
structure, the structure variable name, the dot operator and then the member of the structure is
written. The syntax to access a member of a structure is:
struct_variable.struct_element
Example: part1.modelnum
In the given example part1 is structure variable and model number is structure element and dot
operator is used between part1 and modelnum.
Initialization of Structure Variables
The value into a structure variable can be assigned when it is declared. It is called initialization
of the structure variable. To initialize a structure variable, data is assigned to the members of the
structure; the data items are written in the same order in which these have been defined n the
structure.
struct part
int modelnum;
int partnum;
float price;
};
Function Overloading
If any class have multiple functions with same names but different parameters then they are said
to be overloaded. Function overloading allows you to use the same name for different functions,
to perform, either same or different functions in the same class.
Function overloading is usually used to enhance the readability of the program. If you have to
perform one single operation but with different number or types of arguments, then you can
simply overload the function.
2. sum (int x,int y=0);3. sum (int x,int y=0,int z); // This is incorrect 4. sum (int x,int
y=10,int z=10); // Correct
2. You can give any value a default value to argument, compatible with its data type.
LAB TASKS
LAB TASK
Write a program to implement the cube root with the help of function overloading.
LAB TASK
Write a program that uses function overloading for adding the two given integer and double
precision values separately.
LAB TASK
Write a program that compute absolute value that works for both integer and float. The output of
the program should be like:
In the above example, info structure is defined with two members. The structure data is defined
after the info and it contains two members and both are of structure type. These members’ s1 and
s2 within the structure are the nested structures.
Procedure:
Create a structure called time. Its three members, all type int, should be called hours, minutes,
and seconds. Write a program that prompts the user to enter a time value in hours, minutes,
seconds. This can be in 12:59:59 format, or each number can be entered at a separate prompt
(“Enter hours:”, and so forth). The program should then store the time in a variable of type struct
time, and finally print out the total number of seconds represented by this time value:
Procedure:
A phone number, such as (212) 767-8900, can be thought of as having three parts: the area code
(212), the exchange (767), and the number (8900). Write a program that uses a structure to store
these three parts of a phone number separately. Call the structure phone. Create two structure
variables of type phone. Initialize one, and have the user input a number for the other one. Then
display both numbers. The interchange might look like this:
Enter your area code, exchange, and number: 415 555 1212
Procedure:
Create a structure with the following data members:
Name
en_no
subjects
cr_hrs
q_marks
a_marks
m_marks
f_marks
grades
gpa
Programming Fundamentals 4 88 A
Compiler Construction 3 87 A
OOP 3 90 A
English 3 94 A
Physics 4 89 A
GPA: 4.0
Equipment:
Microsoft Visual C++ 10.0
Turbo C++
Analysis:
Structures are an important component of C++, since their syntax is the same as that of classes.
In fact, classes are (syntactically, at least) nothing more than structures that include functions.
Structures are typically used to group several data items together to form a single entity.
A structure declaration lists the variables that make up the structure. Definitions then set aside
memory for structure variables.
Structure variables are treated as indivisible units in some situations (such as setting one
structure variable equal to another), but in other situations their members are accessed
individually (often using the dot operator).
Conclusion:
Structures are a powerful and flexible way of grouping a diverse collection of data into a single
entity. In structures you can pack number of variables having different data type with single
structure called structure name.