白魔导士吧 关注:6贴子:956
  • 5回复贴,共1
暂时保存一


1楼2007-11-07 16:51回复
    #include<iostream.h>
    #include<math.h>
    class Figure{
    public:
    virtual double getArea()=0;
    };
    class Rectangle:public Figure{
    protected:double height,width;
    public:
    Rectangle(double h=0,double w=0){height=h;width=w;}
    double getArea(){return height*width;}
    };
    class Square:public Figure{
    protected:double width;
    public:Square(double w=0){width=w;}
     double getArea(){return width*width;}
    };
    class Triangle:public Figure{
    protected:double a,b,c;
    public:Triangle(double a1=0,double b1=0,double c1=0){a=a1;b=b1;c=c1;}
     double getArea(){double s=(a+b+c)/2.0;return sqrt(s*(s-a)*(s-b)*(s-c));}
    };
    void main()
    {Figure *fig[3]={new Triangle(2,3,3),new Rectangle(5,8),new Square(6)};
    for(int i=0;i<3;i++)
    cout<<"figure["<<i<<"] area="<<fig[i]->getArea()<<endl;
    }


    2楼2007-11-07 16:52
    回复
      #include<iostream.h>
      class point{
      private:int x,y;
      public:
      point(int i=0,int j=0){ x=i;y=j;}
      void display(){ cout<<"("<<x<<","<<y<<")"<<endl;}
      point operator+(point &p){ return point(x+p.x,y+p.y);}
      friend point operator-(point &,point &);
      };
      point operator-(point &q,point &p){ return point(q.x-p.x,q.y-p.y);}
      void main()
      {
      point p1(4,5),p2(3,9),p3;
      p3=p1+p2;
      p3.display();
      p3=p1-p2;
      p3.display();
      }


      3楼2007-11-07 16:52
      回复
        #include<iostream.h>
        const double pi=3.14159;
        class A{
        public:
        virtual double v()=0;
        };
        class Cube:public A{
        double a;
        public:virtual double v(){ return a*a*a;}
         Cube(double x=0){a=x;}
        };
        class Cylinder:public A{
        double a,b;
        public:Cylinder(double x,double y){a=x;b=y;}
         virtual double v(){return pi*a*a*b;}
        };
        class Sphere:public A{
        double a;
        public:virtual double v(){ return 4*pi*a*a*a/3;}
         Sphere(double x=0){a=x;}
        };
        void fun(A &p)
        {
        cout<<p.v();
        }
        void main()
        {
        double m,n;
        cout<<"请输入正方体的边长:";
        cin>>m;
        Cube cu(m);
        cout<<"\n正方体的体积:";fun(cu);
        cout<<"\n请输入圆柱体的底面半径和高:";cin>>m>>n;
        Cylinder cy(m,n);
        cout<<"\n圆柱体的体积:";fun(cy);
        cout<<"\n请输入球体的半径:";cin>>m;
        Sphere s(m);
        cout<<"\n球体的体积:";fun(s);
        cout<<endl;
        }


        4楼2007-11-07 16:52
        回复
          #include<iostream.h>
          #include<string.h>
          class A{
          char *p;
          public:A(char *q){p=new char[20];strcpy(p,q);}
           char *getp(){return p;}
           void operator+=(A &a)
           {strcat(p,a.p);}
          };
          void main()
          {
          char *str1="你好,";
          char *str2="哈哈!!";
          A m(str1),n(str2);
          m+=n;
          cout<<m.getp()<<endl;
          }


          5楼2007-11-07 16:53
          回复
            #include<iostream.h>
            #include<string.h>
            class A{
            char *m;int n;
            public:A(char *str=0){ n=strlen(str);m=new char[n+1];strcpy(m,str);}
             A & operator=(A &);
             void operator+=(A &);
             friend void operator==(A &,A &);
             char *getm(){return m;}
             ~A(){delete []m;}
            };
            A & A::operator=(A &b)
            {
            delete []m;
            n=b.n;
            m=new char[n+1];
            strcpy(m,b.m);
            return *this;
            }
             
            void A::operator+=(A &a)
            {
            char *str=new char[n+1];strcpy(str,m);
            n+=a.n;
            delete []m;
            m=new char[n+1];
             strcpy(m,str);
            strcat(m,a.m);
            delete []str;
            }
            void operator==(A &a,A &b)
            {
            delete []a.m;
            a.n=b.n;
            a.m=new char[a.n+1];
            strcpy(a.m,b.m);
            }
            void main()
            {
            A p("哈哈哈哈"),q("嘿嘿嘿嘿");
            p=q;cout<<p.getm()<<endl;
            A r("呵呵呵呵");
            q==r;cout<<q.getm()<<endl;
            p+=r;cout<<p.getm()<<endl;
            }


            6楼2007-11-07 16:53
            回复