You are on page 1of 5

Object Oriented Programming

Lab Manual (Lab 03)

Topic: Adding Constructor and Destructor in Classes,


separation of Interface and Implementation,
Constructor Overloading & Default Values
Course Instructor:
Lab Instructor: Ahmad Abduhu

Session: Fall 2018

School of Systems and Technology


UMT Lahore Pakistan

1
Objectives:

The purpose of this activity is to combine all the object oriented concepts studied till now. In respective lab we are
going to separate implementation and interface files. From now onwards we’ll be working in three files i.e.
Header.h, Driver.cpp and Implementation.cpp under a single project. Use of overloaded and default values for
constructors and destructors, their use along with their implementation.

Sample code for separating code into 3 different files

rectangle.h
/*
Class will be declared in this file
*/
class rectangle
{
private:
int height;
int width;
public:
rectangle();
~rectangle();
int area(void);
void initialize(int, int);
};

rectangle.cpp

/*
* Code for every function of class will be added in this file
* */
#include<iostream>
using namespace std;
#include "rectangle.h"
rectangle::rectangle()//Constructor is automatically called when object of class is created
{
//any code added here will be automatically executed when constructor is called
cout<<"Constructor automatically called"<<endl;
}
int rectangle::area(void)
{
return (height * width);
}
void rectangle::initialize(int initial_height, int initial_width)
{
height = initial_height;
width = initial_width;
}

rectangle::~rectangle()//Destructor is automatically called when object of class is destroyed


{
//any code added here will be automatically executed when estructor is called
cout<<"Destructor automatically called"<<endl;
}

2
main.cpp
/*
* We can use our class in this file
* In order to use our class we will have to include the header file of that class
* */
#include<iostream>
#include "rectangle.h"//Including header file
using namespace std;

int main()
{
rectangle wall;

wall.initialize(12,10);

cout<<"Area of the wall-->wall.area() = "<<wall.area()<< "\n\n";

return 0;

Lab Tasks
Make a class of School with Constructor and Destructor also add parameters rooms, staff, address and
function initialize() and print() write test program for it. Your code should be divided into 3 files, main.cpp
, school.h , school.cpp

3
Sample Code 02:
# include<iostream>

using namespace std;

class
Date{ private:
int Day; int Month; int Year;

public:
Date(int d = 13,int m = 10,int y = 1989)/*if any value is missing then the provided default value will be used for it*/
{
Day = d;
Month = m;
Year = y;
}
void DisplayDate()
{
cout<<"Date: "<<Day<<" : "<<Month<<" : "<<Year<<endl;
}
};

int main()
{
Date D1;
D1.DisplayDate();

Date D2(23);

D2.DisplayDate();

Date D3(23,03);

D3.DisplayDate();

Date D4(23,03,2013);

D4.DisplayDate();

return 0;

4
Lab Tasks
Task 1:
Create a class Time, with data members Minutes and seconds. . Overload the constructor in Time, and
Take two times from user i.e First Time for Check-In and the second time for Check-Out. Subtract these two times
to calculate the working hours per day. And display the result in Hours-Minutes.

Task 2:
Create a class Date, with data members Day, Month and year. . Overload the constructor in Date, and Take
two dates from user i.e First Date of starting work, and the second date of Ending work. Subtract these two dates to
calculate the working days per month. And display the result in days.

Task 3:

Calculate the salary of employee on the basis of attendance of employee which you have already compute in above
tasks. Formula to calculate the salary is given below:
Salary = (working-hours per day * total days per month)*1000;

Note: 2 days off per week. Per week total attendance must be 5 of days. And deduct salary on the basis of absentees.
1000 rupees per absent.

You might also like