0% found this document useful (0 votes)
20 views3 pages

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

This C++ program demonstrates type conversion and constructor overloading with operator overloading. It defines a Time class with hours and minutes data members. It overloads operators like + for adding two Time objects, and allows converting between the Time class and int type by defining appropriate constructors and conversion operators. The main() function shows examples of adding Time objects, converting between Time and int, and overloading operators for these conversions.

Uploaded by

patelparind92
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

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

This C++ program demonstrates type conversion and constructor overloading with operator overloading. It defines a Time class with hours and minutes data members. It overloads operators like + for adding two Time objects, and allows converting between the Time class and int type by defining appropriate constructors and conversion operators. The main() function shows examples of adding Time objects, converting between Time and int, and overloading operators for these conversions.

Uploaded by

patelparind92
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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