oops lab 4
oops lab 4
FA24B1-SE-081
LAB NO 4
TASK 1
Determine the logic to complete C++ program with the following features.
a. Declare a class Time with three fields hour, minute and second
b. Provide appropriate constructors to initialize the data members.
c. Provide separate get() an set()methods for each of the data
members.
d. Provide a method display() to print the time.
e. In the main(), using dynamic memory allocation, allocate memory for
two Time objects, currentTime and FligthTime.
f. From main(), call the function getHour() for both the Time objects
and provide the estima ted number of hours in the departure of your
flight. (No need to take into account minutes or seconds)
#include "stdafx.h"
#include<iostream>
using namespace std;
class Time
{
private:
int hrs;
int min;
int sec;
public:
Time():hrs(0),min(0),sec(0)
{}
void set(int h,int m,int s)
{
hrs=h;
min=m;
sec=s;
}
int get_hrs()
{
return hrs;
}
int get_min()
{
return min;
}
int get_sec()
{
return sec;
}
void display()
{
cout<<"Time is "<<hrs<<":"<<min<<":"<<sec<<endl;
}
};
ct->set(10,00,00);
ft->set(02,00,00);
Time display();
ct->get_hrs();
ft->get_hrs();
cout<<"Time ="<<ft->get_hrs()-ct->get_hrs()<<"hrs"<<endl;
system("pause");
return 0;
}
OUTPUT:
TASK # 03
Implement the program in exercise 1 by putting the class definition in a
header file, the class implementation in a cpp file and the main program
in a separate cpp file.
int main()
{
Time *ct,*ft;
ct=new Time;
ft=new Time;
ct->set_hrs(2);
ft->set_hrs(10);
cout<<"Hours="<<ft->get_hrs()-ct->get_hrs();
system("pause");
return o;
}
Time.h header file
class Time
{
private:
int hrs;
int min;
int sec;
public:
Time();
Time(int h,int m,int s);
int get_hrs();
int get_min();
int get_sec();
void set_hrs(int a);
void set_min(int b);
void set_sec(int c);
void display();
};
Time.cpp file
#include "stdafx.h"
#include<iostream>
#include"Time.h"
using namespace std;
Time::Time()
{
hrs=0;
min=0;
sec=0;
}
OUTPUT: