0% found this document useful (0 votes)
3 views8 pages

OOP Lab 1

The document outlines a lab exercise for B.Sc. Electrical Engineering students at Riphah International University, focusing on C++ structures. It includes objectives, evaluation schemes, and detailed explanations of declaring, defining, and using structures in C++, along with practical tasks for students to complete. Additionally, it provides example programs and tasks related to struct usage, such as creating a StudentRecord structure and handling phone numbers.

Uploaded by

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

OOP Lab 1

The document outlines a lab exercise for B.Sc. Electrical Engineering students at Riphah International University, focusing on C++ structures. It includes objectives, evaluation schemes, and detailed explanations of declaring, defining, and using structures in C++, along with practical tasks for students to complete. Additionally, it provides example programs and tasks related to struct usage, such as creating a StudentRecord structure and handling phone numbers.

Uploaded by

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

Department of Electrical Engineering

Riphah College of Science & Technology


Riphah International University, Lahore

Program: B.Sc. Electrical engineering Semester: II


Subject CSL-212 Object Oriented Programming Date: ……………….

Lab1: Structures in C++

OBJECTIVES:

To introduce the C++ struct

(i) Declaring and defining Structures


(ii) Passing Structures to Functions
(iii) Array of Structures

Name: ….…………………………………………………………

SAP: ……………………………………………………………

No. Lab Evaluation Scheme Obtained


Marks
1 Understanding and Ability to Conduct Experiment. (0-5)
2 Implementation and Results (0-5)
Total
No. Lab Report Scheme Obtained
Marks
1 Report Content (0-5)
2 Code/Output Presentation and Conclusion (0-5)
Total

Remarks (if any): ………………………………….

Name & Signature of faculty: …………………………………


Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering

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.

Declaring and defining structs

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:

Employee joe; // struct Employee is capitalized, variable joe is not

This defines a variable of type Employee named joe. As with normal variables, defining a
struct variable allocates memory for that variable.

Object Oriented Programming 2nd Semester-EE RCST Lahore


Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering

EEDEPARTMENT

It is possible to define multiple variables of the same struct type:

Employee joe; // create an Employee struct for Joe


Employee frank; // create an Employee struct for Frank

Accessing struct members

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:

Employee joe; // create an Employee struct for Joe


joe.id = 14; // assign a value to member id within struct joe
joe.age = 32; // assign a value to member age within struct joe
joe.wage = 24.15; // assign a value to member wage within struct joe

Employee frank; // create an Employee struct for Frank


frank.id = 15; // assign a value to member id within struct frank
frank.age = 28; // assign a value to member age within struct frank
frank.wage = 18.27; // assign a value to member wage within struct frank

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.

Performing operations in Strut member variables

Struct member variables act just like normal variables, so it is possible to do normal
operations on them:

int totalAge{ joe.age + frank.age };

if (joe.wage > frank.wage)


cout << "Joe makes more than Frank\n";
else if (joe.wage < frank.wage)
cout << "Joe makes less than Frank\n";
else
cout << "Joe and Frank make the same amount\n";

Object Oriented Programming 2nd Semester-EE RCST Lahore


Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering

EEDEPARTMENT

// Frank got a promotion


frank.wage += 2.50;

// Today is Joe's birthday


++joe.age; // use pre-increment to increment Joe's age by 1

Initializing structs

Initializing structs by assigning values member by member is a little cumbersome, so C++


supports a faster way to initialize structs using an initializer list. This allows you to initialize
some or all the members of a struct at declaration time.

struct Employee
{
int id;
int age;
double wage;
};

Employee joe{ 1, 32, 60000.0 }; // joe.id = 1, joe.age = 32, joe.wage = 60000.0


Employee frank{ 2, 28 }; // frank.id = 2, frank.age = 28, frank.wage = 0.0

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.

Structs and functions

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;
};

Object Oriented Programming 2nd Semester-EE RCST Lahore


Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering

EEDEPARTMENT

void printInformation(Employee employee)


{
cout << "ID: " << employee.id << '\n';
cout << "Age: " << employee.age << '\n';
cout << "Wage: " << employee.wage << '\n';
}

int main()
{
Employee joe { 14, 32, 24.15 };
Employee frank { 15, 28, 18.27 };

// Print Joe's information


printInformation(joe);

cout << '\n';

// Print Frank's information


printInformation(frank);

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!

Write the output of the above program:

Object Oriented Programming 2nd Semester-EE RCST Lahore


Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering

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() ;

if (zero.x == 0.0 && zero.y == 0.0 && zero.z == 0.0)


cout << "The point is zero\n";
else
cout << "The point is not zero\n";

return 0;
}

Object Oriented Programming 2nd Semester-EE RCST Lahore


Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering

EEDEPARTMENT

Write the output of the above program

Example Program

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.cost = 217.55F;
//display structure members
cout << “Model “ << part1.modelnumber;
cout << “, part “ << part1.partnumber;
cout << “, costs $” << part1.cost << endl;
return 0;
}

Object Oriented Programming 2nd Semester-EE RCST Lahore


Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering

EEDEPARTMENT

Lab Task
Task 1

Create a structure name StudentRecord to store information about 5 students. The


information to be stored includes

a. Name of student
b. Age of student
c. Current semester
d. CGPA
e. Tuition fee
f. Student’s Major

After storing the information

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.

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

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.

Object Oriented Programming 2nd Semester-EE RCST Lahore

You might also like