Advertisement
Guest User

наследование ( с новой фунуцией)

a guest
Mar 30th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. // наследование.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. #include <iostream>
  7.  
  8. using namespace std;
  9.  
  10.  
  11. class Rational {
  12. private:
  13.     int chis, znam;
  14. public:
  15.     Rational() {
  16.         chis = 0; znam = 1;
  17.     }
  18.  
  19.     Rational(int x) {
  20.         chis = x; znam = 1;
  21.     }
  22.  
  23.     Rational(int c, int z) {
  24.         chis = c; znam = z;
  25.     }
  26.  
  27.     Rational(const char *str) {
  28.         chis = 0;
  29.         znam = 1;
  30.         sscanf(str, "%d / %d", &chis, &znam);
  31.     }
  32.  
  33.     int get_chis() const {
  34.         return chis;
  35.     }
  36.  
  37.     int get_znam() const {
  38.         return znam;
  39.     }
  40.     double approx() {
  41.         return get_chis()/double(get_znam());
  42.     }
  43.  
  44.     friend double approx(Rational r);
  45. };
  46.  
  47.  
  48. class MegaRational: public Rational {
  49. public:
  50.     MegaRational(): Rational()
  51.     {
  52.     }
  53.  
  54.     MegaRational(int x): Rational(x)
  55.     {
  56.     }
  57.  
  58.     MegaRational(int c, int z): Rational(c, z)
  59.     {
  60.     }
  61.  
  62.     MegaRational(const char *str) : Rational(str)
  63.     {
  64.     }
  65.  
  66.     void drob(Rational a) {
  67.         if(a.get_chis() > a.get_znam()) {
  68.             cout << "B = " << a.get_chis() / a.get_znam() << " и " << a.get_chis()/double(a.get_znam()) <<endl;
  69.         }
  70.         else
  71.             cout << "C = " << a.get_chis() % a.get_znam()<< endl;
  72.     }
  73. };
  74.  
  75. double approx(Rational r) {
  76.     return r.chis/double(r.znam);
  77. }
  78.  
  79.  
  80. Rational operator+(Rational a, Rational b) {
  81.     Rational res(a.get_chis()*b.get_znam() + a.get_znam()*b.get_chis(),
  82.                  a.get_znam()*b.get_znam());
  83.     return res;
  84. }
  85.  
  86. Rational operator-(Rational a, Rational b) {
  87.     Rational res(a.get_chis() * b.get_znam() - b.get_chis() * a.get_znam(),
  88.                  a.get_znam() * b.get_znam());
  89.     return res;
  90. }
  91.  
  92. Rational operator*(Rational a, Rational b) {
  93.     Rational res(a.get_chis() * b.get_chis(),
  94.                  a.get_znam() * b.get_znam());
  95.     return res;
  96. }
  97.  
  98. Rational operator/(Rational a, Rational b) {
  99.     Rational res(a.get_chis() * b.get_znam(),
  100.         a.get_znam() * b.get_chis());
  101.     return res;
  102. }
  103.  
  104.  
  105. ostream& operator<<(ostream& o, Rational r) {
  106.     o << r.get_chis() << " / " << r.get_znam();
  107.     return o;
  108. }
  109.  
  110.  
  111. int _tmain(int argc, _TCHAR* argv[])
  112. {
  113.     Rational     r1(1, 5),
  114.                  r2(4, 8),
  115.                  r3(4);
  116.     MegaRational r4(3, 2),
  117.                  r5("2/7");
  118.  
  119.     Rational a = operator+(r1, r2);
  120.     Rational b = operator-(r1, r5);
  121.     Rational c = operator*(r2, r3);
  122.     Rational d = operator/(r5, r2);
  123.        
  124.     cout << " r1 = " << r1 << endl;
  125.     cout << " r2 = " << r2 << endl;
  126.     cout << " r3 = " << r3 << endl;
  127.     cout << " r4 = " << r4 << endl;
  128.     cout << " r5 = " << r5 << endl;
  129.  
  130.     cout << endl;
  131.  
  132.     Rational res = r1 + r2;
  133.     cout << " a = " << a.approx() << " = " << res << endl;
  134.     cout << " b = " << b.approx() << " = " << r1 - r5 << endl;
  135.     cout << " c = " << c.approx() << " = " << r2 * r3 << endl;
  136.     cout << " d = " << d.approx() << " = " << r5 / r2 << endl;
  137.  
  138.     cout << endl;
  139.     cout << " r4 = " << r4.drob() << " = " << r4 << endl;
  140.    
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement