fork download
  1. #ifndef DISTANCE_H_
  2. #define DISTANCE_H_
  3.  
  4. using namespace std;
  5. #include <iostream>
  6.  
  7. //const to facilitate conversions between the two classes
  8.  
  9. const double FEET_IN_METERS = 0.3048;
  10. const double FURLONG_TO_FEET = 660;
  11.  
  12. class Furlong;
  13. class Metric;
  14.  
  15. class Furlong
  16. {
  17. public:
  18.  
  19. Furlong(int fur = 0, int yar = 0, int fee = 0.0);
  20.  
  21. // Copy Constructor.
  22.  
  23. Furlong( const Furlong& rhs );
  24.  
  25. // Destructor.
  26.  
  27. ~Furlong(void);
  28.  
  29. // Assignment operator=.
  30. Furlong& operator=(const Furlong& rhs);
  31.  
  32. int furlong;
  33. int yards;
  34. double feet;
  35.  
  36. };
  37.  
  38. class Metric
  39. {
  40.  
  41. public:
  42. Metric(int kilo = 0, double mete = 0.0);
  43.  
  44. Metric (const Metric& rhs);
  45.  
  46. ~Metric(void);
  47.  
  48. Metric& operator=(const Metric& rhs);
  49.  
  50. Metric (Furlong& f);
  51.  
  52. operator Furlong();
  53.  
  54. int kilometers;
  55. double meters;
  56. };
  57.  
  58. #endif
  59.  
  60. // -----
  61.  
  62. #include <stdlib.h>
  63. //#include "Distances.h"
  64.  
  65. //FURLONG CLASS DEFINITIONS
  66.  
  67. //Constructor
  68.  
  69. Furlong::Furlong(int fur, int yar, int fee)
  70. {
  71. furlong = fur;
  72. yards = yar;
  73. feet = fee;
  74. }
  75.  
  76. // Copy Constructor.
  77.  
  78. Furlong::Furlong( const Furlong& rhs )
  79. {
  80. furlong = rhs.furlong;
  81. yards = rhs.yards;
  82. feet = rhs.feet;
  83. }
  84.  
  85. // Destructor.
  86.  
  87. Furlong::~Furlong(void)
  88. {
  89.  
  90. }
  91.  
  92. // Assignment operator=.
  93.  
  94. Furlong& Furlong::operator=(const Furlong& rhs)
  95. {
  96.  
  97. furlong = rhs.furlong;
  98. yards = rhs.yards;
  99. feet = rhs.feet;
  100.  
  101. return *this;
  102.  
  103. }
  104. //METRIC CLASS DEFINITONS
  105.  
  106.  
  107.  
  108. Metric::Metric(int kilo, double mete)
  109. {
  110. kilometers = kilo;
  111. meters = mete;
  112. }
  113. Metric::Metric(const Metric& rhs)
  114. {
  115.  
  116. kilometers = rhs.kilometers;
  117. meters = rhs. meters;
  118. }
  119.  
  120. Metric::~Metric(void)
  121. {
  122.  
  123. }
  124.  
  125. Metric& Metric::operator=(const Metric& rhs)
  126. {
  127. kilometers = rhs.kilometers;
  128. meters = rhs.meters;
  129.  
  130. return *this;
  131. }
  132.  
  133. // conversion constructor
  134.  
  135. Metric::Metric (Furlong& f)
  136. {
  137. kilometers = 3;
  138. meters = 2.0;
  139. }
  140.  
  141. //conversion operator
  142.  
  143. Metric::operator Furlong()
  144. {
  145.  
  146. return Furlong(1, 2, 3.0);
  147. }
  148.  
  149. // -----
  150.  
  151. #include <stdlib.h>
  152. //#include "Distances.h"
  153.  
  154.  
  155. using namespace std;
  156.  
  157. int main()
  158. {
  159. Furlong one(1,2,3.0);
  160. Furlong three(4,5,6.0);
  161. Metric two(7,8.0);
  162. Metric four(9, 10.0);
  163.  
  164. one = two;
  165. four = three;
  166.  
  167.  
  168. return 0;
  169. }
Success #stdin #stdout 0s 15224KB
stdin
Standard input is empty
stdout
Standard output is empty