0% found this document useful (0 votes)
3 views

Time duration example ( Type Conversion )

The document contains C++ code examples demonstrating type conversion between basic types and class types, as well as between different class types. It includes three main sections: converting basic types to class types, converting class types to basic types, and converting one class type to another. Each section features a class definition, methods for data handling, and a main function to execute the conversions and display results.

Uploaded by

saadkhan73212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Time duration example ( Type Conversion )

The document contains C++ code examples demonstrating type conversion between basic types and class types, as well as between different class types. It includes three main sections: converting basic types to class types, converting class types to basic types, and converting one class type to another. Each section features a class definition, methods for data handling, and a main function to execute the conversions and display results.

Uploaded by

saadkhan73212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

channel link : h ps://www.youtube.

com/c/ComputerScienceAcademy7

#include<iostream.h>
#include<conio.h>

/*
Type conversion programs:

1. Basic type to class type

*/

/* class me
{

int hr, min; // 1 hr 30 min 2 hr 20 min


public:
void display()
{
cout<<hr<<" hour "<<min<<" minutes"<<endl;

me(){}

me(int TotalMins) //121 121 / 60 => 2 121 % 60 => 1


{
hr = TotalMins / 60; //hr = 2
min = TotalMins % 60; // min = 1

};

void main()
{
clrscr();

int TotalMins = 150;

cout<<"Total minutes = "<<TotalMins<<endl;

me t;
t = TotalMins; //t = me(TotalMins);

cout<<"Time dura on in hour and minute format"<<endl;


t.display();

getch();

*/

/*
Type conversion programs:

2. Class type to Basic type

*/
/*
class me
{

int hr, min;


public:
void display()
{
cout<<hr<<" hour "<<min<<" minutes"<<endl;

void getdata(int x, int y)


{
hr = x;
min = y;
}

operator int()
{
int TotalMins = hr * 60 + min;
return TotalMins;
}

};

void main()
{
clrscr();

me t;

t.getdata(3, 59);

cout<<"Time dura on in hour and minute format"<<endl;


t.display();

int TotalMins = t; // t.operator int()

cout<<"Time dura on in minutes = "<<TotalMins<<endl;

getch();

*/

/*
Type conversion programs:

3. One class type to another class type

*/

class minute
{
int min;
public:
void getdata(int x)
{
min = x; //m.min = 150
}

void display()
{
cout<<"Time dura on in minutes = "<<min<<endl;

int getmin()
{
return min;
}

};

class hours
{
float hr; // 1 2 1.5 hr = 1hr 30 min = > 80 min
public:
void getdata(float x)
{
hr = x;

}
void display()
{
cout<<"Time dura on in hours = "<<hr<<endl;
}

hours(){}

hours(minute m)
{
hr = m.getmin() / 60.0;

};

void main()
{
clrscr();

minute m;
m.getdata(150);
m.display();
hours h;
h = m; //h = hours(m)

h.display();

getch();

You might also like