Skip to content

Instantly share code, notes, and snippets.

@ravikiran0606
Last active July 31, 2016 08:51
Show Gist options
  • Save ravikiran0606/c609906153811f0182eed9d02fb15980 to your computer and use it in GitHub Desktop.
Save ravikiran0606/c609906153811f0182eed9d02fb15980 to your computer and use it in GitHub Desktop.
C++ Program to implement the Type Conversion in Classes:
#include<iostream>
using namespace std;
class duration{
int mi;
public:
duration();
duration(int t);
friend istream& operator >>(istream& inp,duration &a){
inp>>a.mi;
return inp;
}
friend ostream& operator <<(ostream& out,duration &a){
out<<"Duration is "<<a.mi<<" minutes.";
}
};
duration::duration(){
mi=0;
}
duration::duration(int t){
mi=t;
}
class ttime{
int hr,mi;
public:
ttime();
ttime(int t);
ttime(int h,int m);
operator int();
operator duration();
friend istream& operator >>(istream& inp,ttime &a){
inp>>a.hr>>a.mi;
return inp;
}
friend ostream& operator <<(ostream& out,ttime &a){
out<<a.hr<<" hours and "<<a.mi<<" minutes. ";
return out;
}
};
ttime::ttime(){
hr=0;
mi=0;
}
ttime::ttime(int t){
hr=t/60;
mi=t%60;
}
ttime::ttime(int h,int m){
hr=h;
mi=m;
}
ttime::operator int(){
return (hr*60+mi);
}
ttime::operator duration(){
duration d(hr*60+mi);
return d;
}
int main()
{
int ch;
cout<<"Choice : 1) Time to Minutes(primitive type) conversion 2) Minutes(primitive type) to time conversion 3) Time to Duration conversion 4)Exit :P";
ttime t;
duration d;
int n;
while(1){
cout<<"\nEnter your choice...";
cin>>ch;
if(ch==1){
cout<<"Enter the time.";
cin>>t;
n=t;
cout<<"The time in minutes is "<<n;
}
else if(ch==2){
cout<<"Enter the time in minutes.";
cin>>n;
t=n;
cout<<t;
}
else if(ch==3){
cout<"Enter the time.";
cin>>t;
d=t;
cout<<d;
}
else{
break;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment