作业9

#include <iostream>

using namespace std;

class CType//基类

{

private:

  int radius;

  int width;

public:

  CType():radius(16),width(185)//常量用参数列表初始化

  {  
    //radius=16;

    //width=185;

  }

  CType(int r,int w)//:radius(r),width(w)

  {

    radius=r;

    width=w;

  }

  int getWidth()

  {

    return width;

  }

  int getRadius()

  {

    return radius;

  }

};

class CCar

{

private:

  int price;

  CType type;//对象

public:

  CCar();

  CCar(int p,int tr,int tw);

   int getPrice()

  {

    return price;

  }

  CType getCType()

  {

    return type;

  }

};

CCar::CCar()

{

  price=50010;

  CType();//继承父类的默认构造函数
}

CCar::CCar(int p,int tr,int tw):price(p),type(tr,tw)

{

}

int main()

{

  CCar car(48900,17,225);

  cout<<"price="<<car.getPrice();

  cout<<"\tCType.Radius="<<car.getCType().getRadius()<<"\tCType.Width="<<car.getCType().getWidth()<<endl;

  CCar car1;

  cout<<"price="<<car1.getPrice();

  cout<<"\tCType.Radius="<<car1.getCType().getRadius()<<"\tCType.Width="<<car1.getCType().getWidth()<<endl;

  return 1;
}

作业10

#include<iostream>

#include<cmath>

using namespace std;

class Point

{

private:

double x,y;

public:

  Point(){};

  Point(double a,double b)

  {

    x=a;

    y=b;

  }

  double distance()

  {

    double len;

    len=sqrt(x*x+y*y);

    return len;

  }

  friend double dis_point2(Point &,Point &);

  Point(Point &);//复制的构造函数
};

Point::Point(Point &p)
{

  x=p.x;

  y=p.y;

}

double dis_point2(Point &s1,Point &s2)

{

  double t_x=s1.x-s2.x;

  double t_y=s1.y-s2.y;

  double t_xy=sqrt(t_x*t_x+t_y*t_y);

  return t_xy;
}

int main()

{

  Point p0;

  Point p3(p0); //调用复制的构造函数

  Point p1(2.5,6.3),p2(5.5,9.0);

  cout<<"p1到原点的距离为:"<<p1.distance()<<endl

  cout<<"p2到原点的距离为:"<<p2.distance()<<endl;

  cout<<"p1到p2的距离为:"<<dis_point2(p1,p2)<<endl;

  return 1;

}

 作业11

#include <iostream>

using namespace std;

class MyComlex

{

private:

  double real,imag;

public:

MyComlex();

MyComlex(double r,double i);

void outCom();

MyComlex operator-(const MyComlex &c);

friend MyComlex operator+(const MyComlex &c1,const MyComlex &c2);

};

MyComlex::MyComlex():real(0),imag(0){};

MyComlex::MyComlex(double r,double i)

{

  real=r;

  imag=i;

}

void MyComlex::outCom()

{

  cout<<"("<<real<<","<<imag<<")";

}

MyComlex MyComlex::operator-(const MyComlex &c)

{

  return MyComlex(this->real-c.real,this->imag-c.imag);

}

MyComlex operator+(const MyComlex &c1,const MyComlex &c2)

{

  return MyComlex(c1.real+c2.real,c1.imag+c2.imag);

}

int main()

{

  MyComlex c1(1,2),c2(3,4),res;

  c1.outCom();

  cout<<"operator+";

  c2.outCom();

  cout<<"=";

  res=c1+c2;//调用运算符重载函数

  res.outCom();

  cout<<endl;

  c1.outCom();

  cout<<"operator-";

  c2.outCom();

  cout<<"=";

  res=c1-c2;//调用运算符重载函数

  res.outCom();

  cout<<endl;

  return 0;

}

作业12

#include <iostream>

using namespace std;

class vehicle

{

public:

int wheels;

float weight;

public:

  vehicle(int wheels1,float weight1)

  {

    wheels=wheels1;

    weight=weight1;

  }

  int get_wheels()

  {

    return wheels;

  }

  float get_weight()

  {

    return weight;

  }

  void show()

  {

    cout<<"轮胎数:"<<wheels<<",车重"<<weight<<endl;

  }

  ~vehicle()

  {}

};

class car:public vehicle

{

protected:

  int speed_max;

  car(int wheels2,float weight2,int speedmax):vehicle(wheels2,weight2)

  {

    speed_max=speedmax;

    cout<<"最大速度:"<<speed_max<<endl;

  }

  ~car()

  {}

};

class toyota_car:public car

{

private:

  double length,width,height;

public:

  toyota_car(int wheels3,float weight3,int speedmax3,double length3,double width3,double height3):

  car(wheels3,weight3,speedmax3)

  {

    length=length3;

    width=width3;

    height=height3;

  }

  void show3()

  {

    cout<<"车长:"<<length<<",车宽"<<width<<",车高"<<height<<endl;

  }

  ~toyota_car()

  {}

};

int main()

{

  vehicle ve(4,2.4);

  ve.show();

  toyota_car tcar(4,2.4,240,4.8,1.85,1.65);

  tcar.show3();

  return 1;

}

 posted on 2021-06-20 14:57  HuJiao粉  阅读(37)  评论(0)    收藏  举报