thespeedracer38

Friend function implementation

Jan 28th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. /* Friend function implementation
  2. */
  3. #include <iostream>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7.  
  8. class Metre;
  9. class Centimetre{
  10.     double length;
  11.     public:
  12.         inline Centimetre();
  13.         inline Centimetre(double);
  14.         inline ~Centimetre();
  15.         inline void input();
  16.         inline friend void compare(Centimetre&, Metre&);
  17. };
  18.  
  19. class Metre{
  20.     double length;
  21.     public:
  22.         inline Metre();
  23.         inline Metre(double);
  24.         inline ~Metre();
  25.         inline void input();
  26.         friend void compare(Centimetre&, Metre&);
  27. };
  28.  
  29. void compare(Centimetre &c_obj, Metre & m_obj){
  30.     if(c_obj.length > (m_obj.length * 100)){
  31.         cout << c_obj.length << " cm is greater\n";
  32.     }
  33.     else {
  34.         cout << m_obj.length << " m is greater\n";
  35.     }
  36. }
  37.  
  38. Metre::Metre(){
  39.     length = 0.0;
  40. }
  41.  
  42. Centimetre::Centimetre(){
  43.     length = 0.0;
  44. }
  45.  
  46. Centimetre::Centimetre(double length){
  47.     Centimetre::length = length;
  48. }
  49.  
  50. Centimetre::~Centimetre(){
  51.     cout << "Centimetre object is destroyed!" << endl;
  52. }
  53.  
  54. Metre::Metre(double length){
  55.     Metre::length = length;
  56. }
  57.  
  58. Metre::~Metre(){
  59.     cout << "Metre object is destroyed!" << endl;
  60. }
  61.  
  62. void Centimetre::input(){
  63.     cout << "Enter the length in cm: ";
  64.     cin >> length;
  65. }
  66.  
  67. void Metre::input(){
  68.     cout << "Enter the length in m: ";
  69.     cin >> length;
  70. }
  71.  
  72. int main() {
  73.     system("cls");
  74.     Centimetre c_obj;
  75.     Metre m_obj;
  76.     c_obj.input();
  77.     m_obj.input();
  78.     compare(c_obj, m_obj);
  79.     system("pause");
  80.     return 0;
  81. }
Add Comment
Please, Sign In to add comment