You are on page 1of 6

Bahria University, Islamabad Campus

Department of Computer Science


Lab Mid Term Examination
Class/Section: BS (IT)-2B
(Spring 2024 Semester)

Course: Object Oriented Programming Lab (B) Date: 2/April/2024


Course Code: CSC-210 Group:2

Faculty’s Name: Ms. Nabia Khalid Max Marks: 20

Time Allowed: 120 Minutes Total Pages: 2 (including this)

INSTRUCTIONS:
I. All questions are compulsory.
II. There are three questions with parts in total.
III. Paper is to be solved on compiler.
IV. The students are not allowed any helping material (books, tables, formulas, etc.).
V. Anyone caught using Chabot or any other online help will be awarded with F grade in lab plus theory

Student’s Name: MUHAMMAD HUZAIFA Enroll No: 01-135232-058


(USE CAPITAL LETTERS) (FULL ENROLLMENT)

Total Questions Q1 Q2 Q3 Total

Marks Assigned 10 4 6 20

Marks Obtained

Page1|2
Question 1(CLO-2)-------------------------------------------------------------------------------------------------------Marks [10]
Create a class Test. Test has 4 data members namely, dm1,dm2, dm3, dm4 of different data types. dm1
belongs to Date class, you can choose any data type for other data members. dm1 is a static data
member and is only changed by calling a function dm1Modify(). Initial value of dm1 is set to your
birthdate. dm1Modify() always shifts the date to next date. dm1Modify() is called every time a
constructor or destructor is called for the class. Create appropriate set and get methods for each data
member. Using the concepts studied in course, enforce the constraint on functions that are not supposed
to change data members must not do so even by mistake.

Solution should be divided into following parts.


Part a: UML diagram for given problem
Part b: Complete implementation of class in C++
Part c: Driver Program (main function) to exhibit functionality of your class.
Code:
#include <iostream>
#include <string>
#include <ctime>

using namespace std;

class date {
private:
int day;
int month;
int year;
public:
date(int d, int m, int y)
{
day = d;
month = m;
year = y;
}
void nextdate()
{
day++;
}
void display()
{
cout << day << "/" << month << "/" << year << endl;
}
};

class Test {
private:
static date dm1;
int dm2;
float dm3;
string dm4;
void enforceConstraints()
{};

public:
Test(int d,float f,string s)
{
Page2|2
dm2 = d;
dm3 = f;
dm4 = s;
dm1.nextdate();
enforceConstraints();
}

Test(Test& other)
{
dm2 = other.dm2;
dm3 = other.dm3;
dm4 = other.dm4;
dm1.nextdate();
enforceConstraints();
}
~Test()
{
dm1.nextdate();
enforceConstraints();
}
void setdm2(int d2)
{
dm2 = d2;
enforceConstraints();
}
int getdm2()
{
return dm2;
}

void setdm3(float d3)


{
dm3 = d3;
enforceConstraints();
}

float getdm3()
{
return dm3;
}
void setdm4(string d4)
{
dm4 = d4;
enforceConstraints();
}
string getdm4()
{
return dm4;
}
static date getdm1()
{
return dm1;
}
static void dm1modify()
{
dm1.nextdate();
}

Page3|2
};
int main()
{
date d(20,8,2003);
Test dm1modify();
Test obj(6, 6.2, "Hello today");
cout << "before modified value is " << endl;
cout << "dm1 is: "; obj.getDm1().display();
cout << "dm2 is: " << obj.getDm2() << endl;
cout << "dm3 is: " << obj.getDm3() << endl;
cout << "dm4 is: " << obj.getDm4() << endl;
obj.setdm2(10);
obj.setdm3(7.32);
obj.setdm4("today is mid-term paper");
cout << "after modified value is" << endl;
cout << "Modified dm1 is: ";
Test ::getdm1().display();
cout << "Modified dm2 is: " << obj.getdm2() << endl;
cout << "Modified dm3 is: " << obj.getdm3() << endl;
cout << "Modified dm4 is: " << obj.getdm4() << endl;
}
Output:

Question 2 (CLO-1)------------------------------------------------------------------------------------------------------Marks [4]


Write the class definition for a Time class that contains three private integer data members: hours,
minutes, and seconds. Create a static member to hold a colon. Create two public member functions,
setTime()and showTime(). You will use the static colon in the showTime() function. The
setTime()function accepts three integer arguments and passes them on to three private functions,
sethour(), setminutes(), and setseconds(). If an hour is greater than 24, then set it to 00 . If a minute is
greater than 60, then set it to 00. Write a main() function that instantiates an array of five objects of the
Time class and tests the member functions.
Code:
#include <iostream>
using namespace std;
class Time {
private:
int hours;
int minutes;
int seconds;
static char colon;
void setHour(int h);
void setMinutes(int m);
void setSeconds(int s);

Page4|2
public:
Time(int h = 0, int m = 0, int s = 0);
void setTime(int h, int m, int s);
void showTime();
};
char Time::colon = ':';
Time::Time(int h, int m, int s)
{
setTime(h, m, s);
}
void Time::setTime(int h, int m, int s)
{
setHour(h);
setMinutes(m);
setSeconds(s);
}
void Time::setHour(int h)
{
if (h >= 0 && h < 24)
hours = h;
else
hours=0;
}
void Time::setMinutes(int m)
{
if (m >= 0 && m < 60)
minutes = m;
else
minutes=0;
}
void Time::setSeconds(int s)
{
if (s >= 0 && s < 60)
seconds = s;
else
seconds=0;
}
void Time::showTime()
{
cout << hours << colon << minutes << colon << seconds << endl;
}
int main()
{
Time timeArray[5];
timeArray[0].setTime(10, 15, 45);
timeArray[1].setTime(18, 15, 20);
timeArray[2].setTime(21, 52, 52);
timeArray[3].setTime(12, 5, 23);
timeArray[4].setTime(5, 8, 45);
for (int i = 0; i < 5; ++i)
{
cout << "Time is " << i + 1 << ": ";
timeArray[i].showTime();
}
}

Output:

Page5|2
Question 3 (CLO-1) ----------------------------------------------------------------------------------------------------- Marks [6]
Design a CollegeDepartment class. Include fields for the department name (for example “Mathematics”), the
department chair’s name (for example, “Lewis”), an integer that holds the number of classes offered by a
department (for example, 10), and a string pointer that points to the individual courses offered by the
department (for example, “MAT101”). Create two constructors. The default constructor assigns appropriate
default values to the fields. The other constructor accepts parameters for the department name, department
chair’s name, and number of courses and assigns them to their respective fields, and also prompts the user for
the names of all the courses offered.
b. Overload an insertion operator for the class.
c. Overload > and < operators that compare CollegeDepartments based on number of courses offered.
d. Overload an = operator to assign one CollegeDepartment to another.

Page6|2

You might also like