thespeedracer38

Class type to Class Type conversion

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