Advertisement
lil_SV

lab3

Sep 26th, 2022
790
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. class COne {
  7. public:
  8.     COne()
  9.             : f(0), ps(0) {
  10.     }
  11.  
  12.     COne(float _f, char *_ps)
  13.             : f(_f) {
  14.         ps = new char[strlen(_ps)];
  15.         strcpy(ps, _ps);
  16.     }
  17.  
  18.     COne(const COne &Q)
  19.             : f(Q.f) {
  20.         ps = new char[strlen(Q.ps)];
  21.         strcpy(ps, Q.ps);
  22.     }
  23.  
  24.     float getF() const { //getter setter
  25.         return f;
  26.     }
  27.  
  28.     void setF(float f) {
  29.         COne::f = f;
  30.     }
  31.  
  32.     char *getPs() const {
  33.         return ps;
  34.     }
  35.  
  36.     void setPs(char *_ps) {
  37.         ps = new char[strlen(_ps)];
  38.         strcpy(ps, _ps);
  39.     }
  40.  
  41.     // возвращает ссылку на СОne
  42.     // принимает константную ссылку на Q
  43.     COne &operator=(COne const &Q) {    //перегрузка
  44.         delete ps;
  45.         ps = new char[strlen(Q.ps)];
  46.         strcpy(ps, Q.ps);
  47.         f = Q.f;
  48.     }
  49.  
  50.     void print() {
  51.         cout << "----------------\n";
  52.         cout << "Class COne\n";
  53.         cout << "f = " << f << endl;
  54.         printf("ps = %s\n", ps);
  55.     }
  56.  
  57.     ~COne() {      //деструктор
  58.         delete ps;
  59.     }
  60.  
  61. private:
  62.     float f;
  63.     char *ps;
  64. };
  65.  
  66. class CTwo {
  67. public:
  68.     CTwo()
  69.             : l(-2), obj(COne()) {
  70.     }
  71.  
  72.     CTwo(long _l, COne f)
  73.             : l(_l), obj(f) {
  74.     }
  75.  
  76.     CTwo(const CTwo &m)
  77.             : l(m.l), obj(m.obj) {
  78.     }
  79.  
  80.     long getL() const {
  81.         return l;
  82.     }
  83.  
  84.     void setL(long l) {
  85.         CTwo::l = l;
  86.     }
  87.  
  88.     const COne &getObj() const {
  89.         return obj;
  90.     }
  91.  
  92.     void setObj(const COne &obj) {
  93.         CTwo::obj = obj;
  94.     }
  95.  
  96.     CTwo &operator=(CTwo const &ctwo) {
  97.         l = ctwo.l;
  98.         obj = ctwo.obj;
  99.     }
  100.  
  101.     void print() {
  102.         cout << "----------------\n";
  103.         cout << "Class CTwo\n";
  104.         cout << "l = " << l << endl;
  105.         obj.print();
  106.     }
  107.  
  108.     ~CTwo() {}
  109.  
  110. private:
  111.     long l;
  112.     COne obj;
  113. };
  114.  
  115. class CThree : CTwo {
  116. public:
  117.     CThree()
  118.             : ad(69) {
  119.  
  120.     }
  121.  
  122.     CThree(int _ad, CTwo & cTwo)
  123.             : ad(_ad), CTwo(cTwo) {
  124.  
  125.     }
  126.  
  127.     CThree(const CThree & cthree)
  128.     :ad(cthree.ad), CTwo(cthree)
  129.     {
  130.     }
  131.  
  132.     int getAd() const {
  133.         return ad;
  134.     }
  135.  
  136.     void setAd(int ad) {
  137.         CThree::ad = ad;
  138.     }
  139.  
  140.     void print(){
  141.         cout << "----------------\n";
  142.         cout << "Class CThree\n";
  143.         cout << "ad = " << ad << endl;
  144.         CTwo::print();
  145.     }
  146.     ~CThree(){}
  147.  
  148. private:
  149.     int ad;
  150. };
  151.  
  152.  
  153. int main() {
  154.     char *a = "Pidorass";
  155.     COne s(89, a);
  156.     CTwo f(69, s);
  157.     CThree v(228, f);
  158.     v.print();
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement