Write A Program To Demonstrate Type Conversation and Constructor With Operator Overloading

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

NAME:PURAV SHAH ROLL NO:31 EC-A

Q. Write a program to demonstrate type conversation and

constructor with operator overloading.


#include<iostream.h> #include<conio.h> class time { inth,m; public: time(){} //default constructor time (int a)//Constructor -> for converting Basic to Class {h=(a/60); m=(a%60);} time(int a, int b)//Constructor { h=a; m=b; } voidsettime() { cout<<"\n Enter the hours:"; cin>>h; cout<<" Enter the minutes:"; cin>>m; } voidshowtime() { //cout<<h<<" hours "<<m<<" minutes"; cout<<h<<" : "<<m; } time operator+(time); friend time operator+(int , time ); operatorint(); // convert calss to basic }; time time::operator+(time t2) { time t; t.h=h + t2.h; t.m=m + t2.m; return(t); } time operator+(int m1 , time t1) //friend

NAME:PURAV SHAH ROLL NO:31 EC-A

{ ime t; t.h=(m1/60)+t1.h; t.m=(m1%60)+t1.m; return(t); } time::operatorint() { return(h*60 + m); } int main() { clrscr(); cout<<"POWERED BY PSTECHNOSYSTEMS.ORG\n\n"; time t1(260),t2(2,30),t3; cout<<"\n Initial Times "; cout<<"\n t1 = "; t1.showtime(); cout<<"\n t2 = "; t2.showtime(); cout<<"\n\n :: Example of Operator Oveloading Using Member Function :: "; cout<<"\n t3 = t1 + t2" ; t3 = t1 + t2; cout<<"\n t1 = "; t1.showtime(); cout<<"\n t2 = "; t2.showtime(); cout<<"\n t3 = "; t3.showtime(); cout<<"\n\n :: Example of Operator Oveloading using Friend Function :: "; cout<<"\n t3 = Minutes + t1"; cout<<"\n Enter Minutes to add to time object t1 = "; int min; cin>>min; t3 = min + t1; cout<<" t1 = "; t1.showtime(); cout<<"\n t3 = "; t3.showtime();

NAME:PURAV SHAH ROLL NO:31 EC-A

cout<<"\n\n :: Example of Type Conversion Basic(Integer) to Class(time) :: Achieved with Single agument constructor :: "; cout<<"\n\n t3 = Minutes"; cout<<"\n Enter Minutes to convert to time object :: "; cin>>min; t3 = min; cout<<" New t3 after type conversion ::"; t3.showtime(); cout<<"\n\n Type Conversion Class(time) to Basic(Integer) ::\n Using operator int() method :: "; cout<<"\n\n Minutes = t3"; t3.settime(); cout<<" t3 = "; min = t3; t3.showtime(); cout<<" = "<<min; cout<<" minutes\n"; return 0;
} OUTPUT

You might also like