You are on page 1of 14

18MCA2PCOP: Object Oriented Programming using C++ 2019

1. Write a C++ program to create a class by name date with the member data
day,month, and year include member functions to perform the following:
a. To accept a date,
b. To display a date.
c. To find whether the date is valid or not.
d. To check one date is earlier than the other.
e. To Increment a date.
f. To find the next date after adding a number of days.

SOURCE CODE:

#include<iostream>
using namespace std;
class date
{
int dd,mm,yy;
public:
void get_date();
void display_date();
void getprev_day();
void incre_day();
void add_days();
void get_increday();
};
void date::get_date()
{
cout<<"****Date program****\n";
cout<<"Enter the Day Month and Year :\n";
cin>>dd>>mm>>yy;
if(yy>0)
{
if(mm==1 || mm==3 || mm==5 || mm==7 || mm==8 || mm==10 || mm==12)
{
if(dd>0 && dd<=31)
cout<<"Date is valid\n";
else
cout<<"Date is invalid";
}
else
if(mm==4 || mm==6 || mm==6 || mm==11 && dd>0 && dd<=30)
cout<<"Date is valid \n";
else
if(mm==2)
{
if((yy%400==0 ||(yy%100!=0 && yy%4==0)) || dd>0 && dd<=29)

Department of Computer Applications, BMSCE Page 1


18MCA2PCOP: Object Oriented Programming using C++ 2019

cout<<"Date is valid \n";


else if(dd>0 && dd<=28)
cout<<"Date is valid \n";
else
cout<<"date is invalid \n";
}
else
cout<<"date is invalid \n";
}
else
cout<<"date is invalid \n";
}
void date::display_date()
{
cout<<"\n Date is in dd/mm/yy format:"<<dd<<"/"<<mm<<"/"<<yy<<endl;
}
void date::getprev_day()
{
int day, month,year;
cout<<"\n****Get Previous Date****\n";
cout<<"Enter Day,Month and Year to check previous date:"<<endl;
cin>>day>>month>>year;
cout<<endl<<"Date : "<<day<<"/"<<month<<"/"<<year;
if(day==1)
{
if(month==4||month==6||month==9||month==11)
{
day=31;
month=month-1;
}
else if(month==3)
{
if(year%4==0)
day=29;
else
day=28;
month=month-1;
}
else if(month==1)
{
day=31;
month=12;
year=year-1;
}
else if(month==2)
{

Department of Computer Applications, BMSCE Page 2


18MCA2PCOP: Object Oriented Programming using C++ 2019

day=31;
month=month-1;
}
else{
day=30;
month=month-1;
}
}
else{
day=day-1;
}
cout<<endl<<"Previous Date:"<<day<<"/"<<month<<"/"<<year<<endl;
}
void date::get_increday()
{
int n=1;
cout<<"\n****Increment Date****\n";
cout<<"Enter Day,Month and Year for Incrementing a Date:"<<endl;
cin>>dd>>mm>>yy;
int m2[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int i,k=0,p,a;
for(i=0;i<mm;i++)
k=k+m2[i];
k=k+dd+n;
cout<<endl<<"\nNew Date After Incrementing Date is:\n";
if(k<=365)
{
for(i=0;i<13;i++)
{
p=k-m2[i];
if(p<=m2[i+1])
{
a=i+1;
break;
}
else
k=p;
}
cout<<p<<"/"<<a<<"/"<<yy;
}
else{
k=k-365;
for(i=0;i<13;i++)
{
p=k-m2[i];
if(p<=m2[i+1])

Department of Computer Applications, BMSCE Page 3


18MCA2PCOP: Object Oriented Programming using C++ 2019

{
a=i+1;
break;
}
else
k=p;
}
cout<<p<<"/"<<a<<"/"<<yy+1<<"\n";
}
}
void date::add_days()
{
int No_Day;
cout<<"\n****Adding No_Of_Days****\n";
cout<<"Enter Day,Month and Year :"<<endl;
cin>>dd>>mm>>yy;
cout<<"Enter the No of Days you want to Add:"<<endl;
cin>>No_Day;
int m2[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int i,k=0,p,a;
for(i=0;i<mm;i++)
k=k+m2[i];
k=k+dd+No_Day;
cout<<endl<<"New Date After Adding the No_Of_Days:"<<No_Day<<"Days is: ";
if(k<=365)
{
for(i=0;i<13;i++)
{
p=k-m2[i];
if(p<=m2[i+1])
{
a=i+1;
break;
}
else
k=p;
}
cout<<p<<"/"<<a<<"/"<<yy;
}
else{
k=k-365;
for(i=0;i<13;i++)
{
p=k-m2[i];
if(p<=m2[i+1])
{

Department of Computer Applications, BMSCE Page 4


18MCA2PCOP: Object Oriented Programming using C++ 2019

a=i+1;
break;
}
else
k=p;
}
cout<<p<<"/"<<a<<"/"<<yy+1<<"\n";
}
}
int main()
{
date d;
d.get_date();
d.display_date();
d.getprev_day();
d.get_increday();
d.add_days();
return 0;
}

Department of Computer Applications, BMSCE Page 5


18MCA2PCOP: Object Oriented Programming using C++ 2019

OUTPUT:

Department of Computer Applications, BMSCE Page 6


18MCA2PCOP: Object Oriented Programming using C++ 2019

2. Write a C++ program to create a class by name emp with the member data
empno, name, deptname, designation, age and salary and perform the
following by including the member functions in the class:
a. To accept the details of an employee
b. To display the details of an employee
c. To search for an employee in a list of employees by means of empno.
d. To find the youngest employee in a list of employees
e. To find the highest paid employee
f. To sort the list of employees in the increasing order of names

SOURCE CODE:

#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
class emp
{
public:
int age,empno,n;
char name[20],dname[20],desg[20];
float sal;
void getinfo();
void display();
void sortname();
};
void emp::getinfo()
{
cout<<"======================================================\n";
cout<<"enter emp no:";
cin>>empno;
cout<<"enter name:";
cin>>name;
cout<<"enter salary:";
cin>>sal;
cout<<"enter department name:";
cin>>dname;
cout<<"enter designation:";
cin>>desg;
cout<<"enter age:";
cin>>age;
}
void emp::display()
{

Department of Computer Applications, BMSCE Page 7


18MCA2PCOP: Object Oriented Programming using C++ 2019

cout<<"employee details";
cout<<"\nemployee number:"<<empno;
cout<<"\nemployee name:"<<name;
cout<<"\nemployee age:"<<age;
cout<<"\nemployee department:"<<dname;
cout<<"\nemployee designation:"<<desg;
cout<<"\nsalary:"<<sal;
}
int main()
{
emp e[10];
int i,j,n,a,age1,max;
char tem[20];
cout<<"\n enter number of employees:";
cin>>n;
for(i=0;i<n;i++)
e[i].getinfo();
for(i=0;i<n;i++)
e[i].display();
cout<<"\nenter empno no to search:";
cin>>a;
for(i=0;i<n;i++)
{
if(e[i].empno==a)
{
e[i].display();
}
}
cout<<"===============================================\n";
cout<<"\nyoungest age of an employee is:\n";
age1=0;
for(i=1;i<=n;i++)
{
if(e[i].age<e[age1].age)
{
age1=i;
}
}
cout<<e[age1].age<<" and name is "<<e[age1].name<<endl;
cout<<"\n=======================================\n";
cout<<"highest paid employee is:"; max=0;

for(i=1;i<=n;i++)
{
if(e[i].sal>e[max].sal)
{

Department of Computer Applications, BMSCE Page 8


18MCA2PCOP: Object Oriented Programming using C++ 2019

max=i;
}}
cout<<e[max].sal<<" and name is "<<e[max].name<<endl;
cout<<"=====================================\n";
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(e[i].name,e[j].name)>0)
{
strcpy(tem,e[i].name);
strcpy(e[i].name,e[j].name);
strcpy(e[j].name,tem);
}
}
cout<<"\n sorted name in alphabetical order:\n\n";
for(i=0;i<n;i++)
{
cout<<" ";
cout<<e[i].name<<"\n";
}
return 0;
}
}

