Title
of Exp:
Class,
Object and methods implementation.
Set
A:
(a)Write
the definition for the class called 'time' that has hours, minutes &
seconds as integer data members.The class has the following member
functions.
//ASSignment
9:
//set
A
#include<iostream.h>
//using
namespace std;
class
clk
{
int hr,min,sec;
public:
void
settime(int h,int m,int s)
{
hr=h;
min=m;
sec=s;
}
void
showtime()
{
cout<<"\n\t
Houre="<<hr<<endl;
cout<<"\t
Minutes="<<min<<endl;
cout<<"\t
seconds="<<sec<<endl;
}
void
add(int h,int m,int s)
{
hr=hr+h;
min=min+m;
sec=sec+s;
if(sec>60)
{
int
rem=sec/60;
int
quo=sec%60;
min=min+rem;
sec=quo;
if(min>60)
{
int
rem=min/60;
int
quo=min%60;
hr=hr+rem;
min=quo;
}
}
cout<<"\n\n
After addition the time is:-";
cout<<"\n
Hour:-"<<hr<<"\t Minutes:-"<<min<<"\t
second:-"<<sec;
}
void
diff(int h,int m,int s)
{
hr=hr-h;
min=min-m;
sec=sec-s;
cout<<"\n\n
After subtraction the time is:-";
cout<<"\n
Hour:-"<<hr<<"\t Minutes:-"<<min<<"\t
seconds:-"<<sec;
}
};
main()
{
int
h,m,s;
cout<<"\n\n
Enter time (hr,min,sce):-";
cin>>h>>m>>s;
clk c;
c.settime(h,m,s);
cout<<"\n\n
updated time="<<endl;
c.showtime();
cout<<"\n\n
Enter the time to be added:-";
cin>>h>>m>>s;
c.add(h,m,s);
cout<<endl;
cout<<"\n\n
Enter the time to be subtracted:-";
cin>>h>>m>>s;
c.diff(h,m,s);
cout<<endl;
}
****************
Out Put ***************
Enter
time (hr,min,sec):-2 15 27
update
time=
Houre=2
Minutes=15e
Second=27
Enter
the time to be added:-2 15 03
After
addition the time is:-
Hour:-4
Minutes:-30 second:-30
Enter
the time to be subtracted:-2 15 03
After
subtraction the time is :-
Hour:-2 Minutes:-15 seconds:-27
(b)
Write
the definition for the class called “CuboidSolid” that has length
breadth,height and mass has float data members. The class has the
following member functions.
//A9setA:2
#include"iostream.h"
#include<stdio.h>
//using
namespace std;
class
cuboidsolid
{
float
length,breadth,height,mass,volume;
public:
float
getvolume();
float
getsurfacearea();
float
getdensity();
};
float
cuboidsolid::getvolume()
{
cout<<"\n\n
Enter the lentgth:-";
cin>>length;
cout<<"\n
Enter the breadth:-";
cin>>breadth;
cout<<"\n
Enter the height:-";
cin>>height;
volume=length*breadth*height;
return
volume;
}
float
cuboidsolid::getsurfacearea()
{
float
sarea;
sarea=((2*length*breadth)+(2*breadth*height)+(2*length*height));
return
sarea;
}
float
cuboidsolid::getdensity()
{
float
density;
cout<<"\n\n
Enter the mass:-";
cin>>mass;
density=mass/volume;
return
density;
}
void
main()
{
float
v,sa,d;
cuboidsolid
cs;
v=cs.getvolume();
cout<<"\n\n
volume of the cuboid="<< v;
sa=cs.getsurfacearea();
cout<<"\n\n
surface area of the cuboid="<<sa;
d=cs.getdensity();
cout<<"\n\n
density of the cuboid="<< d;
cout<<endl<<endl;
}
****************
Out Put ***************
Enter
the lentgth:-27
Enter
the breadth:-13
Enter
the height:-1
volume
of the cuboid=351
surface
area of cuboid=782
Enter
the mass:-6
density
of the cuboid=0.017094
Title
of Exp:
Constructor:copy,default¶meterizedconstructor.
Set
A:
(a)
Program for the class ‘Fraction’ having integer data member
numerator and denominator.
//Assignment
no:10
//set
A:1
#include<iostream.h>
//using
namespace std;
class
fraction
{
int
numerator,denominator,div;
int
gcdiv(int n,int d)
{
if(n>d)
{
div=n;
int
que,mul,rem;
que=d;
rem=1;
while(rem>1)
{
int
q=div/que;
mul=que*q;
rem=div-mul;
div=que;
que=rem;
}
return
div;
}
else
{
div=d;
int
que,mul,rem;
que=n;
rem=1;
while(rem>0)
{
int
q=div/que;
mul=que*q;
rem=div-mul;
div=que;
que=rem;
}
}
return
div;
}
public:
void
accept(int m,int n);
void
addition(fraction f1,fraction f2);
void
substraction(fraction f1,fraction f2);
void
multiplication(fraction f1,fraction f2);
void
division(fraction f1,fraction f2);
fraction();
fraction(int
n,int m);
void
show();
};
void
fraction::accept(int n,int m)
{
numerator=n;
denominator=m;///
}
void
fraction::addition(fraction f1,fraction f2)
{
int
anum,aden;
anum=f1.numerator
+ f2.numerator;
aden=f1.denominator
+ f2.denominator;
cout<<"\n\n\n
addition of numerator="<<anum;
cout<<"\n\n\n
addition of denominator ="<<aden;
}
void
fraction::substraction(fraction f1,fraction f2)
{
int
snum,sden;
snum=f1.numerator
- f2.numerator;
sden=f1.denominator-
f2.denominator;
cout<<"\n\n\n
substraction of numerator="<<snum;
cout<<"\n\n\n
substraction of numerator="<<sden;
}
void
fraction::multiplication(fraction f1,fraction f2)
{
int
mnum,mden;
mnum=f1.numerator
* f2.numerator;
mden=f1.denominator
* f2.denominator;
cout<<"\n\n\n
multiplication of numerator="<<mnum;
cout<<"\n\n\n
multiplication of denominator="<<mden;
}
void
fraction::division(fraction f1,fraction f2)
{
int
gcd=gcdiv(f1.numerator,f2.numerator);
cout<<"\n\ngcd
of numerator="<<gcd<<endl;
gcd=gcdiv(f1.denominator,f2.denominator);
cout<<"\n\ngcd
of denominator="<<gcd<<endl;
}
fraction::fraction()
{
numerator=0;
denominator=1;
}
fraction::fraction(int
n,int m)
{
numerator=n;
denominator=m;
}
void
fraction::show()
{
cout<<"\n\n
numerator="<<numerator;
cout<<"\n\n
denominator="<<denominator;
}
main()
{
int
n,m;
fraction
f;
cout<<"\n\n
enter tow numbers:";
cin>>n>>m;
fraction
f1(n,m);
cout<<"\n\n
enter two number:";
cin>>n>>m;
//fraction
f1(n,m);
//cout<<"\n\n
enter two numbers:";
//cin>>n>>m;
fraction
f2(n,m);
f.show();
f1.show();
f2.show();
f1.addition(f1,f2);
f1.multiplication(f1,f2);
f1.division(f1,f2);
cout<<endl<<endl<<endl;
}
*****************Out
Put*********************
Numerator=27
Denominator=13
Numerator=1
Denominator=6
Addition
of numerator=28
Addition
of denominator=19
multiplication
of numerator=27
multiplication
of denominator=78
gcd
of numerator=27
gcd of denominator=13
Title of Exp: Inline function, friend function, defult argument, Inline function
a)
Implement a class date with three integer data member day,month and
year.
//Assignment
no:12
//set
A
#include<iostream.h>
#include<conio.h>
class
date
{
int
dt,mn,yr;
public:
inline
int compress(int d,int m,int y)
{
int comp;
dt=d;
mn=m;
yr=y;
comp=
(yr*365) + (mn*30) +dt;
cout<<"\n\n
Given date in total days ="<< comp;
return
comp;
}
int
diff(int d1, int d2)
{
int
dif;
cout<<"\n\n
d1="<<d1;
cout<<"\n\n
d2="<<d2;
if(d1>d2)
dif=d1-d2;
else
dif=d2-d1;
return
dif;
}
};
int
main()
{
date
a;
int
p,q,r,d,c1,c2;
cout<<"\n\n
Enter the date(DD:MM:YYYY):-";
cin>>p>>q>>r;
c1=a.compress(p,q,r);
cout<<"\n\n
Compress date"<<p<<"/"<<q<<"/"<<r<<"in
days"<<c1;
cout<<"\n\n
Enter the second date (HH:MM:YYYY):-";
cin>>p>>q>>r;
c2=a.compress(p,q,r);
d=a.diff(c1,c2);
cout<<"\n\n
Diffrence between two dates in days="<<d;
cout<<endl<<endl;
return(0);
}
****************Out
Put*************************
Enter
the date(DD:MM:YYYY):-27 2 2016
Given
date in total days =15031
Compress
date27/2/2016in days15031
Enter
the second date (HH:MM:YYYY):-3 3 2016
Given
date in total days =15037
d1=15031
d2=15037
Diffrence
between two dates in days=6
Title
of Exp:
13:
Function Overloading.
SET
A:
(a)Implement
a class ‘printdata’ with three member function all with the same
name ‘print’.
//Assignment
No:13//
//set
A:1//
#include<iostream.h>
//using
namespace std;
class
printdata
{
int
m,p,q;
char
str[10];
public:
void
print(int);
void
print(int,int);
void
print(char *);
};
void
printdata::print(int m)
{
cout<<"\n
You have entered interger value="<< m;
}
void
printdata::print(int p,int q)
{
cout<<"\n
use entered two interger values as=\t"<<p<<"\t
and \t" <<q;
}
void
printdata::print(char *str)
{
cout<<"\n
User entered the string:-"<<str;
}
main()
{
int
num,num1,num2;
char
s[10];
printdata
p;
cout<<"\n\n
Enter any one number:-"<<endl;
cin>>num;
p.print(num);
cout<<"\n\n
Enter two numbers:-";
cin>>s;
p.print(s);
cout<<endl<<endl<<endl;
p.print(15);
p.print(40,60);
p.print("hello");
cout<<endl<<endl;
}
****************
Out Put ***************
Enter
any one number :-
27
You
have entered interger value = 27
Enter
two numbers :-
9
1
user
entered the string :- 9
You
have entered interger value = 15
Use
entered two interger values as = 40 and 60
User
entered the string :- hello
(b)Implement
a class ‘maxdata’ with two member function both with the same
name ‘maximum’.
//A13seta:2//
#include<iostream.h>
#include<conio.h>
//using
namespace std;
class
maxdata
{
int
m,n,s[10];
public
:
int
maximum(int, int); // Member function of class
int
maximum(int *s);
}; //
End of the class
int
maxdata::maximum( int m,int n ) //function to find out maximum
number between two numbers
{
if(m
> n )
return
m;
else
return
n;
}
int
maxdata :: maximum(int *s) // function to find out maximum number
from array
{
int
t,j;
t=s[0];
for(j=1;j<10;j++)
{
if(t<s[j])
t=s[j];
}
return
t;
}
void
main()
{
int
num1,num2,i,maxvalue,p[10],mva;
maxdata
m; // Create the object 'm' of the class maxdata
cout<<"\n\n
Enter two numbers :- "<<endl;
cin>>num1>>num2;
maxvalue=
m.maximum(num1,num2); // call function maximum(int,int)
cout<<"\n\n
Maximum value = "<<maxvalue;
cout<<"\n\n
Enter ten values of array numbers :- "<< endl;
for(i=0;i<10;i++)
{
cin>>p[i];
}
mva=
m.maximum(p); // Call function maximum ( int *)
cout<<"\n\n
Maximum Value from array = "<<mva;
cout<<endl<<endl<<endl;
getch();
}
****************
Out Put ***************
Enter
two numbers :-
27
13
Maximum
value = 27
Enter
ten values of array numbers :-
10
20
30
40
50
60
70
80
90
100
Maximum
Value from array = 100
Title
of Exp:
14:
Operator
Overloading.
SET
A:
(a)Define
a class named Complex for representing complex number.
#include<iostream.h>
//using
namespace std;
class
complex
{
int
x,y,a,b;
public:
complex()
{ x=3;
y=4;
}
complex(int,int)
{
x=a;
y=b;
}
void
show()
{
if(y<0)
cout<<x<<y<<"i";
else
cout<<x<<"+i"<<y<<endl;
}
void
read()
{
cout<<"\n
Enter the real and imaginary parts of complex number:-\t";
cin>>x>>y;
}
friend
complex operator+(complex c1,complex c2);
friend
complex operator-(complex c1,complex c2);
};
complex
operator+(complex c1,complex c2)
{
complex
c;
c.x=c1.x+c2.x;
c.y=c1.y+c2.y;
return
c;
}
complex
operator-(complex c1,complex c2)
{
complex
c;
c.x=c1.x-c2.x;
c.y=c1.y-c2.y;
return
c;
}
int
main()
{
int
a,b,x,y;
complex
c1,c2,c3,c4;
cout<<"\n
Default constructor value:-\t";
c1.show();
cout<<"\nEnter
the real and imaginary parts of complex number:-\t";
cin>>x>>y;
complex
c(a,b);
cout<<"\n
Parameterized constructor value:-\t";
c.show();
cout<<"\n
Enter value for addition:-\t";
c1.read();
c2.read();
cout<<"\n
Addition of two complex number:-\t";
c3=c1+c2;
c3.show();
cout<<"\n
Substraction of two complex number:-\t";
c4=c1-c2;
c4.show();
cout<<endl<<endl;
}
****************
Out Put ***************
Default
constructor value :-3+i4
Enter
the real parts and imaginary parts :-2 3
parameterized
constructor value :-2+i3
enter
values for addition :-2 3
Enter
the real and imaginary parts of complex number :-2 3
Enter
the real and imaginary parts of complex number :-3 2
Addition
of two complex number :-5+i5
Substraction
of two complex numbers :- -1-i1
(b)Define
a class name Clock with three intger data members for hours,minutes
and seconds.
#include<iostream.h>
#include<conio.h>
//using
namespace std;
class
clk
{
int
hr,min,sec;
public:
clk()
{
hr=9;
min=20;
sec
=30;
}
clk(int
h,int m,int s)
{
hr=h;
min=m;
sec=s;
}
void
show()
{
cout<<"\n
hours="<<hr<<"\t minutes="<<min<<"\t
seconds="<<sec<<endl;
}
void
operator ++()
{
hr++;
min++;
sec++;
}
void
operator --()
{
hr--;
min--;
sec--;
}
//friend
istream& operator>>(istream&,clk&);
//friend
ostream& operator>>(ostream&,const clk&);
friend
istream& operator >>(istream &in,clk &d)
{
cout<<"\n Enter new time (hh)-";
in>>d.hr;
cout<<"\n Enter min:-";
in>>d.min;
cout<<"\n Enter sec:-";
in>>d.sec;
return in;
}
friend
ostream& operator <<(ostream& out,clk& d)
{
out<<"\n
Time by insertion operator overloading function
:-"<<d.hr<<":"<<d.min<<":"<<d.sec;
return
out;
}
};
void
main()
{
int
h,m,s;
clk
c,c2;
cout<<"\n
Time by default constructor=";
c.show();
cout<<"\n
Enter new time (hh:mm:ss)=";
cin>>h>>m>>s;
cout<<"\n
Time given by you(parameterized constructor)=";
clk
c1(h,m,s);
c1.show();
cout<<"\n
time increment by increment operator:-";
++c1;
c1.show();
cout<<"\n
time decrement by decrement operator:-";
--c1;
c1.show();
cout<<c2;
cin>>c2;
cout<<endl<<endl<<endl;
getch();
}
****************
Out Put ***************
Time
by default constructor=
hours=9
minutes=20 seconds=30
Enter
new time (hh:mm:ss)=7 27 13
Time
given by you(parameterized constructor)=
hours=7
minutes=27 seconds=13
time
increment by increment operator:-
hours=8
minutes=28 seconds=14
time
decrement by decrement operator:-
hours=7
minutes=27 seconds=13
Enter
new time (hh)-2
Enter
min:-33
Enter
sec:-12
Title
of Exp:
15: Inheritance
SET A:Inheritance is the process of creating new classes
from an existing class.
#include<iostream.h>
int
n1,n2;
class
employee
{
protected:
int
c_code;
char
name[20];
public:
void
getdata()
{
cout<<"\nEnter
employee code:";
cin>>c_code;
cout<<"Enter
employee name:";
cin>>name;
}
void
putdata()
{
cout<<"\n\nEmployee
code:"<<c_code;
cout<<"\n\nEmployee
Name:"<<name;
}
};
class
fulltime:public employee
{
int
d_rate;
int
no_of_days;
int
sal;
public:
void
accept_f()
{
cout<<"\nEnter
daily rate:";
cin>>d_rate;
cout<<"\nEnter
no of days:";
cin>>no_of_days;
sal=d_rate*no_of_days;
}
void
disp_f()
{
putdata();
cout<<"\nDaily
RAte:"<<d_rate;
cout<<"\nNumber
of days:"<<no_of_days;
cout<<"\nsalary:"<<sal;
}
friend
int search(fulltime *,int);
};
class
parttime:public employee
{
protected:
int
hrs;
int
hr_rate;
int
sal;
public:
void
accept_p()
{
cout<<"\nEnter
hours worked";
cin>>hrs;
cout<<"\nEnter
hour rate:";
cin>>hr_rate;
sal=hrs*hr_rate;
}
void
disp_p()
{
putdata();
cout<<"\nHours
Worked:"<<hrs;
cout<<"\nHours
Rate:"<<hr_rate;
cout<<"\nSalary:"<<sal;
}
friend
int searchp(parttime *,int);
};
int
searchp(parttime *p,int a)
{
for(int
i=0;i<n2;i++)
if(p[i].c_code==a)
return
1;
return
0;
}
int
search(fulltime *f,int a)
{
for(int
i=0;i<n1;i++)
if(f[i].c_code==a)
return
1;
return
0;
}
int
main()
{
fulltime
f[10];
parttime
p[10];
int
ch,i;
do
{
cout<<"\nMAIN
MENU\n\n";
cout<<"\n1.Accept
details of n employee";
cout<<"\n2.Display
details of n employee";
cout<<"\n3.Search
given employee";
cout<<"\n4.exit";
int
code;
cout<<"\nEnter
your choice";
cin>>ch;
switch(ch)
{
case
1: cout<<"\nEnter how many full time employee:";
cin>>n1;
for(i=0;i<n1;i++)
{
f[i].getdata();
f[i].accept_f();
}
cout<<"\nEnter
how many part time employees";
cin>>n2;
for(i=0;i<n2;i++)
{
p[i].getdata();
p[i].accept_p();
}
break;
case
2: cout<<"\nInformation of Fulltime employee:";
for(i=0;i<n1;i++)
{
f[i].disp_f();
}
cout<<"\nInformation
of Parttime Employee";
for(i=0;i<n2;i++)
{
p[i].disp_p();
}
break;
case
3: char c;
cout<<"\nEnter
which employee you want to search f(fulltime)";
cin>>c;
cout<<"\nenter
employee code:";
cin>>code;
if((c=='p'||c=='P'))
{ if(searchp(p,code))
cout<<"Employee
found"<<"\n";
else
cout<<"Employee
not found"<<"\n";
}
else
if((c=='f'||c=='F'))
{ if(search(f,code))
cout<<"\nEmployee
found"<<"\n";
else
cout<<"\nEmployee
not found"<<"\n";
}
break;
}
}while(ch!=4);
return(0);
}
****************
Out Put ***************
MAIN MENU
1.Accept
details of n employee
2.Display
details of n employee
3.Search
given employee
4.exit
Enter
your choice1
Enter
how many full time employee:2
Enter
employee code:1
Enter
employee name:Omkar
Enter
daily rate:500
Enter
no of days:30
Enter
employee code:2
Enter
employee name:Krishna
Enter
daily rate:500
Enter
no of days:30
Enter
how many part time employees1
Enter
employee code:3
Enter
employee name:sham
Enter
hour rate:100
MAIN MENU
1.Accept
details of n employee
2.Display
details of n employee
3.Search
given employee
4.exit
Enter your choice3
Enter
which employee you want to search f(fulltime)p
enter
employee code:3
Employee
found
MAIN MENU
1.Accept details of n employee
2.Display details of n
employee
3.Search given employee
4.exit
Enter your choice4