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

Quiz 1 - Solution

The document contains code for a date class with private data members for day, month and year. It implements: 1) A default constructor to initialize a date object to 1st January 2000. 2) Setter functions to allow changing the day, month and year values by passing new values while ensuring they are within valid ranges. 3) A private advance() method to increment the date to the next day, rolling over days into months and months into years as needed. 4) A call() method in the main function to test advancing the date using the advance() method.

Uploaded by

Ahmed Ashraf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Quiz 1 - Solution

The document contains code for a date class with private data members for day, month and year. It implements: 1) A default constructor to initialize a date object to 1st January 2000. 2) Setter functions to allow changing the day, month and year values by passing new values while ensuring they are within valid ranges. 3) A private advance() method to increment the date to the next day, rolling over days into months and months into years as needed. 4) A call() method in the main function to test advancing the date using the advance() method.

Uploaded by

Ahmed Ashraf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

National University of Computer & Emerging Sciences, Karachi

Spring-2020 (CS-Department)

Object-oriented Programming (OOP)


Quiz # 1

Date: Tuesday, February 04, 2019


Q2. Consider the following class definition: (10 points)

public class date {


private:
int day; // range from 1 to 31
int month; // range from 1 to 12
int year; // ranging from 2000 onwards
void advance(); // move to next day
};
a) Implement a constructor that initializes new objects to set 1st January 2000 as a default date.

date()

day = 1;

month = 1;

year = 2000;

}
b) Implement a setter function to adjust the date.

void setDay(int newDay)


{
if(newDay>0 && newDay <32)
{
day = newDay;
}
}

void setMonth(int newMonth)


{
if(newMonth>0 && newMonth <13)
{
month = newMonth;
}
}

void setYear(int newYear)


{
if(newYear>1999)
{
year = newYear;
}
}

c) Implement a private method advance(), which moves date to the next day, ensuring that all data
members are updated appropriately.

void advance()
{

const int daysInMonth[12]={31,28,31,30,31,30,31,31,30, 31,30,31};

day++;

if(day>daysInMonth[month])

day = 1;

month++;

if(month>12)

month = 1;

year++;

d) Call advance() from main() to update date.

void call()

advance();

int main()

date d1;

d1.call();

Q3. Short answer questions: (one or two sentences) (5 points)

a) Explain why it is sometimes useful to have accessors & mutators in a class?

To access, set and get the private data members of the class.
b) In the presence of a parameterized constructor, it is not necessary to have a setter function. Do you
agree with this statement? Justify your answer.

No, Parameterized or any other constructor can only be called when the object is created. If we want to
change/update the private data members after the object is created we necessarily need a setter
function.

You might also like