OOP Lab 1
OOP Lab 1
OBJECTIVES:
Name: ….…………………………………………………………
SAP: ……………………………………………………………
EEDEPARTMENT
Structures in C++
In C++, a structure is a user-defined data type. The structure creates a data type for grouping items of
different data types under a single data type.
For example:
Suppose you need to store information about someone, their name, citizenship, and age. You can create
variables like name, citizenship, and age to store the data separately.
However, you may need to store information about many persons in the future. It means variables for
different individuals will be created. For example, name1, citizenship1, age1 etc. To avoid this, it's better to
create a structure.
Because structs are user-defined, we first have to tell the compiler what our struct looks like
before we can begin using it. To do this, we declare our struct using the struct keyword. Here
is an example of a struct declaration:
1 struct Employee
2{
3 int id;
4 int age;
5 double wage;
6 };
This tells the compiler that we are defining a struct named Employee. The Employee struct
contains 3 variables inside of it: an int named id, an int named age, and a double named
wage. These variables that are part of the struct are called members (or fields). Keep in
mind that Employee is just a declaration -- even though we are telling the compiler that the
struct will have member variables, no memory is allocated at this time. By convention, struct
names start with a capital letter to distinguish them from variable names.
Using structs
In order to use the Employee struct, we simply declare a variable of type Employee:
This defines a variable of type Employee named joe. As with normal variables, defining a
struct variable allocates memory for that variable.
EEDEPARTMENT
When we define a variable such as Employee joe, joe refers to the entire struct (which
contains the member variables). In order to access the individual members, we use
the member selection operator (which is a period). Here is an example of using the
member selection operator to initialize each member variable:
As with normal variables, struct member variables are not initialized, and will typically
contain junk. We must initialize them manually.
In the above example, it is very easy to tell which member variables belong to Joe and which
belong to Frank. This provides a much higher level of organization than individual variables
would. Furthermore, because Joe’s and Frank’s members have the same names, this
provides consistency across multiple variables of the same struct type.
Struct member variables act just like normal variables, so it is possible to do normal
operations on them:
EEDEPARTMENT
Initializing structs
struct Employee
{
int id;
int age;
double wage;
};
If the initializer list does not contain an initializer for some elements, those elements are
initialized to a default value (that generally corresponds to the zero state for that type). In
the above example, we see that frank.wage gets default initialized to 0.0 because we did not
specify an explicit initialization value for it.
A big advantage of using structs over individual variables is that we can pass the entire
struct to a function that needs to work with the members:
#include <iostream>
struct Employee
{
int id;
int age;
double wage;
};
EEDEPARTMENT
int main()
{
Employee joe { 14, 32, 24.15 };
Employee frank { 15, 28, 18.27 };
return 0;
}
In the above example, we pass an entire Employee struct to printInformation() (by value,
meaning the argument is copied into the parameter). This prevents us from having to pass
each variable individually. Furthermore, if we ever decide to add new members to our
Employee struct, we will not have to change the function declaration or function call!
EEDEPARTMENT
A function can also return a struct, which is one of the few ways to have a function
return multiple variables.
#include <iostream>
struct Point3d
{
double x;
double y;
double z;
};
Point3d getZeroPoint()
{
// We can create a variable and return the variable.
Point3d temp { 0.0, 0.0, 0.0 };
return temp;
}
Point3d getZeroPoint2()
{
// We can return directly. We already specified the type
// at the function declaration (Point3d), so we don't need
// it again here.
return { 0.0, 0.0, 0.0 };
}
int main()
{
Point3d zero;
zero. getZeroPoint() ;
return 0;
}
EEDEPARTMENT
Example Program
int main()
{
part part1; //define a structure variable
part1.modelnumber = 6244; //give values to structure members
part1.partnumber = 373;
part1.cost = 217.55F;
//display structure members
cout << “Model “ << part1.modelnumber;
cout << “, part “ << part1.partnumber;
cout << “, costs $” << part1.cost << endl;
return 0;
}
EEDEPARTMENT
Lab Task
Task 1
a. Name of student
b. Age of student
c. Current semester
d. CGPA
e. Tuition fee
f. Student’s Major
1. If the student's major is "Computer Science", print out the student's name and his/her gpa.
2. Calculate the percentage marks of each student.
Task 2
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.
Enter your area code, exchange, and number: 415 555 1212
My number is (212) 767-8900
Your number is (415) 555-1212
Task 3
Create a structure called employee that contains two members: an employee number (type
int) and the employee’s compensation (in rupees; type float). Ask the user to fill in this data
for three employees, store it in three variables of type struct employee, and then display the
information for each employee.