• 4.Object Oriented Programming


    The codes:

    // 4.Object Oriented Programming
    
    /*********************************************************
    1
    Classes (I)
    */
    
    // classes example
    #include <iostream>
    using namespace std;
    
    class CRectangle {
        int x, y;
      public:
        void set_values (int,int);
        int area () {return (x*y);}
    };
    
    void CRectangle::set_values (int a, int b) {
      x = a;
      y = b;
    }
    
    int main () {
      CRectangle rect;
      rect.set_values (3,4);
      cout << "area: " << rect.area();
      return 0;
    }
    
    
    // example: one class, two objects
    #include <iostream>
    using namespace std;
    
    class CRectangle {
        int x, y;
      public:
        void set_values (int,int);
        int area () {return (x*y);}
    };
    
    void CRectangle::set_values (int a, int b) {
      x = a;
      y = b;
    }
    
    int main () {
      CRectangle rect, rectb;
      rect.set_values (3,4);
      rectb.set_values (5,6);
      cout << "rect area: " << rect.area() << endl;
      cout << "rectb area: " << rectb.area() << endl;
      return 0;
    }
    
    
    // example: class constructor
    #include <iostream>
    using namespace std;
    
    class CRectangle {
        int width, height;
      public:
        CRectangle (int,int);
        int area () {return (width*height);}
    };
    
    CRectangle::CRectangle (int a, int b) {
      width = a;
      height = b;
    }
    
    int main () {
      CRectangle rect (3,4);
      CRectangle rectb (5,6);
      cout << "rect area: " << rect.area() << endl;
      cout << "rectb area: " << rectb.area() << endl;
      return 0;
    }
    
    
    
    // example on constructors and destructors
    #include <iostream>
    using namespace std;
    
    class CRectangle {
        int *width, *height;
      public:
        CRectangle (int,int);
        ~CRectangle ();
        int area () {return (*width * *height);}
    };
    
    CRectangle::CRectangle (int a, int b) {
      width = new int;
      height = new int;
      *width = a;
      *height = b;
    }
    
    CRectangle::~CRectangle () {
      delete width;
      delete height;
    }
    
    int main () {
      CRectangle rect (3,4), rectb (5,6);
      cout << "rect area: " << rect.area() << endl;
      cout << "rectb area: " << rectb.area() << endl;
      return 0;
    }
    
    
    
    // overloading class constructors
    #include <iostream>
    using namespace std;
    
    class CRectangle {
        int width, height;
      public:
        CRectangle ();
        CRectangle (int,int);
        int area (void) {return (width*height);}
    };
    
    CRectangle::CRectangle () {
      width = 5;
      height = 5;
    }
    
    CRectangle::CRectangle (int a, int b) {
      width = a;
      height = b;
    }
    
    int main () {
      CRectangle rect (3,4);
      CRectangle rectb; // do not include parentheses()
      cout << "rect area: " << rect.area() << endl;
      cout << "rectb area: " << rectb.area() << endl;
      return 0;
    }
    
    
    
    // copy constructor
    #include <iostream>
    using namespace std;
    
    class CRectangle {
        int width, height;
      public:
        CRectangle ();
        CRectangle (int,int);
        int area (void) {return (width*height);}
    };
    
    CRectangle::CRectangle () {
      width = 5;
      height = 5;
    }
    
    CRectangle::CRectangle (int a, int b) {
      width = a;
      height = b;
    }
    
    int main () {
      CRectangle rect (3,4);
      CRectangle rectb;
      CRectangle rectc(rectb); // copy constructor
      CRectangle rectd;
      rectd = rect; // copy assignment operator
      cout << "rect area: " << rect.area() << endl;
      cout << "rectb area: " << rectb.area() << endl;
      cout << "rectc area: " << rectc.area() << endl;
      cout << "rectd area: " << rectd.area() << endl;
      return 0;
    }
    
    
    // pointer to classes example
    #include <iostream>
    using namespace std;
    
    class CRectangle {
        int width, height;
      public:
        void set_values (int, int);
        int area (void) {return (width * height);}
    };
    
    void CRectangle::set_values (int a, int b) {
      width = a;
      height = b;
    }
    
    int main () {
      CRectangle a, *b, *c;
      CRectangle * d = new CRectangle[2];
      b= new CRectangle;
      c= &a;
      a.set_values (1,2);
      b->set_values (3,4);
      d->set_values (5,6); // d or d[0].set..
      d[1].set_values (7,8);
      cout << "a area: " << a.area() << endl;
      cout << "*b area: " << b->area() << endl;
      cout << "*c area: " << c->area() << endl;
      cout << "d[0] area: " << d[0].area() << endl;
      cout << "d[1] area: " << d[1].area() << endl;
      delete[] d;
      delete b;
      return 0;
    }
    
    
    
    
    /******************************************************
    2
    Classes (II)
    */
    
    
    // vectors: overloading operators example
    #include <iostream>
    using namespace std;
    
    class CVector {
      public:
        int x,y;
        CVector () {};
        CVector (int,int);
        CVector operator + (CVector);
    };
    
    CVector::CVector (int a, int b) {
      x = a;
      y = b;
    }
    
    CVector CVector::operator+ (CVector param) {
      CVector temp;
      temp.x = x + param.x;
      temp.y = y + param.y;
      return (temp);
    }
    
    int main () {
      CVector a (3,1);
      CVector b (1,2);
      CVector c;
      c = a + b;
      cout << c.x << "," << c.y;
      return 0;
    }
    
    
    
    // overloading operators
    #include <iostream>
    using namespace std;
    
    class CVector {
      public:
        int x,y;
        CVector () {x=0; y=0;};
        CVector (int,int);
        CVector operator - (CVector); // + -> - ,just a char
    };
    
    CVector::CVector (int a, int b) {
      x = a;
      y = b;
    }
    
    CVector CVector::operator - (CVector param) { // use operator - to add two...
      CVector temp;
      temp.x = x + param.x;
      temp.y = y + param.y;
      return (temp);
    }
    
    int main () {
      CVector a (3,1);
      CVector b (1,2);
      CVector c;
      c = a - b; // it is perfectly possible to do so.
      cout << c.x << "," << c.y;
      return 0;
    }
    
    
    // this
    #include <iostream>
    using namespace std;
    
    class CDummy {
      public:
        int isitme (CDummy& param);
    };
    
    int CDummy::isitme (CDummy& param)
    {
      if (&param == this) return true;
      else return false;
    }
    
    int main () {
      CDummy a;
      CDummy* b = &a;
      if ( b->isitme(a) )
        cout << "yes, &a is b";
      return 0;
    }
    
    
    
    // static members in classes
    #include <iostream>
    using namespace std;
    
    class CDummy {
      public:
        static int n;
        CDummy () { n++; };
        ~CDummy () { n--; };
    };
    
    int CDummy::n=0;
    
    int main () {
      CDummy a;
      CDummy b[5];
      CDummy * c = new CDummy;
      cout << a.n << endl;
      delete c;
      cout << CDummy::n << endl; //可以这样直接取
      cout << a.n << endl;
      return 0;
    }
    
    
    
    // global variable
    #include <iostream>
    using namespace std;
    int n; // global variable
    
    class CDummy {
      public:
        CDummy () { n++; };
        ~CDummy () { n--; };
    };
    
    int main () {
      n=0;
      CDummy a;
      CDummy b[5];
      CDummy * c = new CDummy;
      cout << n << endl;
      delete c;
      cout << n << endl;
      
      return 0;
    }
    
    
    
    /***********************************************************
    3
    Friendship and inheritance
    */
    
    
    // friend functions
    #include <iostream>
    using namespace std;
    
    class CRectangle {
        int width, height;
      public:
        void set_values (int, int);
        int area () {return (width * height);}
        friend CRectangle duplicate (CRectangle); //设外部函数为友
    }; //这样这个外部函数就可以调用这个类中的私有成员了
    
    void CRectangle::set_values (int a, int b) {
      width = a;
      height = b;
    }
    
    CRectangle duplicate (CRectangle rectparam) //这是外部函数
    {
      CRectangle rectres;
      rectres.width = rectparam.width*2; // 可以直接调用 width
      rectres.height = rectparam.height*2; // 可以直接调用 height
      return (rectres);
    }
    
    int main () {
      CRectangle rect, rectb;
      rect.set_values (2,3);
      rectb = duplicate (rect);
      cout << rectb.area();
      return 0;
    }
    
    
    
    // friend class
    #include <iostream>
    using namespace std;
    
    class CSquare; // an empty decaration of class CSquare
    
    //长方形
    class CRectangle {
        int width, height;
      public:
        int area ()
          {return (width * height);}
        void convert (CSquare a); //因为长方形是友,所以可以调用它
    };
    //正方形
    class CSquare {
      private:
        int side;
      public:
        void set_side (int a)
          {side=a;}
        friend class CRectangle; //设长方形为友类
    }; // 我要调用你,要你同意设我为友
    
    void CRectangle::convert (CSquare a) {
      width = a.side; //如果非友,那么a.side是访问不到的
      height = a.side;
    }
      
    int main () {
      CSquare sqr;
      CRectangle rect;
      sqr.set_side(4); //已知正方形的边长
      rect.convert(sqr); //
      cout << rect.area(); //用长方形求面积的方法来求出
      return 0;
    }
    
    
    
    // derived classes 派生类
    #include <iostream>
    using namespace std;
    
    class CPolygon { //多边形
      protected:
        int width, height;
      public:
        void set_values (int a, int b)
          { width=a; height=b;}
      };
      
    //长方形
    class CRectangle: public CPolygon { // public,起过滤作用。如为 private,则继承自 base class
      public:                           // 的各成员、函数,都将为 private ... 如不写,默认为 private
        int area ()
          { return (width * height); }
      };
    
    class CTriangle: public CPolygon { //三角形
      public:
        int area ()
          { return (width * height / 2); }
      };
      
    int main () {
      CRectangle rect;
      CTriangle trgl;
      rect.set_values (4,5); //子类可以调用父类的成员函数
      trgl.set_values (4,5);
      cout << rect.area() << endl;
      cout << trgl.area() << endl;
      return 0;
    }
    
    
    
    // constructors and derived classes
    #include <iostream>
    using namespace std;
    
    class mother {
      public:
        mother ()
          { cout << "mother: no parameters
    "; }
        mother (int a)
          { cout << "mother: int parameter
    "; }
    };
    
    class daughter : public mother {
      public:
        daughter (int a)
          { cout << "daughter: int parameter
    
    "; }
    };
    
    class son : public mother {
      public:
        son (int a) : mother (a) //先给父类参数。可以选择base class 的构造函数
          { cout << "son: int parameter
    
    "; }
    };
    
    int main () {
      daughter cynthia (0); //先调用父类的默认构造函数,再调用自身
      son daniel(0); //先显式调用父类的带参数的构造函数,再调用自身
      
      return 0;
    }
    
    
    // multiple inheritance
    #include <iostream>
    using namespace std;
    
    class CPolygon {
      protected:
        int width, height;
      public:
        void set_values (int a, int b)
          { width=a; height=b;}
      };
    
    class COutput {
      public:
        void output (int i);
      };
    
    void COutput::output (int i) {
      cout << i << endl;
      }
    
    class CRectangle: public CPolygon, public COutput { //继承自两个父类
      public:
        int area ()
          { return (width * height); }
      };
    
    class CTriangle: public CPolygon, public COutput { //继承自两个父类
      public:
        int area ()
          { return (width * height / 2); }
      };
      
    int main () {
      CRectangle rect;
      CTriangle trgl;
      rect.set_values (4,5); //调用一父
      trgl.set_values (4,5);
      rect.output (rect.area()); //调用二父
      trgl.output (trgl.area());
      return 0;
    }
    
    
    
    // multiple inheritance,三个
    #include <iostream>
    using namespace std;
    
    class CPolygon {
      protected:
        int width, height;
      public:
        void set_values (int a, int b)
          { width=a; height=b;}
      };
    
    class COutput {
      public:
        void output (int i);
      };
    
    void COutput::output (int i) {
      cout << i << endl;
      }
      
    class COutputTest {
      public:
        void outputtest( int i ) {
          cout << "en,test again:) " << 2*i << endl;
        }
      };
    
    class CRectangle: public CPolygon, public COutput { //继承自两个父类
      public:
        int area ()
          { return (width * height); }
      };
    
    class CTriangle: public CPolygon, public COutput, public COutputTest { //继承自三个父类
      public:
        int area ()
          { return (width * height / 2); }
      };
      
    int main () {
      CRectangle rect;
      CTriangle trgl;
      rect.set_values (4,5); //调用一父
      trgl.set_values (4,5);
      rect.output (rect.area()); //调用二父
      trgl.output (trgl.area());
      trgl.outputtest ( trgl.area() ); // 调用三父
      return 0;
    }
    
    
    
    /****************************************************
    4
    Polymorphism
    */
    
    
    // pointers to base class
    #include <iostream>
    using namespace std;
    
    class CPolygon {
      protected:
        int width, height;
      public:
        void set_values (int a, int b)
          { width=a; height=b; }
      };
    
    class CRectangle: public CPolygon {
      public:
        int area ()
          { return (width * height); }
      };
    
    class CTriangle: public CPolygon {
      public:
        int area ()
          { return (width * height / 2); }
      };
    
    int main () {
      CRectangle rect;
      CTriangle trgl;
      CPolygon * ppoly1 = &rect; //
      CPolygon * ppoly2 = &trgl; //
      ppoly1->set_values (4,5);
      ppoly2->set_values (4,5);
      cout << rect.area() << endl;
      cout << trgl.area() << endl;
      return 0;
    }
    
    
    
    // virtual members
    #include <iostream>
    using namespace std;
    
    class CPolygon {
      protected:
        int width, height;
      public:
        void set_values (int a, int b)
          { width=a; height=b; }
        virtual int area () //去掉virtual,输出全是0,因为调用子类的中的area失效。
          { return (0); }
      };
    
    class CRectangle: public CPolygon {
      public:
        int area ()
          { return (width * height); }
      };
    
    class CTriangle: public CPolygon {
      public:
        int area ()
          { return (width * height / 2); }
      };
    
    int main () {
      CRectangle rect;
      CTriangle trgl;
      CPolygon poly;
      CPolygon * ppoly1 = &rect;
      CPolygon * ppoly2 = &trgl;
      CPolygon * ppoly3 = &poly;
      ppoly1->set_values (4,5);
      ppoly2->set_values (4,5);
      ppoly3->set_values (4,5);
      cout << ppoly1->area() << endl;
      cout << ppoly2->area() << endl;
      cout << ppoly3->area() << endl;
      return 0;
    }
    //The member function area() has been declared as virtual in the base class because
    // it is later redefined in each derived class. 
    
    
    // virtual members -- 修改
    #include <iostream>
    using namespace std;
    
    class CPolygon {
      protected:
        int width, height;
      public:
        void set_values (int a, int b)
          { width=a; height=b; }
        int area () //去掉virtual,输出全是0,因为调用子类的中的area失效。
          { return (0); }
      };
    
    class CRectangle: public CPolygon {
      public:
        int area ()
          { return (width * height); }
      };
    
    class CTriangle: public CPolygon {
      public:
        int area ()
          { return (width * height / 2); }
      };
    
    int main () {
      CRectangle rect;
      CTriangle trgl;
      CPolygon poly;
      CRectangle * ppoly1 = &rect; //如果指针都对应上,那么就不用虚函数,不用多态继承了
      CTriangle * ppoly2 = &trgl;
      CPolygon * ppoly3 = &poly;
      ppoly1->set_values (4,5);
      ppoly2->set_values (4,5);
      ppoly3->set_values (4,5);
      cout << ppoly1->area() << endl;
      cout << ppoly2->area() << endl;
      cout << ppoly3->area() << endl;
      return 0;
    }
    
    
    
    // abstract base class
    #include <iostream>
    using namespace std;
    
    class CPolygon {
      protected:
        int width, height;
      public:
        void set_values (int a, int b)
          { width=a; height=b; }
        virtual int area (void) =0; //抽象基类之虚函数
      };
    
    class CRectangle: public CPolygon {
      public:
        int area (void)
          { return (width * height); }
      };
    
    class CTriangle: public CPolygon {
      public:
        int area (void)
          { return (width * height / 2); }
      };
    
    int main () {
      CRectangle rect;
      CTriangle trgl;
      CPolygon * ppoly1 = &rect;
      CPolygon * ppoly2 = &trgl;
      ppoly1->set_values (4,5);
      ppoly2->set_values (4,5);
      cout << ppoly1->area() << endl;
      cout << ppoly2->area() << endl;
      return 0;
    }
    
    
    
    // pure virtual members can be called
    // from the abstract base class
    #include <iostream>
    using namespace std;
    
    class CPolygon {
      protected:
        int width, height;
      public:
        void set_values (int a, int b)
          { width=a; height=b; }
        virtual int area (void) =0; //抽象基类之纯虚成员
        void printarea (void)
          { cout << this->area() << endl; } // 根据指针的不同,调用不用的area()
      };
    
    class CRectangle: public CPolygon {
      public:
        int area (void)
          { return (width * height); }
      };
    
    class CTriangle: public CPolygon {
      public:
        int area (void)
          { return (width * height / 2); }
      };
    
    int main () {
      CRectangle rect;
      CTriangle trgl;
      CPolygon * ppoly1 = &rect;
      CPolygon * ppoly2 = &trgl;
      ppoly1->set_values (4,5);
      ppoly2->set_values (4,5);
      ppoly1->printarea();
      ppoly2->printarea();
      return 0;
    }
    
    
    // dynamic allocation and polymorphism
    #include <iostream>
    using namespace std;
    
    class CPolygon {
      protected:
        int width, height;
      public:
        void set_values (int a, int b)
          { width=a; height=b; }
        virtual int area (void) =0; //
        void printarea (void)
          { cout << this->area() << endl; }
      };
    
    class CRectangle: public CPolygon {
      public:
        int area (void)
          { return (width * height); }
      };
    
    class CTriangle: public CPolygon {
      public:
        int area (void)
          { return (width * height / 2); }
      };
    
    int main () {
      CPolygon * ppoly1 = new CRectangle; //
      CPolygon * ppoly2 = new CTriangle;  //
      ppoly1->set_values (4,5);
      ppoly2->set_values (4,5);
      ppoly1->printarea();
      ppoly2->printarea();
      delete ppoly1;
      delete ppoly2;
      return 0;
    }

      

    TOP

  • 相关阅读:
    Python 不同数据类型比较
    计算机外语收集
    d3.js学习-联系力学图
    d3.js学习11
    d3.js学习10
    d3.js学习9
    d3.js学习8
    [springboot jpa] [bug] Could not open JPA EntityManager for transaction
    [spring cloud feign] [bug] 使用对象传输get请求参数
    [ethereum源码分析](5) 创建新账号
  • 原文地址:https://www.cnblogs.com/xin-le/p/4083878.html
Copyright © 2020-2023  润新知