0% found this document useful (0 votes)
29 views

Lab 2 Oop Answer

This document discusses working with structures in C++. It provides 5 problem statements to create and use structures to store and manipulate different data types, including: a date with day, month and year members, a time with hours, minutes and seconds members, fractions with numerator and denominator members, and a volume with 3 distance structure members to calculate a room's volume. The problems demonstrate creating user-defined types with structures, storing data in structure variables, and performing calculations with structured data.

Uploaded by

Hassam Ali
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)
29 views

Lab 2 Oop Answer

This document discusses working with structures in C++. It provides 5 problem statements to create and use structures to store and manipulate different data types, including: a date with day, month and year members, a time with hours, minutes and seconds members, fractions with numerator and denominator members, and a volume with 3 distance structure members to calculate a room's volume. The problems demonstrate creating user-defined types with structures, storing data in structure variables, and performing calculations with structured data.

Uploaded by

Hassam Ali
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/ 5

Practical 2

Working with structures using C++

Objectives
• Getting familiar with structures.

• Understanding and creating user defined types with structures.

EXERCISE
Problem Statement 1
Is structure different from function? If yes, then how? Give explanation in your own words.

ANS:

YES.

 Structure is a collection of variables of different data types under a single name. It is similar to a
class in that, both holds a collection of data of different data types.
A function is a group of statements that together perform a task. 

Problem Statement 2
Create a structure a type date that contains three members: the day, the month, the year, all of type int.
Here the user enters a date in the format 10/9/2007, store it in a variable of type date, then retrieve the
value form the variable and print them out in the same format.
Problem Statement 3
Create a structure called time. Its three members, all of type int, should be called hours, minutes, and
seconds. Write a program that prompts the user to enter a time value in hours, minutes, and seconds.
This can be in 12:30:50 format. The program then stores the time in a variable of type time, and finally
prints out the total number of seconds.
Problem Statement 4
Write a program in C++ that stores the numerator and denominator of two numbers in fraction, after
adding them the result is also stored into fraction format. Use structure fraction to store these numbers
and their result. (Both numerator and denominator should be of type int).
Problem Statement 5
Create a structure called volume that uses three variables of type Distance (structure) to model the
volume of a room. Initialize a variable of type volume to specific dimensions, and then calculate the
volume it represents and print out the result.

You might also like