Uploaded by BINOY GOGRI

Class to class

advertisement
Basic to class type:
Class time
{
Int hrs;
Int mins;
Public:
....
.....
time(int t)
{
hrs=t/60;
mins=t%60;
}
};
Time T;
Int duration=85;
T1=duratio (basic to class)
Class to basic:
Class number
{
Int n;
Void printDetails()
{
Cout<<n;
}
Number(int n)
{
This->n=n;
}
Operator int()
{
return n;
}
};
Int main()
{
Number nub=100;
Nub.printDetails();
Int x=nub; (class to basic)
Cout<<x;
Return 0;
Class to class:
class Class_type_one {
string a = "GeeksforGeeks";
public:
// Member function which returns
// string type data
string get_string()
{
return (a);
}
// Member function to display
void display()
{
cout << a << endl;
}
};
// Destination class, i.e
// Class type to which source class will be converted
class Class_type_two {
string b;
public:
// Operator overloading which accepts data
// of the Destination class and
// assigns those data to the source class
// Here it is for the conversion of
// Class_type_two to Class_type_one
void operator=(Class_type_one a)
{
b = a.get_string();
}
// Member function for displaying
// the data assigned to b.
void display()
{
cout << b << endl;
}
};
int main()
{
// Creating object of class Class_type_one
Class_type_one a;
// Creating object of class Class_type_two
Class_type_two b;
// CLass type conversion
// using operator overloading
b = a;
// Displaying data of object
// of class Class_type_one
a.display();
// Displaying data of object
// of class Class_type_two
b.display();
return 0;
}
Download