Create a class time with member data hour and minute. Overload ++ unary operator for class time for increment and -- unary operator for decrement in time object value.
#include<iostream>
using namespace std;
class time
{
int hour;
int minute;
public:
void settime()
{
cout<<"\n Enter Hour and Minutes";
cin>>hour>>minute;
}
void printtime()
{
cout<<"\n"<<hour<<" : "<<minute;
}
void operator ++() //prefix
{
minute=minute+1;
if(minute>=60)
{
minute=minute-60;
hour=hour+1;
}
hour =hour+1;
}
void operator ++(int) //postfix
{
minute=minute+1;
if(minute>=60)
{
minute=minute-60;
hour=hour+1;
}
hour =hour+1;
}
void operator --() //prefix
{
hour=hour-1;
if(minute>0)
minute=minute-1;
else
{
hour=hour-1;
minute=59;
}
}
void operator --(int) //postfix
{
hour=hour-1;
if(minute>0)
minute=minute-1;
else
{
hour=hour-1;
minute=59;
}
}
};
void main()
{
time t1,t2;
cout<<"\n \n Increment operator";
t1.settime();
t1++;
t1.printtime();
++t1;
t1.printtime();
cout<<"\n \n Decrement operator";
t2.settime();
t2--;
t2.printtime();
--t2;
t2.printtime();
}
Create a class string with character array as a data member and write a program to add two strings with use of operator overloading concept.
#include<iostream>
#include<string>
using namespace std;
class mystring
{
char s[20];
public:
mystring()
{ s[0]='\0';
}
mystring( char c[10])
{
strcpy(s,c);
}
void operator +(mystring s11)
{
strcat(s,s11.s);
cout<<s;
}
};
void main()
{
mystring s1("abc");
mystring s2("def");
s1+s2;
}
Create a class distance which contains feet and inch as a data member. Overhead = = and < operator for the same class. Create necessary functions and constructors too.
#include<iostream>
using namespace std;
class distanceCLS
{
int feet, inch;
public:
distanceCLS()
{
feet=inch=0;
}
distanceCLS(int f, int i)
{
feet=f;
inch=i;
}
void operator ==(distanceCLS d11)
{
int d1=(feet*12) + inch;
int d2=(d11.feet*12) + d11.inch;
if(d1==d2)
cout<<"\n Both distance are same";
else
cout<<"\n Both distance are not same" ;
}
int operator < (distanceCLS d11)
{
int d1=(feet*12) + inch;
int d2=(d11.feet*12) + d11.inch;
if(d1<d2)
return(1);
else
return(0);
}
};
void main()
{
distanceCLS d1(1,2),d2(2,4);
cout<<"\n == operator";
d1==d2;
cout<<"\n < operator";
if(d1<d2)
cout<<"\n \n d1 is less";
else
cout<<"\n \n d2 is less";
}
Create a class MATRIX of size mxn. Overload + operator for addition of the MATRIX.
#include<iostream>
using namespace std;
class MATRIX
{
int a[2][2];
public:
void setdata()
{
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
cout<< "\n Enter element";
cin>> a[i][j];
}
}
}
void printdata()
{
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
cout<<a[i][j];
}
cout<<"\n";
}
}
MATRIX operator +(MATRIX m11)
{
MATRIX temp;
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
temp.a[i][j]=a[i][j]+m11.a[i][j];
}
}
return(temp);
}
};
void main()
{
MATRIX m1,m2,m3;
cout<<"\n \n Enter first matrix";
m1.setdata();
cout<<"\n \n Enter second matrix";
m2.setdata();
m3=m1+m2;
cout<<"\n \n First matrix is \n ";
m1.printdata();
cout<<"\n \n Second matrix is \n";
m1.printdata();
cout<<"\n \n Result matrix is \n";
m3.printdata();
}
Create one class called Rupees, which has one member data to store amount in rupee and create another class called Paise which has member data to store amount in paise. Write a program to convert one amount to another amount with use of type conversion.
#include<iostream>
using
namespace std;
class
paise
{
int p;
public:
void setdata(int a)
{
p=a;
}
void printdata()
{
cout<<"\n Paise is
\n"<<p;
}
};
class
Rupees
{
int r;
public:
Rupees()
{
r=10;
}
operator paise()
{
paise t;
t.setdata(r*100);
return(t);
}
};
void main()
{
Rupees r1;
paise p1;
p1=r1;
p1.printdata();
}