Anda di halaman 1dari 8

Lab Journal 2

Object Oriented Programming


Structures
Name : Adil Ishfaq

Enrollment #: 01-235162-003
Class/Section: BS-IT(3A)

Objective
The objective of this lab is to assess the problem-solving capabilities of the students using
the concepts acquired in the course on Programming Fundamentals.

Task 1: Give answers to the following.

Write C++ code fragments for the following.

1. Declare a structure Time with fields hour, minute and second.


Struct Time {
int hour,minute,second;
};

2. Declare a variable of type Time.

Int time;

3. Declare a variable of type Time, a pointer to Time and an array of 10 Times. Assign
the address of the variable to the pointer.
int time;
int *ptime;
int a[30];
p=time;

U Object Oriented Programming 1


4. Using pointer variable set the value of hour to 5, minute to 10 and second to 50.
Int *ptime;
p=

5. Declare a structure Date. Declare another structure Project with fields: ID, startDate
and endDate. (Use nested structures).
Struct Date
{
};
Struct Project {
Int ID, StartDate,EndDate;
};

6. How would you call the function void printTime(Time *) ?


A=printTime(Time *a);

Task 2: Implement the given exercises.

Exercise 1:

U Object Oriented Programming 2


Write a complete C++ program with the following features.

i. Declare a structure Point with two integer members x and y .


ii. Define a function getInput() ,which accepts a Point by
reference. Get user input for a Point in this function.
iii. Define a function addPoints(), wh ich accepts two Points p1 and
p2. The function adds their respective members, and returns a Point
which is the sum of two. For example if one point is (2,3), the other
is (4,5), the function should return a Point (6,8).
iv. In the main(), declare two variables of type Point . Call the
function getInput() twice to get the values of these Points from
user. Add the two Points using the function addPoints() and
display the x and y values of the result returned by the function.

Code:
#include<iostream>

#include<string.h>

using namespace std;

struct point

int a,b;

};

void getinput(point &p);

point addpoints(point p1,point p2);

int main()

point x,z;

getinput(x);

point c=addpoints(x,z);

cout<<x.a<<" "<<x.b<<"-"<<endl;

system("pause");

void getinput(point &p)

U Object Oriented Programming 3


cout<<"enter a coordinate:"<<endl;

cin>>p.a;

cout<<"enter b coordinate:"<<endl;

cin>>p.b;

point addpoints(point p1,point p2)

point p3;

p3.a=p1.a+p2.a;

p3.b=p1.b+p2.b;

return p3;

Output:

Exercise 2:

i. Declare a structure Rectangle with two Points as its members, the top
left and the bottom right.
ii. Declare a variable o f type Rectangle and get user input for the two
points.
iii. Define a function computeArea() which accepts a Rectangle and
returns its area.
iv. Display the area of th e Rectangle in the main().

Code:
#include <iostream>

U Object Oriented Programming 4


#include <string>

using namespace std;

struct rectangle

int topleft;

int bottomright;

};

rectangle p1;

int computearea(rectangle z1);

int main()

cout<<"enter value of topleft:";

cin>>p1.topleft;

cout<<"enter value of bottomright:";

cin>>p1.bottomright;

int x=computearea(p1);

cout <<"area is"<<x;

int computearea(rectangle z1)

int area = z1.topleft*z1.bottomright;

return area;

Output:

U Object Oriented Programming 5


Exercise 3:

i. Declare a structure Time with fields hours and minutes.


ii. Declare ano ther structure Flight with fields to store flight ID,
arrival time and departure time.
iii. Define a function input(Flight *) which allows users enter data
for a flight.
iv. Define ano ther function display(Flight *) which displays the
data for a flight.
v. In the main, declare a variable of type Flight and call the input()
and display() functions.

Code:
#include <iostream>

#include <string>

using namespace std;

struct time

int hours;

int minutes;

};

struct flight

int flight_ID;

float arrrival_time;

float departure_time;

};

flight readinput(flight& f1)

cout<<"enter flight ID:"<<endl;

cin>>f1.flight_ID;

cout<<"enter arrival time:"<<endl;

cin>>f1.arrival_time;

U Object Oriented Programming 6


cout<<"enter departure time:"<<endl;

cin>>f1.departure_time;

return f1;

flight displayinput(flight& f1)

flight f;

readinput(f);

cout<<"flight ID:"<<f.flight_ID<<endl;

cout<<"arrival time:"f.arrival_time<<endl;

cout<<"departuretimr:"<<f.departure_time<<endl;

Output:

Implement the given exercises and get them checked by the instructor. Upload the
complete Word file on Piazza. If you are unable to complete the tasks in the lab session,
include those tasks in the journal and deposit this journal along with your programs (printed
or handwritten) before the start of the next lab session.

Submitted to: Ms. Umarah Qaseem


Task 1:
Task 2:
Task 3:
Date:
Total marks:

U Object Oriented Programming 7


U Object Oriented Programming 8

Anda mungkin juga menyukai