Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.36 KB | None | 0 0
  1. // LR_N1_OOP.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <Windows.h>
  7. using namespace std;
  8.  
  9. class MyDouble
  10. {
  11. private:
  12.         double number;
  13.         friend istream & operator>> (istream&, MyDouble&);
  14.         friend ostream & operator<<(ostream&, MyDouble&);
  15. public:
  16.    
  17.  
  18.     MyDouble()
  19.     {
  20.         number = 0;
  21.     }
  22.     MyDouble(double num)
  23.     {
  24.         number = num;
  25.     }
  26.     MyDouble(int num)
  27.     {
  28.         number = (double)num;
  29.     }
  30.     MyDouble(int num, int fract)
  31.     {
  32.         number = (double)num;
  33.         double fractionpart=fract;
  34.         while (fractionpart>=1)
  35.         {
  36.             fractionpart /= 10;
  37.         }
  38.         number += fractionpart;
  39.     }
  40.     ~MyDouble(){}
  41.     double getNum()
  42.     {
  43.         return number;
  44.     }
  45.     void genFromTwo(int num, int fract)
  46.     {
  47.         number = (double)num;
  48.         double fractionpart = fract;
  49.         while (fractionpart >= 1)
  50.         {
  51.             fractionpart /= 10;
  52.         }
  53.         number += fractionpart;
  54.     }
  55.     double IntPart()
  56.     {
  57.         return (int)number;
  58.     }
  59.     double Fraction()
  60.     {
  61.         return (number - this->IntPart());
  62.     }
  63.  
  64.     double operator()() {
  65.         return this->IntPart();
  66.     }
  67.  
  68.     double operator[](int index) {
  69.         return this->Fraction();
  70.     }
  71. };
  72.  
  73. istream& operator >> (istream& is, MyDouble& x)
  74. {
  75.     is >> x.number;
  76.     return is;
  77. }
  78. ostream& operator<<(ostream& os, MyDouble& x)
  79. {
  80.     os << x.number;
  81.     return os;
  82. }
  83.  
  84. void main()
  85. {
  86.     SetConsoleCP(1251);
  87.     SetConsoleOutputCP(1251);
  88.  
  89.     MyDouble obj1;
  90.     MyDouble obj2(1321, 4124312);
  91.    
  92.     cout << "Ціла частина: " << obj2() << " Дробова частина:" << obj2[4]<<" \n";
  93.     cout << "Введіть значення першого об'єкту: ";
  94.     cin >> obj1;
  95.    
  96.     cout <<"Значення першого об'єкту (потокове виведення через перевантажений оператор <<): "<< obj1<<endl;
  97.     cout << "Значення другого об'єкту: " <<obj2<<endl;
  98. /*  cout << "Введіть 2 цілих числа для створення дійсного" << endl;
  99.     int param1, param2;
  100.     cin>>param1;
  101.     cin >> param2;
  102.     obj1.genFromTwo(param1, param2);
  103.     */
  104. //  cout << "Ціла частина: " << obj1.IntPart() << " Дробова частина: " << obj1.Fraction()<<endl;
  105.  
  106. //  cout << "Ціла частина: " << obj2.IntPart() << " Дробова частина: " << obj2.Fraction()<<endl;
  107.  
  108. //  cout << "1 об'єкт: " << obj1.getNum() << endl;
  109.     system("PAUSE");
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement