Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // LR_N1_OOP.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <Windows.h>
- using namespace std;
- class MyDouble
- {
- private:
- double number;
- friend istream & operator>> (istream&, MyDouble&);
- friend ostream & operator<<(ostream&, MyDouble&);
- public:
- MyDouble()
- {
- number = 0;
- }
- MyDouble(double num)
- {
- number = num;
- }
- MyDouble(int num)
- {
- number = (double)num;
- }
- MyDouble(int num, int fract)
- {
- number = (double)num;
- double fractionpart=fract;
- while (fractionpart>=1)
- {
- fractionpart /= 10;
- }
- number += fractionpart;
- }
- ~MyDouble(){}
- double getNum()
- {
- return number;
- }
- void genFromTwo(int num, int fract)
- {
- number = (double)num;
- double fractionpart = fract;
- while (fractionpart >= 1)
- {
- fractionpart /= 10;
- }
- number += fractionpart;
- }
- double IntPart()
- {
- return (int)number;
- }
- double Fraction()
- {
- return (number - this->IntPart());
- }
- double operator()() {
- return this->IntPart();
- }
- double operator[](int index) {
- return this->Fraction();
- }
- };
- istream& operator >> (istream& is, MyDouble& x)
- {
- is >> x.number;
- return is;
- }
- ostream& operator<<(ostream& os, MyDouble& x)
- {
- os << x.number;
- return os;
- }
- void main()
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- MyDouble obj1;
- MyDouble obj2(1321, 4124312);
- cout << "Ціла частина: " << obj2() << " Дробова частина:" << obj2[4]<<" \n";
- cout << "Введіть значення першого об'єкту: ";
- cin >> obj1;
- cout <<"Значення першого об'єкту (потокове виведення через перевантажений оператор <<): "<< obj1<<endl;
- cout << "Значення другого об'єкту: " <<obj2<<endl;
- /* cout << "Введіть 2 цілих числа для створення дійсного" << endl;
- int param1, param2;
- cin>>param1;
- cin >> param2;
- obj1.genFromTwo(param1, param2);
- */
- // cout << "Ціла частина: " << obj1.IntPart() << " Дробова частина: " << obj1.Fraction()<<endl;
- // cout << "Ціла частина: " << obj2.IntPart() << " Дробова частина: " << obj2.Fraction()<<endl;
- // cout << "1 об'єкт: " << obj1.getNum() << endl;
- system("PAUSE");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement