Assignemnt 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

OOP Assignment#2

Submitted By: Hamza Arif Khan (FA21-EEE-008)


Task#1

Code:
#include <iostream>

#include <string>

using namespace std;

class Date{

private:

int month;

int day;

int year;

public:

Date(int m,int d,int y){

if(d>0 || d<=31){

day = d;

if(m>0 || m<=12){

month = m;

if(y>=1900){

year = y;
}

void setDate(int m,int d,int y){

if(d>0 || d<=31){

day = d;

if(m>0 || m<=12){

month = m;

if(y>=1900){

year = y;

}}

Date(){month = 1;

year = 1900;

day = 1;

int getDay() const

return day;

int getMonth() const

return month;

int getYear() const

return year;

void setDay(int day)


{

if(day>0 || day<=31)

this->day = day;

void setMonth(int month)

if(month>0 || month<=31)

this->month = month;

void setYear(int year)

if(year>1900)

this->year = year;

void printDate(){

cout<<year<<"/";

if(month>9){

cout<<month<<"/";

}else{

cout<<"0"<<month<<"/";

if(day>9){

cout<<day;

}else{

cout<<"0"<<day;

}}};

int main() {

Date dateDefault;

cout<<"Default date print : ";


dateDefault.printDate();

cout<<"\nParameter date print : ";

Date date(10, 18, 2022);

date.printDate();

int m,y,d;

cout<<"\nEnter month : ";

cin>>m;

cout<<"Enter day : ";

cin>>d;

cout<<"Enter year : ";

cin>>y;

dateDefault.setDate(m,d,y);

cout<<"Entered date is : ";

dateDefault.printDate();

Task#2

Date header file:

#include <iostream>

#include <string>
using namespace std;

class Date{

private:

int month;

int day;

int year;

friend void print();

public:

Date(int m,int d,int y){

if(d>0 || d<=31){

day = d;

if(m>0 || m<=12){

month = m;

}
if(y>=1900){

year = y;

void setDate(int m,int d,int y){

if(d>0 || d<=31){

day = d;

if(m>0 || m<=12){

month = m;

if(y>=1900){

year = y;

}}
Date(){month = 1;

year = 1900;

day = 1;

int getDay() const

return day;

int getMonth() const

return month;

int getYear() const

return year;
}

void setDay(int day)

if(day>0 || day<=31)

this->day = day;

void setMonth(int month)

if(month>0 || month<=31)

this->month = month;

void setYear(int year)

if(year>1900)
this->year = year;

void printDate(){

cout<<year<<"/";

if(month>9){

cout<<month<<"/";

}else{

cout<<"0"<<month<<"/";

if(day>9){

cout<<day;

}else{

cout<<"0"<<day;

}}};
Employee headerfile:

#include<iostream>

#include<string.h>

#include"date.h"

using namespace std;

class Employee{

protected:

char firstname[25];

char lastname[25];

Date birthDate;

Date hireDate;

public:

Employee(char first[25],char last[25],Date Bday, Date Hday ){

strcpy(firstname,first);

strcpy(lastname,last);

birthDate=Bday;

hireDate=Hday;

void print(){

cout<<"Name the employee is "<<firstname<<" "<<lastname<<endl;

birthDate.printDate();

cout<<endl;

hireDate.printDate();

cout<<endl;

};
Main code:

#include<iostream>

#include<string.h>

#include"employee.h"

using namespace std;

int main(){

Date birth(7,24,1949);

Date hire(3,12,1988);

Employee manager("bob","blue",birth,hire);

manager.print();

return 0;

Task#3

Code:

#include<iostream>

using namespace std;

class Count{

private:
int x;

friend int set(Count);

public:

Count() : x(0) {}

};

int set(Count C){

C.x=50;

return C.x;

int main(){

Count C;

cout<<"the value of x is: "<<set(C)<<endl;

return 0;

You might also like