Object Oriented Programming (LAB) : Course Tittle
Object Oriented Programming (LAB) : Course Tittle
Course Code:
CC 1022L
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)
{
}
while (m>12 || m < 1)
{
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
}