面向对象程序设计1-6

来源:互联网 发布:中山大学网络电视台 编辑:程序博客网 时间:2024/05/15 23:34
1
#include <iostream.h>
class shape{
  public:
  virtual float area()=0;
};
float total(shape *s[],int n)
{
  float sum=0;
  for(int i=0;i<n;i++)
  sum+=s[i]->area();
  return sum;
}

class triangle:public shape{
  protected:
    float h,d;
  public:
    triangle(float hight,float length)
    {
      h=hight,d=length;
    }
    float area()
    {
      return 0.5*h*d;
    }
};
class rectangle : public shape{
  protected:
    float l,w;
  public:
    rectangle(float len,float weight)
    {
      l=len,w=weight;
    }
    float area()
    {
      return l*w;
    }
};
void main()
{
  shape *s[2];
  s[0]=new rectangle(1.0,2.0);
  s[1]=new triangle(2.0,2.0);
  float sum=total(s,2);
  cout<<sum;
}

2
#include <iostream.h>
class station{
  private :
    float unlead,lead,total;
  public:
    station();
    float getTotal(float unleadamount,float leadamount)
    {
      total=unleadamount*unlead + leadamount*lead;
      return total;
    }
};
station::station()
{
  unlead=17.0;
  lead=16.0;
  total=0;
}
void main()
{
  float sum;
  station st;
  sum=st.getTotal(2.0,1.0);
  cout<<sum;

}

3
#include <iostream.h>
class Rectangle{
  private:
    float l,w;
  public:
    Rectangle(float,float);
    float getarea()
    {
      return l*w;
    }
    float add_area(Rectangle r1)
    {
       return this->getarea()+r1.getarea();
    }
};
Rectangle::Rectangle(float length,float weight)
{
  l=length;
  w=weight;
}
void main()
{
  float sum;
  Rectangle Re1(1.0,1.0),Re2(3.0,3.0);
  sum=Re1.add_area(Re2);
  cout<<sum;
}

4
#include <iostream.h>
class COMPLEX{
  private:
    float real,vir;
  friend void init(COMPLEX& ,float,float);
  friend void show(COMPLEX&);
  friend COMPLEX add(COMPLEX&,COMPLEX&);
  friend COMPLEX sub(COMPLEX&,COMPLEX&);
};
COMPLEX add(COMPLEX& a,COMPLEX& b)
{
  COMPLEX c;
  c.real=a.real+b.real;
  c.vir=a.vir+b.vir;
  return c;
}
COMPLEX sub(COMPLEX& a,COMPLEX& b)
{
  COMPLEX c;
  c.real=a.real-b.real;
  c.vir=a.vir-b.vir;
  return c;
}
void init(COMPLEX& com,float r,float v)
{
  com.real=r;
  com.vir=v;
}
void show(COMPLEX& com)
{
  if(com.vir>0 && com.vir!=1)
    cout<<com.real<<"+"<<com.vir<<"i"<<endl;
  else if(com.vir==1)
    cout<<com.real<<"+i"<<endl;
  else if(com.vir<0  && com.vir!=-1)
    cout<<com.real<<"-"<<com.vir<<"i"<<endl;
  else if(com.vir==-1)
    cout<<com.real<<"-i"<<endl;
  else
    cout<<com.real<<endl;
}
void main()
{
  COMPLEX test,test1,test2,test3;
  init(test,1,1);
  init(test1,2,2);
  init(test2,3,-1);
  init(test3,4,0);
  show(test);
  show(test1);
  show(test2);
  show(test3);
  show(add(test,test1));
  show(sub(test,test1));
  show(add(test2,test3));
  show(sub(test2,test3));
}

5
#include <iostream.h>
#include <math.h>
class POSITION{
  private:
    float x,y;
  public :
    POSITION(float,float);
    float getx()
    {
      return x;
    }
    float gety()
    {
      return y;
    }
    void move(POSITION pos ,float level,float high)
    {
      pos.x+=level;
      pos.y+=high;
    }
    friend float distancetozero(POSITION& p);
    friend float distance(POSITION& p1,POSITION& p2);
};
POSITION::POSITION(float a,float b)
{
  x=a;
  y=b;
}
float distancetozero(POSITION& p)
{
   return  sqrt(p.x*p.x+p.y*p.y);
}
float distance(POSITION& p1,POSITION& p2)
{
   return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)) ;
}
void main()
{
  POSITION pos1(3.0,4.0),pos2(7.0,7.0);
  cout<<pos1.getx()<<endl;
  cout<<pos2.gety()<<endl;
  cout<<distance(pos1,pos2)<<endl;
  cout<<distancetozero(pos1)<<endl;
  cout<<distancetozero(pos2)<<endl;

}

6
#include <iostream.h>
class Goods{
  private:
    static float totalweight;
    static int totalnum;
    float weight;
  public:
    Goods(float w)
    {
      weight=w;
      totalweight+=weight;
      totalnum+=1;
    }
    static float getTotalweight()
    {
      return totalweight;
    }
    static int getTotalnum()
    {
      return totalnum;
    }
    float getWeight()
    {
      return weight;
    }
    ~Goods()
    {
      totalweight-=weight;
      totalnum-=1;
    }
};
float Goods::totalweight=0.0;
int Goods::totalnum=0;
void main()
{
  cout<<Goods::getTotalweight()<<endl<<Goods::getTotalnum()<<endl;
  Goods g1(3.0),g2(4.0),g3(5.0) ;
  cout<<Goods::getTotalweight()<<endl;
  cout<<Goods::getTotalnum()<<endl;
}