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

oops lab 4

The document outlines a series of tasks related to Object-Oriented Programming in C++. It includes instructions for creating a Distance class, implementing a Time class with constructors and methods, and organizing the code into header and implementation files. The document also provides example code snippets and expected outputs for the tasks.

Uploaded by

hashirhaider78
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)
8 views

oops lab 4

The document outlines a series of tasks related to Object-Oriented Programming in C++. It includes instructions for creating a Distance class, implementing a Time class with constructors and methods, and organizing the code into header and implementation files. The document also provides example code snippets and expected outputs for the tasks.

Uploaded by

hashirhaider78
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/ 7

NAME : EMAN HUSSAIN

FA24B1-SE-081

OBJECT ORRIENTED PROGRAMMING LAB

SUBMMITED TO SIR REHAN

LAB NO 4
TASK 1

1.Declare a class Distance with fields feet and inches (integers).

2. Declare an object of type Distance and a pointer to Distance. Assign


the address of the object to the pointer.
3. Write C++ statements to call function display() of class Distance using:
object , pointer to object.

4. Declare a pointer to Distance and allocate memory for an object using


the keyword new.

5.Suppose the Distance class has a constructor Distance(int f, int i).


Allocate memory for an object of class Distance using the keyword new
in such a way that this constructor is invoked.
TASK # 02

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

};

int _tmain(int argc, _TCHAR* argv[])


{
Time T,*ct,*ft;
ct=new Time;
ft=new Time;

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.

OOPs lab 4.cpp


#include "stdafx.h"
#include<iostream>
#include"Time.h"
using namespace std;

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

Time()::Time(int h,int m,int s)


{
hrs=h;
min=m;
sec=s;
}
int Time::get_hrs()
{
return hrs;
}
int Time::get_min()
{
return min;
}
int Time::get_sec()
{
return sec;
}
void Time::set_hrs(int a)
{
hrs=a;
}
void Time::set_min(int b)
{
min=b;
}
void Time::set_sec(int c)
{
sec=c;
}
void Time::display()
{
cout<<hrs<<":"<<min<<":"<<sec<<endl;
}

OUTPUT:

You might also like