0% found this document useful (0 votes)
87 views7 pages

Object Oriented Programming (LAB) : Course Tittle

The document provides instructions for two assignments - the first asks students to create a Fraction class to perform arithmetic on fractions, while the second modifies an existing Date class to perform error checking and add a nextDay method to increment the date. It includes the header and main files for solutions to both assignments testing the Fraction and Date classes.

Uploaded by

HUZAIFA SAEED
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views7 pages

Object Oriented Programming (LAB) : Course Tittle

The document provides instructions for two assignments - the first asks students to create a Fraction class to perform arithmetic on fractions, while the second modifies an existing Date class to perform error checking and add a nextDay method to increment the date. It includes the header and main files for solutions to both assignments testing the Fraction and Date classes.

Uploaded by

HUZAIFA SAEED
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Course Tittle:

Object Oriented Programming (LAB)

Course Code:

CC 1022L

Assignment Topic : “Assignment 5”


Due Date for Submission : “Thursday, 08-04-2021”
Submitted to : “Muhammad Imran Khalil”
Assistant Professor
School of Systems And Technology (SST)

Name : Huzaifa Saeed


Student Id : F2020266522
Section : V2
BSCS (Computer Sciences)
Task 1:
Create a class called Fraction for performing arithmetic with fractions. Write a
program to test
your class. Use integer variables to represent the private data of the class—then numerator
and
the denominator. For example the, fraction 2
4
would be stored in the object as 2 in numerator and
4 in denominator. Write setter/getter function to display values of rational number. Also
provide
default constructor for class.
Write main function to test your class.

Sollution:

Header File:

#include<iostream>
#include<cmath>
using namespace std;

class fraction {
private:
int numerator, denominator;
public:
fraction();
void setnum(int);
void setden(int);
int getden();
int getnum();
void display();
};
fraction::fraction(){
numerator = 1;
denominator = 1;
}
void fraction::setnum(int n){
numerator = n;
}
void fraction::setden(int d){
denominator = d;
}
int fraction::getnum(){
return numerator;
}
int fraction::getden(){
return denominator;
}
void fraction::display(){
float result = numerator / denominator;
cout <<"Answer to your fraction is : " << numerator << "/" << denominator << "="
<< result << endl;
}

Main File:

#include"task1.h"

int main()
{
fraction f1, f2;
f1.setden(2); f1.setnum(4);
f1.display();
f2.display();
return 0;
}
Task 2:
Consider the following definition of Date class
class Date
{
private:
int month;
int day;
int year;
public:
Date( int , int, int );
void print();
}; // end class Date
Date::Date( int m, int d, int y )
{
month = m;
day = d;
year = y;
} // end constructor Date
// print Date in the format mm/dd/yyyy
void Date::print()
{
cout << month << '/' << day << '/' << year;
} // end function print
Modify the given Date class to perform error checking on the initializer values for data
members
month, day and year. Also, provide a member function nextDay to increment the day by one.
Write a program that tests function nextDay in a loop that prints the date during each iteration
to
illustrate that nextDay works correctly. Be sure to test the Following cases in main function
a) Incrementing into the next month.
b) Incrementing into the next year.

Solution:

Header File:
#include <iostream>
using namespace std;

class Date
{
private:
int month;
int day;
int year;
public:
Date(int, int, int);
void print();
int nextday();
void nextmonth();
void nextyear();
}; // end class Date
Date::Date(int m, int d, int y)
{
month = m;
day = d;
year = y;
} // end constructor Date
// print Date in the format mm/dd/yyyy
void Date::print()
{
cout <<endl<< month << '/' << day << '/' << year << endl;
}
int Date::nextday(){
if (day >= 31){
cout << "\ncant increase the day from 31 \n";
}
else{
day = day + 1;
}
return day;
}
void Date::nextmonth(){
if (month >= 12)
{
cout << "\ncant increase the month from 12 \n";
}
else{
month = month + 1;
}
// cout << "next month is : " << month << endl;
}
void Date::nextyear(){
year = year + 1;
//cout << "next year is : " << year << endl;
}

Main File:

#include "date.h"
int main()
{
int d, m, y;
int d1, m1, y1;

cout << "enter value for date , month , year " << endl;
cin >> d >> m >> y;
while (d > 31 || d < 1)
{

cout << "enter the correct value for date \n";


cin >> d1;
d = d1;

}
while (m>12 || m < 1)
{

cout << "enter the correct value for month \n";


cin >> m1;
m = m1;
}

Date d3(d,m,y);
d3.print();
int i = 0;
cout << endl;
while (i != 1){
cout << d3.nextday() << endl;
cout << "\npress 1 to stop incresing the day; ";
cin >> i;

}
d3.print();
d3.nextmonth();//incrementing month
d3.nextyear();//incrementing year
d3.print();//printing date with incremented year
}

You might also like