关闭

面向对象程序设计1-6

484人阅读 评论(0) 收藏 举报
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;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:56106次
    • 积分:596
    • 等级:
    • 排名:千里之外
    • 原创:20篇
    • 转载:6篇
    • 译文:0篇
    • 评论:9条
    最新评论