thespeedracer38

Class type to Class Type conversion using operator function

Feb 11th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. /*  Conversion from Class Type to Class Type
  2.     using operator function
  3. */
  4.  
  5. #include <iostream>
  6. #include <cstdlib>
  7.  
  8. using namespace std;
  9.  
  10. class C{
  11.     int m;
  12.     public:
  13.         C(int);
  14.         ~C();
  15.         void show();
  16. };
  17.  
  18. class D{
  19.     int m;
  20.     public:
  21.         D(int);
  22.         ~D();
  23.         void show();
  24.         operator C();
  25. };
  26.  
  27. inline C::C(int m){
  28.     C::m = m;
  29. }
  30.  
  31. inline C::~C(){
  32.     cout << "Destructor Called for C" << endl;
  33. }
  34.  
  35. inline D::operator C(){
  36.     return C(m);
  37. }
  38.  
  39. inline D::D(int m){
  40.     D::m = m;
  41. }
  42.  
  43. inline D::~D(){
  44.     cout << "Destructor Called for D" << endl;
  45. }
  46.  
  47. inline void D::show(){
  48.     cout << m << endl;
  49. }
  50.  
  51. inline void C::show(){
  52.     cout << m << endl;
  53. }
  54.  
  55. int main() {
  56.     D obj1(3);
  57.     cout << "obj1.m = ";
  58.     obj1.show();
  59.     C obj2 = obj1;
  60.     cout << "obj2.m = ";
  61.     obj2.show();
  62.     cin.get();
  63.     return 0;
  64. }
Add Comment
Please, Sign In to add comment