Advertisement
lil_SV

Alina_Lab2

Nov 19th, 2022
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class cOne {
  6. public:
  7.     cOne() : d(0) {}
  8.  
  9.     cOne(double d, string s) : d(d), s(s) {}
  10.  
  11.     cOne(cOne const &a) : d(a.d), s(a.s) {}
  12.  
  13.     cOne &operator=(cOne const &a) {
  14.         d = a.d;
  15.         s = a.s;
  16.         return *this;
  17.     }
  18.  
  19.     void print() {
  20.         cout<<"class cOne\n";
  21.         cout << "d = " << d << '\n';
  22.         cout << "s = " << s << '\n';
  23.         cout << "-----------------------\n";
  24.     }
  25.  
  26.     double getD() const {
  27.         return d;
  28.     }
  29.  
  30.     void setD(double _d) {
  31.         d = _d;
  32.     }
  33.  
  34.     const string &getS() const {
  35.         return s;
  36.     }
  37.  
  38.     void setS(const string &_s) {
  39.         s = _s;
  40.     }
  41.  
  42.     ~cOne() {}
  43.  
  44. private:
  45.     double d;
  46.     string s;
  47. };
  48.  
  49. class cTwo {
  50. public:
  51.     cTwo() : l(0), obj(cOne()) {}
  52.  
  53.     cTwo(long l, const cOne &obj) : l(l), obj(obj) {}
  54.  
  55.     cTwo(cTwo const &a) : l(a.l), obj(a.obj) {}
  56.  
  57.     long getL() const {
  58.         return l;
  59.     }
  60.  
  61.     void setL(long _l) {
  62.         l = _l;
  63.     }
  64.  
  65.     const cOne &getObj() const {
  66.         return obj;
  67.     }
  68.  
  69.     void setObj(const cOne &_obj) {
  70.         obj = _obj;
  71.     }
  72.  
  73.     cTwo &operator=(cTwo const &a) {
  74.         l = a.l;
  75.         obj = a.obj;
  76.         return *this;
  77.     }
  78.  
  79.     void print() {
  80.         cout<<"class cTwo\n";
  81.         cout << "l = " << l << '\n';
  82.         cout << "-----------------------\n";
  83.         obj.print();
  84.     }
  85.  
  86.     ~cTwo(){}
  87.  
  88. private:
  89.     long l;
  90.     cOne obj;
  91. };
  92.  
  93. int main() {
  94.     cOne a(3.14, "PI");
  95.     a.print();
  96.     cOne b(2.71, "E");
  97.     b.print();
  98.     a=b;
  99.     a.print();
  100.     cout<<"\nСоздаем класс cTwo\n";
  101.     cTwo c(123, a);
  102.     c.print();
  103.     cout<<"\nПроверяем методы доступа\n";
  104.     c.setL(-666);
  105.     c.setObj(b);
  106.     c.print();
  107.     cTwo d(88888, b);
  108.     c=d;
  109.     c.print();
  110.     return 0;
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement