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

LAB 1 12 02 25 - Structures and Function Overloading 12022025 104546am

The document provides an introduction to structures in C++, explaining their definition, member access, and initialization. It includes examples of structure declarations, accessing members using the dot operator, and function overloading. Additionally, it outlines various lab tasks related to structures, such as calculating total seconds and handling phone number structures.

Uploaded by

hamzamanzoor0055
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)
15 views10 pages

LAB 1 12 02 25 - Structures and Function Overloading 12022025 104546am

The document provides an introduction to structures in C++, explaining their definition, member access, and initialization. It includes examples of structure declarations, accessing members using the dot operator, and function overloading. Additionally, it outlines various lab tasks related to structures, such as calculating total seconds and handling phone number structures.

Uploaded by

hamzamanzoor0055
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

Lab # 1

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

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.

Defining the Structure

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

st_name: Structure name. Also called structure tag.

type: Data type of structure

l,m: Members of the structure

EXAMPLE (A simple structure)

#include <iostream.h>

struct part //declare a structure

int modelnumber; //ID number of widget

int partnumber; //ID number of widget part

float cost; //cost of part

};

int main()
{

part part1; //define a structure variable

part1.modelnumber = 6244; //give values to structure members

part1.partnumber = 373;

part1.price = 217.55;
//display structure members

cout << “Model ” << part1.modelnumber;

cout << “, part ” << part1.partnumber;

cout << “, price $” << part1.price << endl;

return 0;

Program # 1:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

struct movies_t {
string title;
int year;
} mine, yours;

void printmovie (movies_t movie);

int main ()
{
string mystr;

mine.title = "2001 A Space Odyssey";


mine.year = 1968;

cout << "Enter title: ";


getline (cin,yours.title);
cout << "Enter year: ";
getline (cin,mystr);
stringstream(mystr) >> yours.year;

cout << "My favorite movie is:\n ";


printmovie (mine);
cout << "And yours is:\n ";
printmovie (yours);
return 0;
}

void printmovie (movies_t movie)


{
cout << movie.title;
cout << " (" << movie.year << ")\n";
}

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;

};

part part1 //define a structure variable

In the above example, modelnum, partnum and price are the members of structure car. The
variable part1 is declared as structure variable.

Accessing Structure Members

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;

};

part part1 = {624, 300. 100000};

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.

Ways to overload a function

1. By changing number of Arguments.


2. By having different types of argument.
Number of Arguments different
In this type of function overloading we define two functions with same names but different
number of parameters of the same type. For example, in the below mentioned program we have
made two sum() functions to return sum of two and three integers.
Here sum() function is overloaded, to have two and three arguments. Which sum() function will
be called, depends on the number of arguments.

int sum (int x, int y)


{
cout<<x+y;
}
int sum (int x, int y, int z)
{
cout<<x+y+z;
}
Int main()

sum (10,20); //sum() with 2 parameters

sum (10,20,30); //sum() with 3 parameters

Different Data type of Arguments


In this type of overloading we define two or more functions with same name and same number
of parameters, but the type of parameter is different. For example in this program, we have two
sum() function, first one gets two integer arguments and second one gets two double arguments.
Default Arguments
When we mention a default value for a parameter while declaring the function, it is said to be as
default argument. In this case, even if we make a call to the function without passing any value
for that parameter, the function will take the default value specified.
Here we have provided a default value for y, during function definition.
OUTPUT:
First two function calls will produce the exact same value.
For the third function call, y will take 10 as value and output will become 20.
By setting default argument, we are also overloading the function. Default arguments also allow
you to use the same function in different situations just like function overloading.

Rules for using Default Arguments


Only the last argument must be given default value. You cannot have a default argument
followed by non-default argument.
1. If you default an argument, then you will have to default all the subsequent arguments after
that.

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.

LAB TASK : Calculate total number of seconds.

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:

long totalsecs = t1.hours*3600 + t1.minutes*60 + t1.seconds

LAB TASK Interchange of phone structures.

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

My number is (212) 767-8900

Your number is (415) 555-1212


LAB TASK Calculation of result.

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

Subjects credit hours Total Marks Grade

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.

You might also like