Department of Computer Applications, BMSCE Page 9


18MCA2PCOP: Object Oriented Programming using C++ 2019

OUTPUT:

Department of Computer Applications, BMSCE Page 10


18MCA2PCOP: Object Oriented Programming using C++ 2019

3. a. Write a C++ program to create a class by name rectangle with length


and breadth as its member data. Provide constructors to initialize the objects
of the class and find the area and the perimeter of a rectangle.
Area = length * breadth ,Perimeter = 2* (length+ breadth).

SOURCE CODE:

#include<iostream>
using namespace std;
class rectangle
{

private:
int length, breadth;
int area, perimeter;

public:
rectangle()
{
cout<<"\nThe Area and perimeter of the rectangle: ";
cout<<"\nEnter the value of length: ";
cin>>length;
cout<<"Enter the value of breadth: ";
cin>>breadth;
area=length*breadth;
perimeter=2*(length+ breadth);
}
void display()
{
cout<<"\n Area is: "<<area<<endl;
cout<<"\n perimeter of rectangle is: "<<perimeter<<endl;
}
};
int main()
{
rectangle x;
x.display();
return 0;
}

Department of Computer Applications, BMSCE Page 11


18MCA2PCOP: Object Oriented Programming using C++ 2019

OUTPUT:

Department of Computer Applications, BMSCE Page 12


18MCA2PCOP: Object Oriented Programming using C++ 2019

b. Write a C++ program to find factorial using copy constructor.

SOURCE CODE:

#include<iostream>
using namespace std;
class fact
{
int n, i, facti;
public:
fact(int x)
{
n=x;
facti=1;
}
fact(fact &x)
{
n=x.n;
facti=1;
}
void calculate()
{
for(i=1; i<=n; i++)
{
facti=facti*i;
}
}
void display()
{
cout<<"\n Factorial : "<<facti;
}
};
int main()
{
int x;
cout<<"\n Enter Value : ";
cin>>x;
fact f1(x);
f1.calculate();

Department of Computer Applications, BMSCE Page 13


18MCA2PCOP: Object Oriented Programming using C++ 2019

f1.display();
fact f2(f2);
f2.calculate();
f2.display();
return 0;
}

OUTPUT:

Department of Computer Applications, BMSCE Page 14

You might also like